module LunaPark::Extensions::DataMapper::InstanceMethods

Public Instance Methods

transaction(&block) click to toggle source
# File lib/luna_park/extensions/data_mapper.rb, line 69
def transaction(&block)
  dataset.transaction(&block)
end

Private Instance Methods

dataset() click to toggle source

Usefull for extensions

# File lib/luna_park/extensions/data_mapper.rb, line 189
def dataset
  raise NotImplementedError
end
entity_class() click to toggle source
# File lib/luna_park/extensions/data_mapper.rb, line 178
def entity_class
  self.class.entity_class
end
from_row(input) click to toggle source

@example

def find(id)
  entity_attrs = from_row(products.where(id: id))
  Entity.new(entity_attrs)
end
# File lib/luna_park/extensions/data_mapper.rb, line 129
def from_row(input)
  return if input.nil?
  raise ArgumentError, 'Can not be an Array' if input.is_a?(Array)

  mapper_class ? mapper_class.from_row(input.to_h) : input
end
from_rows(rows_array) click to toggle source

@example

def where_type(type)
  entities_attrs = from_rows(products.where(type: type))
  entities_attrs.map { |entity_attrs| Entity.new(entity_attrs) }
end
# File lib/luna_park/extensions/data_mapper.rb, line 120
def from_rows(rows_array)
  mapper_class ? mapper_class.from_rows(rows_array) : rows_array
end
mapper_class() click to toggle source

Read config

# File lib/luna_park/extensions/data_mapper.rb, line 174
def mapper_class
  self.class.mapper_class
end
primary_key() click to toggle source
# File lib/luna_park/extensions/data_mapper.rb, line 182
def primary_key
  self.class.db_primary_key
end
read_all(rows) click to toggle source

Get collection of entities from row @example

def where_type(type)
  read_all products.where(type: type)
end
# File lib/luna_park/extensions/data_mapper.rb, line 82
def read_all(rows)
  to_entities from_rows rows.to_a
end
read_one(row) click to toggle source

Get one entity from row @example

def find(id)
  read_all products.where(id: id)
end
# File lib/luna_park/extensions/data_mapper.rb, line 91
def read_one(row)
  to_entity from_row row
end
to_entities(attrs_array) click to toggle source

@example

to_entities(attributes_hashes) # => Array of Entity
to_entities(attributes_hash)   # => Array of Entity
# File lib/luna_park/extensions/data_mapper.rb, line 141
def to_entities(attrs_array)
  Array(attrs_array).map { |attrs| to_entity(attrs) }
end
to_entity(attrs) click to toggle source

@example

to_entity(attributes_hash) # => Entity
# File lib/luna_park/extensions/data_mapper.rb, line 147
def to_entity(attrs)
  return if attrs.nil?

  entity_class ? entity_class.new(attrs) : attrs
end
to_row(input) click to toggle source

@example

def create(entity)
  row = to_row(entity)
  database.insert(row)
end
# File lib/luna_park/extensions/data_mapper.rb, line 111
def to_row(input)
  mapper_class ? mapper_class.to_row(input) : input.to_h
end
to_rows(input_array) click to toggle source

@example

def create(entities)
  rows = to_rows(entities)
  database.insert_many(rows)
end
# File lib/luna_park/extensions/data_mapper.rb, line 102
def to_rows(input_array)
  mapper_class ? mapper_class.to_rows(input_array) : input_array.map(&:to_h)
end
wrap(input) click to toggle source

@example

to_entity(attributes_hash) # => Entity
to_entity(entity)          # => Entity
# File lib/luna_park/extensions/data_mapper.rb, line 166
def wrap(input)
  return if input.nil?

  entity_class ? entity_class.wrap(input) : input
end
wrap_all(input_array) click to toggle source

@example

to_entities(attributes_hashes) # => Array of Entity
to_entities(entities)          # => Array of Entity
to_entities(entity)            # => Array of Entity
# File lib/luna_park/extensions/data_mapper.rb, line 159
def wrap_all(input_array)
  Array(input_array).map { |input| wrap(input) }
end