Class: ROM::Factory::Factories

Inherits:
Object
  • Object
show all
Extended by:
Dry::Configurable
Defined in:
lib/rom/factory/factories.rb

Overview

A registry with all configured factories

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.registryObject (readonly)



50
51
52
# File 'lib/rom/factory/factories.rb', line 50

def registry
  @registry
end

.structsObject (readonly)



54
55
56
# File 'lib/rom/factory/factories.rb', line 54

def structs
  @structs
end

Class Method Details

.[](name, attrs = {}) ⇒ ROM::Struct

Create and persist a new struct

Examples:

create a struct with default attributes

MyFactory[:user]

create a struct with some attributes overridden

MyFactory[:user, name: "Jane"]

Parameters:

  • name (Symbol)

    The name of the registered factory

  • attrs (Hash) (defaults to: {})

    An optional hash with attributes

Returns:

  • (ROM::Struct)


159
160
161
# File 'lib/rom/factory/factories.rb', line 159

def [](name, attrs = {})
  registry[name].persistable.create(attrs)
end

.define(spec, **opts, &block) ⇒ ROM::Factory::Builder

Define a new builder

Examples:

a simple builder

MyFactory.define(:user) do |f|
  f.name "Jane"
  f.email "[email protected]"
end

a builder using auto-generated fake values

MyFactory.define(:user) do |f|
  f.name { fake(:name) }
  f.email { fake(:internet, :email) }
end

a builder using sequenced values

MyFactory.define(:user) do |f|
  f.sequence(:name) { |n| "user-#{n}" }
end

a builder using values from other attribute(s)

MyFactory.define(:user) do |f|
  f.name "Jane"
  f.email { |name| "#{name.downcase}@rom-rb.org" }
end

a builder with "belongs-to" association

MyFactory.define(:group) do |f|
  f.name "Admins"
end

MyFactory.define(:user) do |f|
  f.name "Jane"
  f.association(:group)
end

a builder with "has-many" association

MyFactory.define(:group) do |f|
  f.name "Admins"
  f.association(:users, count: 2)
end

MyFactory.define(:user) do |f|
  f.sequence(:name) { |n| "user-#{n}" }
end

a builder which extends another builder

MyFactory.define(:user) do |f|
  f.name "Jane"
  f.admin false
end

MyFactory.define(admin: :user) do |f|
  f.admin true
end

Parameters:

  • spec (Symbol, Hash<Symbol=>Symbol>)

    Builder identifier, can point to a parent builder too

  • opts (Hash)

    Additional options

Options Hash (**opts):

  • relation (Symbol)

    An optional relation name (defaults to pluralized builder name)

Returns:



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rom/factory/factories.rb', line 126

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 = config.rom.relations[relation_name]
      DSL.new(name, relation: relation, factories: self, &block).call
    end

  registry[name] = builder
end

.extend_builder(name, parent, &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.



179
180
181
# File 'lib/rom/factory/factories.rb', line 179

def extend_builder(name, parent, &block)
  DSL.new(name, attributes: parent.attributes, relation: parent.relation, factories: self, &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.



164
165
166
# File 'lib/rom/factory/factories.rb', line 164

def for_relation(relation)
  registry.fetch(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.



169
170
171
# File 'lib/rom/factory/factories.rb', line 169

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.



174
175
176
# File 'lib/rom/factory/factories.rb', line 174

def infer_relation(name)
  ::Dry::Core::Inflector.pluralize(name).to_sym
end

.inherited(klass) ⇒ 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.



57
58
59
60
61
62
# File 'lib/rom/factory/factories.rb', line 57

def inherited(klass)
  registry = {}
  klass.instance_variable_set(:'@registry', registry)
  klass.instance_variable_set(:'@structs', Structs.new(registry))
  super
end