Class: ROM::Factory::DSL

Inherits:
BasicObject
Defined in:
lib/rom/factory/dsl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, schema: {}, relation:, factories:) {|_self| ... } ⇒ DSL

Returns a new instance of DSL.

Yields:

  • (_self)

Yield Parameters:



25
26
27
28
29
30
31
32
# File 'lib/rom/factory/dsl.rb', line 25

def initialize(name, schema: {}, relation:, factories:, &block)
  @_name = name
  @_relation = relation
  @_factories = factories
  @_schema = schema.dup
  @_valid_names = _relation.schema.attributes.map(&:name)
  yield(self)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (private)



71
72
73
74
75
76
77
# File 'lib/rom/factory/dsl.rb', line 71

def method_missing(meth, *args, &block)
  if _valid_names.include?(meth)
    define_attr(meth, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#_factoriesObject (readonly)

Returns the value of attribute _factories.



23
24
25
# File 'lib/rom/factory/dsl.rb', line 23

def _factories
  @_factories
end

#_nameObject (readonly)

Returns the value of attribute _name.



23
24
25
# File 'lib/rom/factory/dsl.rb', line 23

def _name
  @_name
end

#_relationObject (readonly)

Returns the value of attribute _relation.



23
24
25
# File 'lib/rom/factory/dsl.rb', line 23

def _relation
  @_relation
end

#_schemaObject (readonly)

Returns the value of attribute _schema.



23
24
25
# File 'lib/rom/factory/dsl.rb', line 23

def _schema
  @_schema
end

#_valid_namesObject (readonly)

Returns the value of attribute _valid_names.



23
24
25
# File 'lib/rom/factory/dsl.rb', line 23

def _valid_names
  @_valid_names
end

Instance Method Details

#association(name) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rom/factory/dsl.rb', line 57

def association(name)
  assoc = _relation.associations[name]
  other = _relation.__registry__[assoc.target]

  fk = _relation.foreign_key(other)
  pk = other.primary_key

  block = -> { create(name)[pk] }

  _schema[fk] = attributes::Callable.new(self, block)
end

#callObject



34
35
36
# File 'lib/rom/factory/dsl.rb', line 34

def call
  ::ROM::Factory::Builder.new(_schema, _relation)
end

#create(name, *args) ⇒ Object



38
39
40
# File 'lib/rom/factory/dsl.rb', line 38

def create(name, *args)
  _factories[name, *args]
end

#fake(*args) ⇒ Object



53
54
55
# File 'lib/rom/factory/dsl.rb', line 53

def fake(*args)
  ::ROM::Factory.fake(*args)
end

#sequence(meth, &block) ⇒ Object



42
43
44
45
46
# File 'lib/rom/factory/dsl.rb', line 42

def sequence(meth, &block)
  if _valid_names.include?(meth)
    define_sequence(meth, block)
  end
end

#timestampsObject



48
49
50
51
# File 'lib/rom/factory/dsl.rb', line 48

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