Class | PassiveRecord |
In: |
lib/passive_record.rb
|
Parent: | Object |
created_at | [RW] | Date accessors to be consistent with ActiveRecord: |
updated_at | [RW] | Date accessors to be consistent with ActiveRecord: |
Returns the list of available has_many associations
Model.associations #=> [:names, :addresses]
# File lib/passive_record.rb, line 59 59: def associations 60: @associations 61: end
Return the list of available attributes for the class
Name.attributes #=> [:id, :first_name, :last_name]
# File lib/passive_record.rb, line 52 52: def attributes 53: @attributes 54: end
Creates instance methods for each item in the list. Expects an array
class Address < PassiveRecord define_fields :street, :city, :state, :postal_code, :country end
# File lib/passive_record.rb, line 43 43: def define_fields(attrs) 44: @attributes = attrs 45: # Assign attr_accessor for each attribute in the list 46: attrs.each {|att| attr_accessor att} 47: end
Provide some basic ActiveRecord-like methods for working with non-ActiveRecord objects
class Person < PassiveRecord has_many :names, :addresses, :phone_numbers end
# File lib/passive_record.rb, line 33 33: def has_many(*associations) 34: # Simply sets up an attr_accessor for each item in the list 35: @associations = associations 36: associations.each {|association| attr_accessor association} 37: end
Assign values to each of the attributes passed in the params hash
# File lib/passive_record.rb, line 4 4: def initialize(params = {}) 5: params.each { |k, v| send("#{k}=", v)} 6: end
Compare this object with another object of the same class. Returns true if the attributes and values are identical.
@name === @name #=> true @name === @name2 #=> false
# File lib/passive_record.rb, line 23 23: def ===(other) 24: self.attributes == other.attributes 25: end
Return a hash of the class‘s attribute names and values
@name.attributes => {:first_name=>"Dima", :last_name=>"Dozen"}
# File lib/passive_record.rb, line 13 13: def attributes 14: @attributes = {} 15: self.class.attributes.flatten.each {|att| @attributes[att] = self.send(att) } 16: @attributes 17: end