Class: MR::Model::OneToOneAssociation

Inherits:
Association show all
Defined in:
lib/mr/model/associations.rb

Instance Attribute Summary

Attributes inherited from Association

#name, #reader_method_name, #writer_method_name

Instance Method Summary collapse

Methods inherited from Association

#define_accessor_on, #initialize

Constructor Details

This class inherits a constructor from MR::Model::Association

Instance Method Details

#read(record) ⇒ Object



119
120
121
122
123
# File 'lib/mr/model/associations.rb', line 119

def read(record)
  if associated_record = record.send(@association_reader_name)
    associated_record.model_class.new(associated_record)
  end
end

#write(value, model, record, &block) ⇒ Object



125
126
127
128
129
130
# File 'lib/mr/model/associations.rb', line 125

def write(value, model, record, &block)
  raise BadAssociationValueError.new(value) if value && !value.kind_of?(MR::Model)
  associated_record = model.instance_exec(value, &block) if value
  record.send(@association_writer_name, associated_record)
  value
end