lib/rom/factory/dsl.rb in rom-factory-0.9.1 vs lib/rom/factory/dsl.rb in rom-factory-0.10.0
- old
+ new
@@ -25,27 +25,28 @@
#
# @api public
class DSL < BasicObject
define_method(:rand, ::Kernel.instance_method(:rand))
- attr_reader :_name, :_relation, :_attributes, :_factories, :_valid_names
+ attr_reader :_name, :_relation, :_attributes, :_factories, :_struct_namespace, :_valid_names
attr_reader :_traits
# @api private
- def initialize(name, attributes: AttributeRegistry.new, relation:, factories:)
+ 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
# @api private
def call
- ::ROM::Factory::Builder.new(_attributes, _traits, relation: _relation)
+ ::ROM::Factory::Builder.new(_attributes, _traits, relation: _relation, struct_namespace: _struct_namespace)
end
# Delegate to a builder and persist a struct
#
# @param [Symbol] The name of the registered builder
@@ -108,10 +109,11 @@
attributes: _traits.values_at(*parents).flat_map(&:elements).inject(
AttributeRegistry.new, :<<
),
relation: _relation,
factories: _factories,
+ struct_namespace: _struct_namespace,
&block
)._attributes
end
# Create an association attribute
@@ -122,14 +124,19 @@
# @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 (has-many only)
+ # @option options [Integer] count Number of objects to generate
#
# @api public
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