Class: ROM::Factory::DSL

Inherits:
BasicObject
Defined in:
lib/rom/factory/dsl.rb

Overview

Factory builder DSL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, attributes: AttributeRegistry.new, relation:, factories:) {|_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.

Yields:

  • (_self)

Yield Parameters:



31
32
33
34
35
36
37
38
# File 'lib/rom/factory/dsl.rb', line 31

def initialize(name, attributes: AttributeRegistry.new, relation:, factories:)
  @_name = name
  @_relation = relation
  @_factories = factories
  @_attributes = attributes.dup
  @_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, *args, &block) ⇒ 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.



126
127
128
129
130
131
132
# File 'lib/rom/factory/dsl.rb', line 126

def method_missing(meth, *args, &block)
  if _valid_names.include?(meth)
    define_attr(meth, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#_attributesObject (readonly)



28
29
30
# File 'lib/rom/factory/dsl.rb', line 28

def _attributes
  @_attributes
end

#_factoriesObject (readonly)



28
29
30
# File 'lib/rom/factory/dsl.rb', line 28

def _factories
  @_factories
end

#_nameObject (readonly)



28
29
30
# File 'lib/rom/factory/dsl.rb', line 28

def _name
  @_name
end

#_relationObject (readonly)



28
29
30
# File 'lib/rom/factory/dsl.rb', line 28

def _relation
  @_relation
end

#_valid_namesObject (readonly)



28
29
30
# File 'lib/rom/factory/dsl.rb', line 28

def _valid_names
  @_valid_names
end

Instance Method Details

#association(name, options = {}) ⇒ Object

Create an association attribute

Examples:

belongs-to

f.association(:group)

has-many

f.association(:posts, count: 2)

Parameters:

  • name (Symbol)

    The name of the configured association

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

    Additional options

Options Hash (options):

  • count (Integer)

    Number of objects to generate (has-many only)



116
117
118
119
120
121
# File 'lib/rom/factory/dsl.rb', line 116

def association(name, options = {})
  assoc = _relation.associations[name]
  builder = -> { _factories.for_relation(assoc.target) }

  _attributes << attributes::Association.new(assoc, builder, options)
end

#callObject

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.



41
42
43
# File 'lib/rom/factory/dsl.rb', line 41

def call
  ::ROM::Factory::Builder.new(_attributes, _relation)
end

#create(name, *args) ⇒ Object

Delegate to a builder and persist a struct

Parameters:

  • The (Symbol)

    name of the registered builder



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

def create(name, *args)
  _factories[name, *args]
end

#fake(type) ⇒ Object #fake(api, type) ⇒ Object #fake(api, type, *args) ⇒ Object

Create a fake value using Faker gem

Overloads:

  • #fake(type) ⇒ Object

    Examples:

    f.email { fake(:name) }

    Parameters:

    • type (Symbol)

      The value type to generate

  • #fake(api, type) ⇒ Object

    Examples:

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

    Parameters:

    • api (Symbol)

      The faker API identifier ie. :internet, :product etc.

    • type (Symbol)

      The value type to generate

  • #fake(api, type, *args) ⇒ Object

    Examples:

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

    Parameters:

    • api (Symbol)

      The faker API identifier ie. :internet, :product etc.

    • type (Symbol)

      The value type to generate

    • args (Array)

      Additional arguments

See Also:



99
100
101
# File 'lib/rom/factory/dsl.rb', line 99

def fake(*args)
  ::ROM::Factory.fake(*args)
end

#sequence(meth, &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.

Create a sequence attribute

Parameters:

  • name (Symbol)

    The attribute name



59
60
61
62
63
# File 'lib/rom/factory/dsl.rb', line 59

def sequence(meth, &block)
  if _valid_names.include?(meth)
    define_sequence(meth, block)
  end
end

#timestampsObject

Set timestamp attributes



68
69
70
71
# File 'lib/rom/factory/dsl.rb', line 68

def timestamps
  created_at { ::Time.now }
  updated_at { ::Time.now }
end