Class: ROM::Mapper::Header
- Inherits:
-
Object
- Object
- ROM::Mapper::Header
- Includes:
- Adamantium, Enumerable
- Defined in:
- lib/rom/mapper/header.rb
Overview
Mapper header wrapping axiom header and providing mapping information
Class Method Summary collapse
-
.build(input, options = {}) ⇒ Object
private
Build a header.
Instance Method Summary collapse
-
#[](name) ⇒ Attribute
Return attribute with the given name.
-
#attribute_names ⇒ Object
private
Return attribute names.
-
#each(&block) ⇒ Object
private
Iterate over attributes.
-
#keys ⇒ Array<Attribute>
Return all key attributes.
-
#mapping ⇒ Object
private
Return attribute mapping.
Class Method Details
.build(input, options = {}) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Build a header
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/rom/mapper/header.rb', line 15 def self.build(input, = {}) return input if input.is_a?(self) keys = .fetch(:keys, []) header = Axiom::Relation::Header.coerce(input, keys: keys) mapping = .fetch(:map, {}) attributes = header.each_with_object({}) { |field, object| attribute = Attribute.coerce(field, mapping[field.name]) object[attribute.name] = attribute } new(header, attributes) end |
Instance Method Details
#[](name) ⇒ Attribute
Return attribute with the given name
62 63 64 |
# File 'lib/rom/mapper/header.rb', line 62 def [](name) attributes.fetch(name) end |
#attribute_names ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return attribute names
69 70 71 |
# File 'lib/rom/mapper/header.rb', line 69 def attribute_names map(&:name) end |
#each(&block) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Iterate over attributes
76 77 78 79 80 |
# File 'lib/rom/mapper/header.rb', line 76 def each(&block) return to_enum unless block_given? attributes.each_value(&block) self end |
#keys ⇒ Array<Attribute>
Return all key attributes
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rom/mapper/header.rb', line 45 def keys # FIXME: find a way to simplify this header.keys.flat_map { |key_header| key_header.flat_map { |key| attributes.values.select { |attribute| attribute.tuple_key == key.name } } } end |
#mapping ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return attribute mapping
33 34 35 36 37 |
# File 'lib/rom/mapper/header.rb', line 33 def mapping each_with_object({}) { |attribute, hash| hash.update attribute.mapping } end |