Class: ROM::Factory::Attributes::Association::OneToOne Private

Inherits:
OneToMany
  • Object
show all
Defined in:
lib/rom/factory/attributes/association.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from ROM::Factory::Attributes::Association::Core

Instance Method Details

#call(attrs = EMPTY_HASH, parent, persist: true) ⇒ 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.


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rom/factory/attributes/association.rb', line 104

def call(attrs = EMPTY_HASH, parent, persist: true)
  # do not associate if count is 0
  return { name => nil } if count.zero?

  return if attrs.key?(name)

  association_hash = assoc.associate(attrs, parent)

  struct = if persist
             builder.persistable.create(*traits, association_hash)
           else
             belongs_to_name = Dry::Core::Inflector.singularize(assoc.source_alias)
             belongs_to_associations = { belongs_to_name.to_sym => parent }
             final_attrs = attrs.merge(association_hash).merge(belongs_to_associations)
             builder.struct(*traits, final_attrs)
           end

  { name => struct }
end

#countObject

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.


125
126
127
# File 'lib/rom/factory/attributes/association.rb', line 125

def count
  options.fetch(:count, 1)
end