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.
-
#define(spec, **opts, &block) ⇒ ROM::Factory::Builder
Define a new builder.
-
#structs ⇒ Structs
Return in-memory struct builder.
Instance Attribute Details
#registry ⇒ Hash<Symbol=>Builder> (readonly)
Returns a map with defined db-backed builders.
68 |
# File 'lib/rom/factory/factories.rb', line 68 option :registry, default: proc { Registry.new } |
#rom ⇒ ROM::Container (readonly)
Returns configured rom container.
60 |
# File 'lib/rom/factory/factories.rb', line 60 param :rom |
Instance Method Details
#[](name, *traits, **attrs) ⇒ ROM::Struct
Create and persist a new struct
165 166 167 |
# File 'lib/rom/factory/factories.rb', line 165 def [](name, *traits, **attrs) registry[name].persistable(struct_namespace).create(*traits, attrs) end |
#define(spec, **opts, &block) ⇒ ROM::Factory::Builder
Define a new builder
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/rom/factory/factories.rb', line 132 def define(spec, **opts, &block) name, parent = spec.is_a?(Hash) ? spec.flatten(1) : spec if registry.key?(name) raise ArgumentError, "#{name.inspect} factory has been already defined" end builder = if parent extend_builder(name, registry[parent], &block) else relation_name = opts.fetch(:relation) { infer_relation(name) } relation = rom.relations[relation_name] DSL.new(name, relation: relation.struct_namespace(struct_namespace), factories: self, &block).call end registry[name] = builder end |