Skip to content

Commit

Permalink
Map canonical names and aliases in a schema
Browse files Browse the repository at this point in the history
  • Loading branch information
waiting-for-dev committed Jan 2, 2019
1 parent d34b479 commit 8fd23c5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
11 changes: 11 additions & 0 deletions core/lib/rom/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,17 @@ def to_ast
[:schema, [name, attributes.map(&:to_ast)]]
end

# Hash from canonical name to alias for aliased attributes
#
# @return [Hash<Symbol, Symbol>]
#
# @api public
def alias_mapping
each_with_object({}) do |attr, obj|
obj[attr.name] = attr.alias if attr.aliased?
end
end

# @api private
def set!(key, value)
instance_variable_set("@#{ key }", value)
Expand Down
9 changes: 9 additions & 0 deletions core/spec/unit/rom/schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,13 @@
expect(schema.project(:name).primary_key_name).to be(:id)
end
end

describe '#alias_mapping' do
it 'returns a hash from canonical name to alias including aliased attributes' do
attrs = { id: ROM::Types::Integer.meta(name: :id, alias: :pk), name: ROM::Types::String }
schema = ROM::Schema.define(:name, attributes: attrs.values)

expect(schema.alias_mapping).to eq(id: :pk)
end
end
end

0 comments on commit 8fd23c5

Please sign in to comment.