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:, struct_namespace:) {|_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:



36
37
38
39
40
41
42
43
44
45
# 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

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.



150
151
152
153
154
155
156
# 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

Instance Attribute Details

#_attributesObject (readonly)



32
33
34
# File 'lib/rom/factory/dsl.rb', line 32

def _attributes
  @_attributes
end

#_factoriesObject (readonly)



32
33
34
# File 'lib/rom/factory/dsl.rb', line 32

def _factories
  @_factories
end

#_nameObject (readonly)



32
33
34
# File 'lib/rom/factory/dsl.rb', line 32

def _name
  @_name
end

#_relationObject (readonly)



32
33
34
# File 'lib/rom/factory/dsl.rb', line 32

def _relation
  @_relation
end

#_struct_namespaceObject (readonly)



32
33
34
# File 'lib/rom/factory/dsl.rb', line 32

def _struct_namespace
  @_struct_namespace
end

#_traitsObject (readonly)



33
34
35
# File 'lib/rom/factory/dsl.rb', line 33

def _traits
  @_traits
end

#_valid_namesObject (readonly)



32
33
34
# File 'lib/rom/factory/dsl.rb', line 32

def _valid_names
  @_valid_names
end

Instance Method Details

#association(name, *traits, **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)

    Additional options

Options Hash (**options):

  • count (Integer)

    Number of objects to generate



135
136
137
138
139
140
141
142
143
144
145
# 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

#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.



48
49
50
# 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) ⇒ Object

Delegate to a builder and persist a struct

Parameters:

  • The (Symbol)

    name of the registered builder



57
58
59
# File 'lib/rom/factory/dsl.rb', line 57

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:



104
105
106
# File 'lib/rom/factory/dsl.rb', line 104

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



66
67
68
# File 'lib/rom/factory/dsl.rb', line 66

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

#timestampsObject

Set timestamp attributes



73
74
75
76
# File 'lib/rom/factory/dsl.rb', line 73

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

#trait(name, parents = [], &block) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
# 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