Class: MR::FakeRecord::Reflection
- Inherits:
-
Object
- Object
- MR::FakeRecord::Reflection
- Defined in:
- lib/mr/fake_record/associations.rb
Constant Summary collapse
- BELONGS_TO_ASSOC_PROC =
proc do |r| r.[: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
-
#association_class ⇒ Object
readonly
Returns the value of attribute association_class.
-
#foreign_key ⇒ Object
readonly
Returns the value of attribute foreign_key.
-
#foreign_type ⇒ Object
readonly
Returns the value of attribute foreign_type.
-
#macro ⇒ Object
readonly
ActiveRecord methods.
-
#name ⇒ Object
readonly
ActiveRecord methods.
-
#options ⇒ Object
readonly
ActiveRecord methods.
-
#reader_method_name ⇒ Object
readonly
Returns the value of attribute reader_method_name.
-
#writer_method_name ⇒ Object
readonly
Returns the value of attribute writer_method_name.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #define_accessor_on(fake_record_class) ⇒ Object
-
#initialize(macro, name, options = nil) ⇒ Reflection
constructor
A new instance of Reflection.
-
#klass ⇒ Object
ActiveRecord method.
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, = nil) @macro = macro.to_sym @name = name = || {} @class_name = [:class_name] @foreign_key = [:foreign_key] @foreign_type = [: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_class ⇒ Object (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_key ⇒ Object (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_type ⇒ Object (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 |
#macro ⇒ Object (readonly)
ActiveRecord methods
123 124 125 |
# File 'lib/mr/fake_record/associations.rb', line 123 def macro @macro end |
#name ⇒ Object (readonly)
ActiveRecord methods
123 124 125 |
# File 'lib/mr/fake_record/associations.rb', line 123 def name @name end |
#options ⇒ Object (readonly)
ActiveRecord methods
123 124 125 |
# File 'lib/mr/fake_record/associations.rb', line 123 def end |
#reader_method_name ⇒ Object (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_name ⇒ Object (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.[:polymorphic], self.name ].map(&:to_s) <=> [ other.macro, other.[: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 |
#klass ⇒ Object
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 |