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
- #_struct_namespace ⇒ 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(type, *args, **options) ⇒ Object
Create a fake value using Faker gem.
-
#initialize(name, relation:, factories:, struct_namespace:, attributes: AttributeRegistry.new) {|_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, relation:, factories:, struct_namespace:, attributes: AttributeRegistry.new) {|_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.
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rom/factory/dsl.rb', line 40 def initialize(name, relation:, factories:, struct_namespace:, attributes: AttributeRegistry.new) @_name = name @_relation = relation @_factories = factories @_struct_namespace = struct_namespace @_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.
168 169 170 171 172 173 174 |
# File 'lib/rom/factory/dsl.rb', line 168 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)
36 37 38 |
# File 'lib/rom/factory/dsl.rb', line 36 def _attributes @_attributes end |
#_factories ⇒ Object (readonly)
36 37 38 |
# File 'lib/rom/factory/dsl.rb', line 36 def _factories @_factories end |
#_name ⇒ Object (readonly)
36 37 38 |
# File 'lib/rom/factory/dsl.rb', line 36 def _name @_name end |
#_relation ⇒ Object (readonly)
36 37 38 |
# File 'lib/rom/factory/dsl.rb', line 36 def _relation @_relation end |
#_struct_namespace ⇒ Object (readonly)
36 37 38 |
# File 'lib/rom/factory/dsl.rb', line 36 def _struct_namespace @_struct_namespace end |
#_traits ⇒ Object (readonly)
37 38 39 |
# File 'lib/rom/factory/dsl.rb', line 37 def _traits @_traits end |
#_valid_names ⇒ Object (readonly)
36 37 38 |
# File 'lib/rom/factory/dsl.rb', line 36 def _valid_names @_valid_names end |
Instance Method Details
#association(name, *traits, **options) ⇒ Object
Create an association attribute
153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/rom/factory/dsl.rb', line 153 def association(name, *traits, **) assoc = _relation.associations[name] if assoc.is_a?(::ROM::SQL::Associations::OneToOne) && .fetch(:count, 1) > 1 ::Kernel.raise ::ArgumentError, "count cannot be greater than 1 on a OneToOne" end 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.
52 53 54 55 56 57 58 59 60 |
# File 'lib/rom/factory/dsl.rb', line 52 def call ::ROM::Factory::Builder.new( _attributes, _traits, relation: _relation, struct_namespace: _struct_namespace, factories: _factories ) end |
#create(name, *args) ⇒ Object
Delegate to a builder and persist a struct
67 68 69 |
# File 'lib/rom/factory/dsl.rb', line 67 def create(name, *args) _factories[name, *args] end |
#fake(type) ⇒ Object #fake(genre, type) ⇒ Object #fake(genre, type, **options) ⇒ Object #fake(genre, subgenre, type, **options) ⇒ Object
Create a fake value using Faker gem
123 124 125 |
# File 'lib/rom/factory/dsl.rb', line 123 def fake(type, *args, **) ::ROM::Factory.fake(type, *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
76 77 78 |
# File 'lib/rom/factory/dsl.rb', line 76 def sequence(meth, &block) define_sequence(meth, block) if _valid_names.include?(meth) end |
#timestamps ⇒ Object
Set timestamp attributes
83 84 85 86 |
# File 'lib/rom/factory/dsl.rb', line 83 def created_at { ::Time.now } updated_at { ::Time.now } end |
#trait(name, parents = [], &block) ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/rom/factory/dsl.rb', line 127 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, struct_namespace: _struct_namespace, &block )._attributes end |