Module: LunaPark::Extensions::DataMapper::ClassMethods

Defined in:
lib/luna_park/extensions/data_mapper.rb

Constant Summary collapse

DEFAULT_PRIMARY_KEY =
:id

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#__entity_coercion__Object (readonly)

Returns the value of attribute entity_coercion.



64
65
66
# File 'lib/luna_park/extensions/data_mapper.rb', line 64

def __entity_coercion__
  @__entity_coercion__
end

#entity_classObject (readonly)

Returns the value of attribute entity_class.



64
65
66
# File 'lib/luna_park/extensions/data_mapper.rb', line 64

def entity_class
  @entity_class
end

#mapper_classObject (readonly)

Returns the value of attribute mapper_class.



64
65
66
# File 'lib/luna_park/extensions/data_mapper.rb', line 64

def mapper_class
  @mapper_class
end

Instance Method Details

#__build_entity_coercion__(coercion) ⇒ Object

Raises:

  • (ArgumentError)


148
149
150
151
152
153
154
155
# File 'lib/luna_park/extensions/data_mapper.rb', line 148

def __build_entity_coercion__(coercion)
  return entity_class.method(coercion) if coercion.is_a? Symbol
  return coercion                      if coercion.respond_to?(:call)

  raise ArgumentError, 'coercion MUST be call\'able, Symbol or nil' unless coercion.nil?

  infer_entity_coercion
end

#__define_class__(name, parent) ⇒ Object



195
196
197
198
# File 'lib/luna_park/extensions/data_mapper.rb', line 195

def __define_class__(name, parent)
  klass = Class.new(parent)
  const_set name, klass
end

#__define_constants__(not_found: LunaPark::Extensions::DataMapper::NotFound) ⇒ Object



191
192
193
# File 'lib/luna_park/extensions/data_mapper.rb', line 191

def __define_constants__(not_found: LunaPark::Extensions::DataMapper::NotFound)
  __define_class__ 'NotFound', not_found
end

#base_anonymous_mapperObject

This method is abstract.

Examples:

class Transaction::Repository < LunaPark::Repository
  # Parent of this mapper will be changed
  mapper do
    attr :foo
  end

  def self.base_anonymous_mapper
    MyBaseMapper
  end
end


177
178
179
# File 'lib/luna_park/extensions/data_mapper.rb', line 177

def base_anonymous_mapper
  LunaPark::Mappers::Codirectional
end

#entity(entity, coercion = nil) ⇒ Object

Configure tagret entity class and coercion for it

Examples:

default coercion for entity type than responds to .call

class MyRepository
  entity MyEntity
end

input = { foo: 'FOO', bar: 'BAR' }
MyRepository.new.send(:wrap, input) == MyEntity.call(input)

default coercion for entity type than responds to .wrap

class MyRepository
  entity MyEntity
end

input = { foo: 'FOO', bar: 'BAR' }
MyRepository.new.send(:wrap, input) == MyEntity.wrap(input)

custom coercion by symbol, for entity type than responds to described_method

class MyRepository
  entity MyEntity, :build
end

input = { foo: 'FOO', bar: 'BAR' }
MyRepository.new.send(:wrap, input) == MyEntity.build(input)

custom coercion by callable object

class MyRepository
  entity MyEntity, BUILD_ENTITY
end

input = { foo: 'FOO', bar: 'BAR' }
MyRepository.new.send(:wrap, input) == BUILD_ENTITY(input)


101
102
103
104
105
# File 'lib/luna_park/extensions/data_mapper.rb', line 101

def entity(entity, coercion = nil)
  @entity_class = entity
  @__entity_coercion__ = __build_entity_coercion__(coercion)
  @entity_class
end

#infer_entity_coercionObject



157
158
159
160
161
162
# File 'lib/luna_park/extensions/data_mapper.rb', line 157

def infer_entity_coercion
  return entity_class.method(:call) if entity_class.respond_to?(:call)
  return entity_class.method(:wrap) if entity_class.respond_to?(:wrap)

  ->(input) { entity_class.new(input.to_h) }
end

#inherited(klass) ⇒ Object



200
201
202
203
204
205
206
# File 'lib/luna_park/extensions/data_mapper.rb', line 200

def inherited(klass)
  klass.__define_constants__(not_found: NotFound)
  klass.entity entity_class, __entity_coercion__
  klass.mapper mapper_class
  klass.primary_key row_primary_key
  super
end

#mapper(mapper = Undefined, &block) ⇒ Object

Configure Mapper

Examples:

With anonymous mapper

class Repository < LunaPark::Repository
  mapper do
    attr :foo, row: :fuu
  end
end

Repository.mapper_class.to_row(foo: 'Foo') # => { fuu: 'Foo' }

With mapper class

class Repository::Mapper < LunaPark::Mapper
  attr :foo, row: :fuu
end

class Repository < LunaPark::Repository
  mapper Mapper
end

Repository.new.mapper_class.to_row(foo: 'Foo') # => { fuu: 'Foo' }

Without mapper

class Repository < LunaPark::Repository
  def example_to_row(attrs)
    to_row attrs
  end
end

Repository.new.example_to_row(foo: 'Foo') # => { foo: 'Foo' }

Raises:

  • (ArgumentError)


138
139
140
141
142
143
144
145
146
# File 'lib/luna_park/extensions/data_mapper.rb', line 138

def mapper(mapper = Undefined, &block)
  raise ArgumentError, 'Expected mapper xOR block' unless (mapper == Undefined) ^ block.nil?

  return @mapper_class = mapper if block.nil?

  @mapper_class = Class.new(base_anonymous_mapper)
  @mapper_class.class_eval(&block)
  @mapper_class
end

#primary_key(attr) ⇒ Object



181
182
183
# File 'lib/luna_park/extensions/data_mapper.rb', line 181

def primary_key(attr)
  @row_primary_key = attr
end

#row_primary_keyObject



187
188
189
# File 'lib/luna_park/extensions/data_mapper.rb', line 187

def row_primary_key
  @row_primary_key || DEFAULT_PRIMARY_KEY
end