Class: ROM::Factory::DSL
- Inherits:
- BasicObject
- Defined in:
- lib/rom/factory/dsl.rb
Instance Attribute Summary collapse
-
#_factories ⇒ Object
readonly
Returns the value of attribute _factories.
-
#_name ⇒ Object
readonly
Returns the value of attribute _name.
-
#_relation ⇒ Object
readonly
Returns the value of attribute _relation.
-
#_schema ⇒ Object
readonly
Returns the value of attribute _schema.
-
#_valid_names ⇒ Object
readonly
Returns the value of attribute _valid_names.
Instance Method Summary collapse
- #association(name) ⇒ Object
- #call ⇒ Object
- #create(name, *args) ⇒ Object
- #fake(*args) ⇒ Object
-
#initialize(name, schema: {}, relation:, factories:) {|_self| ... } ⇒ DSL
constructor
A new instance of DSL.
- #sequence(meth, &block) ⇒ Object
- #timestamps ⇒ Object
Constructor Details
#initialize(name, schema: {}, relation:, factories:) {|_self| ... } ⇒ DSL
Returns a new instance of DSL.
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
#_factories ⇒ Object (readonly)
Returns the value of attribute _factories.
23 24 25 |
# File 'lib/rom/factory/dsl.rb', line 23 def _factories @_factories end |
#_name ⇒ Object (readonly)
Returns the value of attribute _name.
23 24 25 |
# File 'lib/rom/factory/dsl.rb', line 23 def _name @_name end |
#_relation ⇒ Object (readonly)
Returns the value of attribute _relation.
23 24 25 |
# File 'lib/rom/factory/dsl.rb', line 23 def _relation @_relation end |
#_schema ⇒ Object (readonly)
Returns the value of attribute _schema.
23 24 25 |
# File 'lib/rom/factory/dsl.rb', line 23 def _schema @_schema end |
#_valid_names ⇒ Object (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 |
#call ⇒ Object
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 |
#timestamps ⇒ Object
48 49 50 51 |
# File 'lib/rom/factory/dsl.rb', line 48 def created_at { ::Time.now } updated_at { ::Time.now } end |