class RomFactory::Factory

Attributes

_name[R]
_relation[R]
_schema[R]

Public Class Methods

new() { |self| ... } click to toggle source
# File lib/rom_factory/factory.rb, line 3
def initialize
  yield(self)
end

Public Instance Methods

create(attrs) click to toggle source
# File lib/rom_factory/factory.rb, line 14
def create(attrs)
  values = _schema.merge(wrap_attributes_to_callable(attrs)).map {|k, v| [k, v.call]}
  record_id = _relation.insert(values.to_h)
  Struct.new(values.to_h.merge(id: record_id))
end
factory(name:, relation:) { |self| ... } click to toggle source
# File lib/rom_factory/factory.rb, line 7
def factory(name:, relation:, &block)
  @_relation = RomFactory::Config.config.container.relations.fetch(relation)
  @_name = name
  @_schema = {}
  yield(self)
end
sequence(method_id, &block) click to toggle source
# File lib/rom_factory/factory.rb, line 20
def sequence(method_id, &block)
  if _relation.schema.attributes.map(&:name).include?(method_id)
    define_sequence_method(method_id, block)
  end
  self.send(method_id)
end
timestamps() click to toggle source
# File lib/rom_factory/factory.rb, line 27
def timestamps
  created_at { Time.now }
  updated_at { Time.now }
end

Private Instance Methods

define_regular_method(method_id) click to toggle source
# File lib/rom_factory/factory.rb, line 57
def define_regular_method(method_id)
  define_singleton_method method_id, Proc.new {|v = nil, &block|
    if block
      _schema[method_id] = RomFactory::Attributes::Callable.new(block)
    else
      _schema[method_id] = RomFactory::Attributes::Regular.new(v)
    end
  }
end
define_sequence_method(method_id, block) click to toggle source
# File lib/rom_factory/factory.rb, line 51
def define_sequence_method(method_id, block)
  self.define_singleton_method method_id, ->(){
    _schema[method_id] = RomFactory::Attributes::Sequence.new(&block)
  }
end
method_missing(method_id, *arguments, &block) click to toggle source
Calls superclass method
# File lib/rom_factory/factory.rb, line 42
def method_missing(method_id, *arguments, &block)
  if _relation.schema.attributes.map(&:name).include?(method_id)
    define_regular_method(method_id)
    self.send(method_id, *arguments, &block)
  else
    super
  end
end
wrap_attributes_to_callable(attrs) click to toggle source
# File lib/rom_factory/factory.rb, line 38
def wrap_attributes_to_callable(attrs)
  attrs.map {|k, v| [k, RomFactory::Attributes::Regular.new(v)]}.to_h
end