Class: ROM::Factory::DSL
- Inherits:
- BasicObject
- Defined in:
- lib/rom/factory/dsl.rb
Overview
Factory builder DSL
Instance Attribute Summary collapse
- #_attributes ⇒ Object readonly
- #_factories ⇒ Object readonly
- #_name ⇒ Object readonly
- #_relation ⇒ Object readonly
- #_valid_names ⇒ Object readonly
Instance Method Summary collapse
-
#association(name, options = {}) ⇒ Object
Create an association attribute.
- #call ⇒ Object private
-
#create(name, *args) ⇒ Object
Delegate to a builder and persist a struct.
-
#fake(*args) ⇒ Object
Create a fake value using Faker gem.
-
#initialize(name, attributes: AttributeRegistry.new, relation:, factories:) {|_self| ... } ⇒ DSL
constructor
private
A new instance of DSL.
-
#sequence(meth, &block) ⇒ Object
private
Create a sequence attribute.
-
#timestamps ⇒ Object
Set timestamp attributes.
Constructor Details
#initialize(name, attributes: AttributeRegistry.new, relation:, factories:) {|_self| ... } ⇒ DSL
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of DSL.
31 32 33 34 35 36 37 38 |
# File 'lib/rom/factory/dsl.rb', line 31 def initialize(name, attributes: AttributeRegistry.new, relation:, factories:) @_name = name @_relation = relation @_factories = factories @_attributes = attributes.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)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
126 127 128 129 130 131 132 |
# File 'lib/rom/factory/dsl.rb', line 126 def method_missing(meth, *args, &block) if _valid_names.include?(meth) define_attr(meth, *args, &block) else super end end |
Instance Attribute Details
#_attributes ⇒ Object (readonly)
28 29 30 |
# File 'lib/rom/factory/dsl.rb', line 28 def _attributes @_attributes end |
#_factories ⇒ Object (readonly)
28 29 30 |
# File 'lib/rom/factory/dsl.rb', line 28 def _factories @_factories end |
#_name ⇒ Object (readonly)
28 29 30 |
# File 'lib/rom/factory/dsl.rb', line 28 def _name @_name end |
#_relation ⇒ Object (readonly)
28 29 30 |
# File 'lib/rom/factory/dsl.rb', line 28 def _relation @_relation end |
#_valid_names ⇒ Object (readonly)
28 29 30 |
# File 'lib/rom/factory/dsl.rb', line 28 def _valid_names @_valid_names end |
Instance Method Details
#association(name, options = {}) ⇒ Object
Create an association attribute
116 117 118 119 120 121 |
# File 'lib/rom/factory/dsl.rb', line 116 def association(name, = {}) assoc = _relation.associations[name] builder = -> { _factories.for_relation(assoc.target) } _attributes << attributes::Association.new(assoc, builder, ) end |
#call ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
41 42 43 |
# File 'lib/rom/factory/dsl.rb', line 41 def call ::ROM::Factory::Builder.new(_attributes, _relation) end |
#create(name, *args) ⇒ Object
Delegate to a builder and persist a struct
50 51 52 |
# File 'lib/rom/factory/dsl.rb', line 50 def create(name, *args) _factories[name, *args] end |
#fake(type) ⇒ Object #fake(api, type) ⇒ Object #fake(api, type, *args) ⇒ Object
Create a fake value using Faker gem
99 100 101 |
# File 'lib/rom/factory/dsl.rb', line 99 def fake(*args) ::ROM::Factory.fake(*args) end |
#sequence(meth, &block) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Create a sequence attribute
59 60 61 62 63 |
# File 'lib/rom/factory/dsl.rb', line 59 def sequence(meth, &block) if _valid_names.include?(meth) define_sequence(meth, block) end end |
#timestamps ⇒ Object
Set timestamp attributes
68 69 70 71 |
# File 'lib/rom/factory/dsl.rb', line 68 def created_at { ::Time.now } updated_at { ::Time.now } end |