Class: RomFactory::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/rom_factory/factory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Factory

Returns a new instance of Factory.

Yields:

  • (_self)

Yield Parameters:



3
4
5
# File 'lib/rom_factory/factory.rb', line 3

def initialize
  yield(self)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *arguments, &block) ⇒ Object (private)



42
43
44
45
46
47
48
49
# 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

Instance Attribute Details

#_nameObject (readonly)

Returns the value of attribute _name.



32
33
34
# File 'lib/rom_factory/factory.rb', line 32

def _name
  @_name
end

Instance Method Details

#create(attrs) ⇒ Object



14
15
16
17
18
# 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| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



7
8
9
10
11
12
# 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) ⇒ Object



20
21
22
23
24
25
# 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

#timestampsObject



27
28
29
30
# File 'lib/rom_factory/factory.rb', line 27

def timestamps
  created_at { Time.now }
  updated_at { Time.now }
end