Class: LunaPark::Mappers::Simple

Inherits:
Object
  • Object
show all
Defined in:
lib/luna_park/mappers/simple.rb

Overview

Abstract mapper for transform data from Entity attributes schema to Database row schema

Examples:

class TransactionMapper < LunaPark::Mappers::Simple
  def self.from_row(row)
    {
      uid: row[:id],
      charge: {
        amount:   row[:charge_amount],
        currency: row[:charge_currency]
      },
      comment: row[:comment]
    }
  end

  def self.to_row(attributes)
    {
      id:              attributes[:uid],
      charge_amount:   attributes.dig(:charge, :amount),
      charge_currency: attributes.dig(:charge, :currency),
      comment:         attributes[:comment]
    }
  end
end
# attribute scheme repeats entity schema
attributes = { charge: { amount: 10, currency: 'USD' }, comment: 'FooBar' }

# Mapper transforms entity attributes to database row
row = TransactionMapper.to_row(attributes)

# Mapper also transforms database row to entity attributes
attributes = TransactionMapper.from_row(row)
# find
row = sequel_database_table.where(id: 42).first
attributes = TransactionMapper.from_row(row)
Entities::Transaction.new(attributes)
# update
new_attributes = { charge: { amount: 10, currency: 'USD' }, comment: 'FooBar' }
new_row = TransactionMapper.to_row(new_attributes)
sequel_database_table.update(42, new_row)
# create
attributes = { charge: { amount: 10, currency: 'USD' }, comment: 'FooBar' }
transaction = Entities::Transaction.new(attributes)

row            = TransactionMapper.to_row(transaction.to_h)  # => { charge_amount: 10, ... }
new_row        = sequel_database_table.returning.insert(row) # => { id: 123, charge_amount: 10, ... }
new_attributes = TransactionMapper.from_row(new_row)         # => { uid: 123, charge: { amount: 10, ... }
transaction.set_attributes(new_attributes)

Direct Known Subclasses

Codirectional

Class Method Summary collapse

Class Method Details

.from_row(_row) ⇒ Object

This method is abstract.


85
86
87
# File 'lib/luna_park/mappers/simple.rb', line 85

def from_row(_row)
  raise LunaPark::Errors::AbstractMethod
end

.from_rows(rows) ⇒ Object

Transforms array of rows to array of attribute hashes

Raises:



68
69
70
71
72
73
# File 'lib/luna_park/mappers/simple.rb', line 68

def from_rows(rows)
  return [] if rows.nil?
  raise Errors::NotArray.new(input: rows) unless rows.is_a?(Array)

  rows.to_a.map { |hash| from_row(hash) }
end

.to_row(_attrs) ⇒ Object

This method is abstract.


90
91
92
# File 'lib/luna_park/mappers/simple.rb', line 90

def to_row(_attrs)
  raise LunaPark::Errors::AbstractMethod
end

.to_rows(attr_hashes) ⇒ Object

Transforms array of attribute hashes to array of rows

Raises:



77
78
79
80
81
82
# File 'lib/luna_park/mappers/simple.rb', line 77

def to_rows(attr_hashes)
  return [] if attr_hashes.nil?
  raise Errors::NotArray.new(input: attr_hashes) unless attr_hashes.is_a?(Array)

  attr_hashes.to_a.map { |entity| to_row(entity) }
end