Class: MR::FakeRecord::Reflection

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

Constant Summary collapse

BELONGS_TO_ASSOC_PROC =
proc do |r|
  r.options[:polymorphic] ? PolymorphicBelongsToAssociation : BelongsToAssociation
end
ASSOCIATION_CLASS =
{
  :belongs_to => BELONGS_TO_ASSOC_PROC,
  :has_one    => proc{ HasOneAssociation },
  :has_many   => proc{ HasManyAssociation }
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(macro, name, options = nil) ⇒ Reflection

Returns a new instance of Reflection.



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/mr/fake_record/associations.rb', line 136

def initialize(macro, name, options = nil)
  @macro   = macro.to_sym
  @name    = name
  @options = options || {}

  @class_name   = @options[:class_name]
  @foreign_key  = @options[:foreign_key]
  @foreign_type = @options[:foreign_type]

  @reader_method_name = name.to_s
  @writer_method_name = "#{@reader_method_name}="
  @association_class  = ASSOCIATION_CLASS[@macro].call(self)
end

Instance Attribute Details

#association_classObject (readonly)

Returns the value of attribute association_class.



125
126
127
# File 'lib/mr/fake_record/associations.rb', line 125

def association_class
  @association_class
end

#foreign_keyObject (readonly)

Returns the value of attribute foreign_key.



124
125
126
# File 'lib/mr/fake_record/associations.rb', line 124

def foreign_key
  @foreign_key
end

#foreign_typeObject (readonly)

Returns the value of attribute foreign_type.



124
125
126
# File 'lib/mr/fake_record/associations.rb', line 124

def foreign_type
  @foreign_type
end

#macroObject (readonly)

ActiveRecord methods



123
124
125
# File 'lib/mr/fake_record/associations.rb', line 123

def macro
  @macro
end

#nameObject (readonly)

ActiveRecord methods



123
124
125
# File 'lib/mr/fake_record/associations.rb', line 123

def name
  @name
end

#optionsObject (readonly)

ActiveRecord methods



123
124
125
# File 'lib/mr/fake_record/associations.rb', line 123

def options
  @options
end

#reader_method_nameObject (readonly)

Returns the value of attribute reader_method_name.



120
121
122
# File 'lib/mr/fake_record/associations.rb', line 120

def reader_method_name
  @reader_method_name
end

#writer_method_nameObject (readonly)

Returns the value of attribute writer_method_name.



120
121
122
# File 'lib/mr/fake_record/associations.rb', line 120

def writer_method_name
  @writer_method_name
end

Instance Method Details

#<=>(other) ⇒ Object



169
170
171
172
173
174
175
176
# File 'lib/mr/fake_record/associations.rb', line 169

def <=>(other)
  if other.kind_of?(self.class)
    [ self.macro,  self.options[:polymorphic],  self.name ].map(&:to_s) <=>
    [ other.macro, other.options[:polymorphic], other.name ].map(&:to_s)
  else
    super
  end
end

#define_accessor_on(fake_record_class) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/mr/fake_record/associations.rb', line 155

def define_accessor_on(fake_record_class)
  reflection = self
  fake_record_class.class_eval do

    define_method(reflection.reader_method_name) do
      self.association(reflection.name).read
    end
    define_method(reflection.writer_method_name) do |value|
      self.association(reflection.name).write(value)
    end

  end
end

#klassObject

ActiveRecord method



151
152
153
# File 'lib/mr/fake_record/associations.rb', line 151

def klass
  @klass ||= (@class_name.to_s.constantize if @class_name)
end