Class: MR::Model::Association

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

Direct Known Subclasses

OneToManyAssociation, OneToOneAssociation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Association

Returns a new instance of Association.



90
91
92
93
94
95
96
# File 'lib/mr/model/associations.rb', line 90

def initialize(name)
  @name = name.to_s
  @reader_method_name = @name
  @writer_method_name = "#{@name}="
  @association_reader_name = @name
  @association_writer_name = "#{@name}="
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



87
88
89
# File 'lib/mr/model/associations.rb', line 87

def name
  @name
end

#reader_method_nameObject (readonly)

Returns the value of attribute reader_method_name.



88
89
90
# File 'lib/mr/model/associations.rb', line 88

def reader_method_name
  @reader_method_name
end

#writer_method_nameObject (readonly)

Returns the value of attribute writer_method_name.



88
89
90
# File 'lib/mr/model/associations.rb', line 88

def writer_method_name
  @writer_method_name
end

Instance Method Details

#define_accessor_on(model_class) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/mr/model/associations.rb', line 98

def define_accessor_on(model_class)
  association = self
  model_class.class_eval do

    define_method(association.reader_method_name) do
      association.read(record)
    end

    define_method(association.writer_method_name) do |value|
      begin
        association.write(value, self, record){ |m| m.record }
      rescue BadAssociationValueError => exception
        raise ArgumentError, exception.message, caller
      end
    end

  end
end