Class: MR::FakeRecord::ReflectionSet

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

Instance Method Summary collapse

Constructor Details

#initializeReflectionSet

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, options = nil)
  options ||= {}
  options[:foreign_key] ||= "#{name}_id"
  if options[:polymorphic]
    options[:foreign_type] ||= "#{name}_type"
  end
  reflection = Reflection.new(:belongs_to, name, options)
  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, options = nil)
  reflection = Reflection.new(:has_many, name, options)
  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, options = nil)
  reflection = Reflection.new(:has_one, name, options)
  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_toObject



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_manyObject



78
79
80
# File 'lib/mr/fake_record/associations.rb', line 78

def has_many
  @has_many.values.sort
end

#has_oneObject



74
75
76
# File 'lib/mr/fake_record/associations.rb', line 74

def has_one
  @has_one.values.sort
end