# Pastebin bG9ABlAS class AssociationOperation attr_reader :model_mapper, :mapper_registry, :model, :attributes, :options def initialize( model_mapper, mapper_registry, model, attributes, **options) @model_mapper = model_mapper @mapper_registry = mapper_registry @model = model @attributes = attributes @options = options end def finish # NOOP end def mapper_for_class(klass) mapper_registry.fetch(klass) end end class AssociationStoreOperation < AssociationOperation def belongs_to(association_class, field, column, inverse_of: nil) mapper = mapper_for_class(association_class) object = model.public_send(field) if inverse_of object.public_send("#{inverse_of}=", model) end attributes[column] = object.id mapper.store(object) end def has_one(association_class, field, column) # NOOP end end class AssociationLoadOperation < AssociationOperation def belongs_to(association_class, field, column, inverse_of: nil) mapper = mapper_for_class(association_class) object_id = attributes.delete(column) object = mapper.find(object_id) model.public_send("#{field}=", object) end def has_one(association_class, field, column) mapper = mapper_for_class(association_class) if object = mapper.public_send("find_by_#{column}", column) model.public_send("#{field}=", object) end end end