Class: ROM::Factory::Factories
- Inherits:
-
Object
- Object
- ROM::Factory::Factories
- Extended by:
- Dry::Configurable, Initializer
- Defined in:
- lib/rom/factory/factories.rb
Overview
A registry with all configured factories
Instance Attribute Summary collapse
-
#registry ⇒ Hash<Symbol=>Builder>
readonly
A map with defined db-backed builders.
-
#rom ⇒ ROM::Container
readonly
Configured rom container.
-
#struct_namespace(namespace = Undefined) ⇒ Factories
readonly
Get factories with a custom struct namespace.
Instance Method Summary collapse
-
#[](name, *traits, **attrs) ⇒ ROM::Struct
Create and persist a new struct.
- #builder_struct_namespace(ns) ⇒ Object private
-
#define(spec, opts = EMPTY_HASH, &block) ⇒ ROM::Factory::Builder
Define a new builder.
- #extend_builder(name, parent, relation_name, ns, &block) ⇒ Object private
- #for_relation(relation) ⇒ Object private
- #infer_factory_name(name) ⇒ Object private
- #infer_relation(name) ⇒ Object private
-
#structs ⇒ Structs
Return in-memory struct builder.
Instance Attribute Details
#registry ⇒ Hash<Symbol=>Builder> (readonly)
Returns a map with defined db-backed builders.
65 |
# File 'lib/rom/factory/factories.rb', line 65 option :registry, default: proc { Registry.new } |
#rom ⇒ ROM::Container (readonly)
Returns configured rom container.
57 |
# File 'lib/rom/factory/factories.rb', line 57 param :rom |
Instance Method Details
#[](name, *traits, **attrs) ⇒ ROM::Struct
Create and persist a new struct
170 171 172 |
# File 'lib/rom/factory/factories.rb', line 170 def [](name, *traits, **attrs) registry[name].struct_namespace(struct_namespace).persistable.create(*traits, **attrs) end |
#builder_struct_namespace(ns) ⇒ 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.
205 206 207 |
# File 'lib/rom/factory/factories.rb', line 205 def builder_struct_namespace(ns) ns ? {namespace: ns, overridable: false} : {namespace: struct_namespace, overridable: true} end |
#define(spec, opts = EMPTY_HASH, &block) ⇒ ROM::Factory::Builder
Define a new builder
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/rom/factory/factories.rb', line 129 def define(spec, opts = EMPTY_HASH, &block) name, parent = spec.is_a?(Hash) ? spec.flatten(1) : spec namespace = opts[:struct_namespace] relation_name = opts.fetch(:relation) { infer_relation(name) } if registry.key?(name) raise ArgumentError, "#{name.inspect} factory has been already defined" end builder = if parent extend_builder(name, registry[parent], relation_name, namespace, &block) else relation = rom.relations[relation_name] DSL.new( name, relation: relation, factories: self, struct_namespace: builder_struct_namespace(namespace), &block ).call end registry[name] = builder end |
#extend_builder(name, parent, relation_name, ns, &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.
225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/rom/factory/factories.rb', line 225 def extend_builder(name, parent, relation_name, ns, &block) namespace = parent.[:struct_namespace] namespace = builder_struct_namespace(ns) if ns relation = rom.relations.fetch(relation_name) { parent.relation } DSL.new( name, attributes: parent.attributes, relation: relation, factories: self, struct_namespace: namespace, &block ).call end |
#for_relation(relation) ⇒ 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.
210 211 212 |
# File 'lib/rom/factory/factories.rb', line 210 def for_relation(relation) registry[infer_factory_name(relation.name.to_sym)] end |
#infer_factory_name(name) ⇒ 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.
215 216 217 |
# File 'lib/rom/factory/factories.rb', line 215 def infer_factory_name(name) ::Dry::Core::Inflector.singularize(name).to_sym end |
#infer_relation(name) ⇒ 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.
220 221 222 |
# File 'lib/rom/factory/factories.rb', line 220 def infer_relation(name) ::Dry::Core::Inflector.pluralize(name).to_sym end |