Class: LunaPark::Mappers::Codirectional
- Defined in:
- lib/luna_park/mappers/codirectional.rb,
lib/luna_park/mappers/codirectional/copyists/slice.rb,
lib/luna_park/mappers/codirectional/copyists/nested.rb
Overview
DSL for describe Nested Schema translation: entity attributes to database row and vice-versa
Defined Under Namespace
Modules: Copyists
Class Method Summary collapse
- .attr(attr, row: nil) ⇒ Object
- .attrs(*common_keys) ⇒ Object
- .from_row(input) ⇒ Object
-
.map(*common_keys, attr: nil, row: nil) ⇒ Object
Describe translation between two schemas: attr and table.
- .to_row(input) ⇒ Object
Methods inherited from Simple
Class Method Details
.attr(attr, row: nil) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/luna_park/mappers/codirectional.rb', line 57 def attr(attr, row: nil) return attrs(attr) if row.nil? attr_path = to_path(attr) row_path = to_path(row) if attr_path == row_path attrs(attr_path) else nested_copyists << Copyists::Nested.new(attrs_path: attr_path, row_path: row_path) end end |
.attrs(*common_keys) ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/luna_park/mappers/codirectional.rb', line 74 def attrs(*common_keys) common_keys.each do |common_key| path = to_path(common_key) if path.is_a?(Array) nested_copyists << Copyists::Nested.new(attrs_path: path, row_path: path) else slice_copyist.add_key(path) end end end |
.from_row(input) ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/luna_park/mappers/codirectional.rb', line 85 def from_row(input) row = input.to_h {}.tap do |attrs| slice_copyist.from_row(row: row, attrs: attrs) nested_copyists.each { |copyist| copyist.from_row(row: row, attrs: attrs) } end end |
.map(*common_keys, attr: nil, row: nil) ⇒ Object
Describe translation between two schemas: attr and table
46 47 48 49 50 |
# File 'lib/luna_park/mappers/codirectional.rb', line 46 def map(*common_keys, attr: nil, row: nil) attrs(*common_keys) if common_keys.any? self.attr attr, row: row if attr end |
.to_row(input) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/luna_park/mappers/codirectional.rb', line 93 def to_row(input) attrs = input.to_h {}.tap do |row| slice_copyist.to_row(row: row, attrs: attrs) nested_copyists.each { |copyist| copyist.to_row(row: row, attrs: attrs) } end end |