class ROM::Factory::DSL

Factory builder DSL

@api public

Attributes

_attributes[R]
_factories[R]
_name[R]
_relation[R]
_struct_namespace[R]
_traits[R]
_valid_names[R]

Public Class Methods

new(name, attributes: AttributeRegistry.new, relation:, factories:, struct_namespace:) { |self| ... } click to toggle source

@api private

# File lib/rom/factory/dsl.rb, line 36
def initialize(name, attributes: AttributeRegistry.new, relation:, factories:, struct_namespace:)
  @_name = name
  @_relation = relation
  @_factories = factories
  @_struct_namespace = struct_namespace
  @_attributes = attributes.dup
  @_traits = {}
  @_valid_names = _relation.schema.attributes.map(&:name)
  yield(self)
end

Public Instance Methods

association(name, *traits, **options) click to toggle source

Create an association attribute

@example belongs-to

f.association(:group)

@example has-many

f.association(:posts, count: 2)

@param [Symbol] name The name of the configured association @param [Hash] options Additional options @option options [Integer] count Number of objects to generate

@api public

# File lib/rom/factory/dsl.rb, line 135
def association(name, *traits, **options)
  assoc = _relation.associations[name]

  if assoc.is_a?(::ROM::SQL::Associations::OneToOne) && options.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, **options)
end
call() click to toggle source

@api private

# File lib/rom/factory/dsl.rb, line 48
def call
  ::ROM::Factory::Builder.new(_attributes, _traits, relation: _relation, struct_namespace: _struct_namespace)
end
create(name, *args) click to toggle source

Delegate to a builder and persist a struct

@param [Symbol] The name of the registered builder

@api public

# File lib/rom/factory/dsl.rb, line 57
def create(name, *args)
  _factories[name, *args]
end
fake(*args) click to toggle source

Create a fake value using Faker gem

@overload fake(type)

@example
  f.email { fake(:name) }

@param [Symbol] type The value type to generate

@overload fake(api, type)

@example
  f.email { fake(:internet, :email) }

@param [Symbol] api The faker API identifier ie. :internet, :product etc.
@param [Symbol] type The value type to generate

@overload fake(api, type, *args)

@example
  f.email { fake(:number, :between, 10, 100) }

@param [Symbol] api The faker API identifier ie. :internet, :product etc.
@param [Symbol] type The value type to generate
@param [Array] args Additional arguments

@see github.com/stympy/faker/tree/master/doc

@api public

# File lib/rom/factory/dsl.rb, line 104
def fake(*args)
  ::ROM::Factory.fake(*args)
end
sequence(meth, &block) click to toggle source

Create a sequence attribute

@param [Symbol] name The attribute name

@api private

# File lib/rom/factory/dsl.rb, line 66
def sequence(meth, &block)
  define_sequence(meth, block) if _valid_names.include?(meth)
end
timestamps() click to toggle source

Set timestamp attributes

@api public

# File lib/rom/factory/dsl.rb, line 73
def timestamps
  created_at { ::Time.now }
  updated_at { ::Time.now }
end
trait(name, parents = [], &block) click to toggle source
# File lib/rom/factory/dsl.rb, line 109
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

Private Instance Methods

attributes() click to toggle source

@api private

# File lib/rom/factory/dsl.rb, line 178
def attributes
  ::ROM::Factory::Attributes
end
define_attr(name, *args, &block) click to toggle source

@api private

# File lib/rom/factory/dsl.rb, line 169
def define_attr(name, *args, &block)
  _attributes << if block
                   attributes::Callable.new(name, self, block)
                 else
                   attributes::Value.new(name, *args)
                 end
end
define_sequence(name, block) click to toggle source

@api private

# File lib/rom/factory/dsl.rb, line 164
def define_sequence(name, block)
  _attributes << attributes::Callable.new(name, self, attributes::Sequence.new(name, &block))
end
method_missing(meth, *args, &block) click to toggle source

@api private

Calls superclass method
# File lib/rom/factory/dsl.rb, line 150
def method_missing(meth, *args, &block)
  if _valid_names.include?(meth)
    define_attr(meth, *args, &block)
  else
    super
  end
end
respond_to_missing?(method_name, include_private = false) click to toggle source

@api private

Calls superclass method
# File lib/rom/factory/dsl.rb, line 159
def respond_to_missing?(method_name, include_private = false)
  _valid_names.include?(meth) || super
end