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
- #_traits ⇒ Object readonly
- #_valid_names ⇒ Object readonly
Instance Method Summary collapse
-
#association(name, *traits, **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.
- #trait(name, parents = [], &block) ⇒ Object
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.
34 35 36 37 38 39 40 41 42 |
# File 'lib/rom/factory/dsl.rb', line 34 def initialize(name, attributes: AttributeRegistry.new, relation:, factories:) @_name = name @_relation = relation @_factories = factories @_attributes = attributes.dup @_traits = {} @_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.
140 141 142 143 144 145 146 |
# File 'lib/rom/factory/dsl.rb', line 140 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)
30 31 32 |
# File 'lib/rom/factory/dsl.rb', line 30 def _attributes @_attributes end |
#_factories ⇒ Object (readonly)
30 31 32 |
# File 'lib/rom/factory/dsl.rb', line 30 def _factories @_factories end |
#_name ⇒ Object (readonly)
30 31 32 |
# File 'lib/rom/factory/dsl.rb', line 30 def _name @_name end |
#_relation ⇒ Object (readonly)
30 31 32 |
# File 'lib/rom/factory/dsl.rb', line 30 def _relation @_relation end |
#_traits ⇒ Object (readonly)
31 32 33 |
# File 'lib/rom/factory/dsl.rb', line 31 def _traits @_traits end |
#_valid_names ⇒ Object (readonly)
30 31 32 |
# File 'lib/rom/factory/dsl.rb', line 30 def _valid_names @_valid_names end |
Instance Method Details
#association(name, *traits, **options) ⇒ Object
Create an association attribute
130 131 132 133 134 135 |
# File 'lib/rom/factory/dsl.rb', line 130 def association(name, *traits, **) assoc = _relation.associations[name] builder = -> { _factories.for_relation(assoc.target) } _attributes << attributes::Association.new(assoc, builder, *traits, **) 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.
45 46 47 |
# File 'lib/rom/factory/dsl.rb', line 45 def call ::ROM::Factory::Builder.new(_attributes, _traits, relation: _relation) end |
#create(name, *args) ⇒ Object
Delegate to a builder and persist a struct
54 55 56 |
# File 'lib/rom/factory/dsl.rb', line 54 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
101 102 103 |
# File 'lib/rom/factory/dsl.rb', line 101 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
63 64 65 |
# File 'lib/rom/factory/dsl.rb', line 63 def sequence(meth, &block) define_sequence(meth, block) if _valid_names.include?(meth) end |
#timestamps ⇒ Object
Set timestamp attributes
70 71 72 73 |
# File 'lib/rom/factory/dsl.rb', line 70 def created_at { ::Time.now } updated_at { ::Time.now } end |
#trait(name, parents = [], &block) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/rom/factory/dsl.rb', line 105 def trait(name, parents = [], &block) _traits[name] = DSL.new( "#{_name}_#{name}", attributes: _traits.values_at(*parents).flat_map(&:elements).inject( AttributeRegistry.new, :<< ), relation: _relation, factories: _factories, &block )._attributes end |