Class: LunaPark::Mappers::Codirectional

Inherits:
Simple
  • Object
show all
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

Examples:

class Mappers::Transaction < LunaPark::Mappers::Codirectional
  map attr: :uid,                 row: :id
  map attr: [:charge, :amount],   row: :charge_amount
  map attr: [:charge, :currency], row: :charge_currency # using aliased args
  map :comment
end

mapper = Mappers::Transaction

attrs = { charge: { amount: 10, currency: 'USD' }, comment: 'Foobar' }
transaction = Entities::Transaction.new(attrs)

# Mapper transforms attr attributes to database row and vice-verse
row       = mapper.to_row(transaction)        # => {          charge_amount: 10, charge_currency: 'USD', comment: 'Foobar' }
new_row   = sequel_database_table.insert(row) # => { id:  42, charge_amount: 10, charge_currency: 'USD', comment: 'Foobar' }
new_attrs = mapper.from_row(new_row)          # => { uid: 42, charge: { amount: 10, currency: 'USD' },   comment: 'Foobar' }

transaction.set_attributes(new_attrs)
transaction.to_h # => { uid: 42, charge: { amount: 10, currency: 'USD' }, comment: 'Foobar' }

Direct Known Subclasses

LunaPark::Mapper

Defined Under Namespace

Modules: Copyists

Class Method Summary collapse

Methods inherited from Simple

from_rows, to_rows

Class Method Details

.attr(attr, row: attr, mapper: nil, array: nil) ⇒ Object

Examples:

class Mappers::Transaction < LunaPark::Mappers::Codirectional
  attr :uid,              row: :id
  attr %i[charge amount], row: :charge_amount
  attr :comment
end


58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/luna_park/mappers/codirectional.rb', line 58

def attr(attr, row: attr, mapper: nil, array: nil)
  attr_path = to_path(attr)
  row_path  = to_path(row)

  if attr_path == row_path && !attr_path.is_a?(Array)
    slice_copyist.add_key(attr_path)
  else
    nested_copyists << Copyists::Nested.new(
      attrs_path: attr_path, row_path: row_path,
      mapper: mapper, map_array: array
    )
  end
end

.attrs(*common_keys) ⇒ Object

Examples:

class Mappers::Transaction < LunaPark::Mappers::Codirectional
  attrs :created_at, :updated_at, :deleted_at
end


76
77
78
# File 'lib/luna_park/mappers/codirectional.rb', line 76

def attrs(*common_keys)
  common_keys.each { |common_key| attr common_key }
end

.from_row(input) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/luna_park/mappers/codirectional.rb', line 80

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

Examples:

class Mappers::Transaction < LunaPark::Mappers::Codirectional
  map attr: :id,                row: :uid
  map attr: [:charge, :amount], row: :charge_amount
  map :comment
end

Mappers::Transaction.from_row({ id: 1, charge_amount: 2 })     # => { uid: 1, charge: { amount: 2 } }
Mappers::Transaction.to_row({ uid: 1, charge: { amount: 2 } }) # => { id: 1, charge_amount: 2 }


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



88
89
90
91
92
93
94
# File 'lib/luna_park/mappers/codirectional.rb', line 88

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