Class: ROM::Mapper::Header

Inherits:
Object
  • Object
show all
Includes:
Adamantium, Enumerable
Defined in:
lib/rom/mapper/header.rb

Overview

Mapper header wrapping axiom header and providing mapping information

Class Method Summary collapse

Instance Method Summary collapse

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, options = {})
  return input if input.is_a?(self)

  keys       = options.fetch(:keys, [])
  header     = Axiom::Relation::Header.coerce(input, keys: keys)

  mapping    = options.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

Returns:



62
63
64
# File 'lib/rom/mapper/header.rb', line 62

def [](name)
  attributes.fetch(name)
end

#attribute_namesObject

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

#keysArray<Attribute>

Return all key attributes

Returns:



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

#mappingObject

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