Class: ROM::Factory::DSL
Overview
Factory builder DSL
Defined Under Namespace
Modules: Kernel
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, *seq_traits, traits: EMPTY_ARRAY, **options) ⇒ Object
Create an association attribute.
- #call ⇒ Object private
-
#create(name, *args) ⇒ Object
Delegate to a builder and persist a struct.
-
#fake ⇒ 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.
- #inspect ⇒ Object (also: #to_s) private
-
#sequence(meth) ⇒ Object
private
Create a sequence attribute.
-
#timestamps ⇒ Object
Set timestamp attributes.
- #trait(name, parents = []) ⇒ 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.
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/rom/factory/dsl.rb', line 53 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) ⇒ 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.
195 196 197 198 199 200 201 |
# File 'lib/rom/factory/dsl.rb', line 195 def method_missing(meth, ...) if _valid_names.include?(meth) define_attr(meth, ...) else super end end |
Instance Attribute Details
#_attributes ⇒ Object (readonly)
49 50 51 |
# File 'lib/rom/factory/dsl.rb', line 49 def _attributes @_attributes end |
#_factories ⇒ Object (readonly)
49 50 51 |
# File 'lib/rom/factory/dsl.rb', line 49 def _factories @_factories end |
#_name ⇒ Object (readonly)
49 50 51 |
# File 'lib/rom/factory/dsl.rb', line 49 def _name @_name end |
#_relation ⇒ Object (readonly)
49 50 51 |
# File 'lib/rom/factory/dsl.rb', line 49 def _relation @_relation end |
#_struct_namespace ⇒ Object (readonly)
49 50 51 |
# File 'lib/rom/factory/dsl.rb', line 49 def _struct_namespace @_struct_namespace end |
#_traits ⇒ Object (readonly)
50 51 52 |
# File 'lib/rom/factory/dsl.rb', line 50 def _traits @_traits end |
#_valid_names ⇒ Object (readonly)
49 50 51 |
# File 'lib/rom/factory/dsl.rb', line 49 def _valid_names @_valid_names end |
Instance Method Details
#association(name, *seq_traits, traits: EMPTY_ARRAY, **options) ⇒ Object
Create an association attribute
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/rom/factory/dsl.rb', line 170 def association(name, *seq_traits, traits: EMPTY_ARRAY, **) 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, *seq_traits, *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.
65 66 67 68 69 70 71 72 73 |
# File 'lib/rom/factory/dsl.rb', line 65 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
80 |
# File 'lib/rom/factory/dsl.rb', line 80 def create(name, *args) = _factories[name, *args] |
#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
138 |
# File 'lib/rom/factory/dsl.rb', line 138 def fake(...) = ::ROM::Factory.fake(...) |
#inspect ⇒ Object Also known as: to_s
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.
189 |
# File 'lib/rom/factory/dsl.rb', line 189 def inspect = "#<#{self.class} name=#{_name}>" |
#sequence(meth) ⇒ 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
87 88 89 |
# File 'lib/rom/factory/dsl.rb', line 87 def sequence(meth, &) define_sequence(meth, &) if _valid_names.include?(meth) end |
#timestamps ⇒ Object
Set timestamp attributes
94 95 96 97 |
# File 'lib/rom/factory/dsl.rb', line 94 def created_at { ::Time.now } updated_at { ::Time.now } end |
#trait(name, parents = []) ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/rom/factory/dsl.rb', line 140 def trait(name, parents = [], &) _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, & )._attributes end |