Class: ActiveRecordMock

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record_mock.rb

Overview

ActiveRecordMock

Author

Joel Parker Henderson, [email protected]

Copyright

Copyright © 2006-2008 Joel Parker Henderson

License

CreativeCommons License, Non-commercial Share Alike

License

LGPL, GNU Lesser General Public License

A simple mock object that provides the ActiveRecord method signatures read_attribute(key) and write_attribute(key,val), and simple record finder signatures find(id) and find(:all).

Example:

mock = ActiveRecordMock.new
mock.write_attribute('foo','bar')
mock.read_attribute('foo') => 'bar'

Example of initialize with attributes:

mock = ActiveRecordMock.new(:foo => 'bar', :goo => 'car', :hoo => 'dar')
mock.read_attribute(:foo') => 'bar'
mock.read_attribute(:goo') => 'car'
mock.read_attribute(:hoo') => 'dar'

Example of creating mock users:

anne = ActiveRecordMock.new(:id => 123, :name => 'Anne')
beth = ActiveRecordMock.new(:id => 456, :name => 'Beth')
cate = ActiveRecordMock.new(:id => 789, :name => 'Cate')

Example of mock finder creation:

ActiveRecordMock.find=[anne,beth,cate]

Example of mock finder retrieval of records by id:

ActiveRecordMock.find(123) => anne
ActiveRecordMock.find(456) => beth
ActiveRecordMock.find(789) => cate

Example of mock finder retrieval of all records:

ActiveRecordMock.find(:all) => [anne,beth,cate]

Constant Summary collapse

@@find =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ops = {}) ⇒ ActiveRecordMock

Returns a new instance of ActiveRecordMock.



45
46
47
48
# File 'lib/active_record_mock.rb', line 45

def initialize(ops={})
 @attributes=ops
 @@find << self
end

Class Method Details

.find(id, *ops) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/active_record_mock.rb', line 58

def self.find(id,*ops)
 case id
 when :all 
  @@find
 else
  @@find.each{|x| if x.read_attribute(:id)==id then return x end } or nil
 end
end

.find=(records) ⇒ Object



67
68
69
# File 'lib/active_record_mock.rb', line 67

def self.find=(records)
 @@find=records
end

Instance Method Details

#read_attribute(k) ⇒ Object



50
51
52
# File 'lib/active_record_mock.rb', line 50

def read_attribute(k)
 @attributes[k]
end

#write_attribute(k, v) ⇒ Object



54
55
56
# File 'lib/active_record_mock.rb', line 54

def write_attribute(k,v)
 @attributes[k]=v
end