Class: MR::FakeRecord::ReflectionSet
- Inherits:
-
Object
- Object
- MR::FakeRecord::ReflectionSet
- Defined in:
- lib/mr/fake_record/associations.rb
Instance Method Summary collapse
- #add_belongs_to(fake_record_class, name, options = nil) ⇒ Object
- #add_has_many(fake_record_class, name, options = nil) ⇒ Object
- #add_has_one(fake_record_class, name, options = nil) ⇒ Object
- #all(type = nil) ⇒ Object
- #belongs_to ⇒ Object
- #find(name) ⇒ Object
- #has_many ⇒ Object
- #has_one ⇒ Object
-
#initialize ⇒ ReflectionSet
constructor
A new instance of ReflectionSet.
Constructor Details
#initialize ⇒ ReflectionSet
Returns a new instance of ReflectionSet.
64 65 66 67 68 |
# File 'lib/mr/fake_record/associations.rb', line 64 def initialize @belongs_to = {} @has_one = {} @has_many = {} end |
Instance Method Details
#add_belongs_to(fake_record_class, name, options = nil) ⇒ Object
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/mr/fake_record/associations.rb', line 95 def add_belongs_to(fake_record_class, name, = nil) ||= {} [:foreign_key] ||= "#{name}_id" if [:polymorphic] [:foreign_type] ||= "#{name}_type" end reflection = Reflection.new(:belongs_to, name, ) reflection.define_accessor_on(fake_record_class) @belongs_to[name.to_s] = reflection end |
#add_has_many(fake_record_class, name, options = nil) ⇒ Object
112 113 114 115 116 |
# File 'lib/mr/fake_record/associations.rb', line 112 def add_has_many(fake_record_class, name, = nil) reflection = Reflection.new(:has_many, name, ) reflection.define_accessor_on(fake_record_class) @has_many[name.to_s] = reflection end |
#add_has_one(fake_record_class, name, options = nil) ⇒ Object
106 107 108 109 110 |
# File 'lib/mr/fake_record/associations.rb', line 106 def add_has_one(fake_record_class, name, = nil) reflection = Reflection.new(:has_one, name, ) reflection.define_accessor_on(fake_record_class) @has_one[name.to_s] = reflection end |
#all(type = nil) ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/mr/fake_record/associations.rb', line 86 def all(type = nil) case type when :belongs_to, :has_one, :has_many self.send(type) else self.belongs_to + self.has_one + self.has_many end end |
#belongs_to ⇒ Object
70 71 72 |
# File 'lib/mr/fake_record/associations.rb', line 70 def belongs_to @belongs_to.values.sort end |
#find(name) ⇒ Object
82 83 84 |
# File 'lib/mr/fake_record/associations.rb', line 82 def find(name) @belongs_to[name.to_s] || @has_one[name.to_s] || @has_many[name.to_s] end |
#has_many ⇒ Object
78 79 80 |
# File 'lib/mr/fake_record/associations.rb', line 78 def has_many @has_many.values.sort end |
#has_one ⇒ Object
74 75 76 |
# File 'lib/mr/fake_record/associations.rb', line 74 def has_one @has_one.values.sort end |