Module: Top4R::ModelMixin::InstanceMethods
- Defined in:
- lib/top4r/model.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
Instance Method Summary collapse
-
#bless(client) ⇒ Object
“Blesses” model object.
-
#eql?(other) ⇒ Boolean
Equality method override of Object#eql? default.
-
#to_hash ⇒ Object
Returns hash representation of model object instance.
-
#to_i ⇒ Object
Returns integer representation of model object instance.
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
35 36 37 |
# File 'lib/top4r/model.rb', line 35 def client @client end |
Instance Method Details
#bless(client) ⇒ Object
“Blesses” model object.
Should be overridden by model class if special behavior is expected
Expected to return blessed object (usually self
)
88 89 90 |
# File 'lib/top4r/model.rb', line 88 def bless(client) self.basic_bless(client) end |
#eql?(other) ⇒ Boolean
Equality method override of Object#eql? default.
Relies on the class using this mixin to provide a attributes
class method that will return an Array of attributes to check are equivalent in this #eql? override.
It is by design that the #eql? method will raise a NoMethodError if no attributes
class method exists, to alert you that you must provide it for a meaningful result from this #eql? override. Otherwise this will return a meaningless result.
47 48 49 50 51 52 53 |
# File 'lib/top4r/model.rb', line 47 def eql?(other) attrs = self.class.attributes attrs.each do |att| return false unless self.send(att).eql?(other.send(att)) end true end |
#to_hash ⇒ Object
Returns hash representation of model object instance.
For example, u = Top4R::User.new(:id => 2342342, :screen_name => ‘tony_blair_is_the_devil’) u.to_hash #=> => 2342342, :screen_name => ‘tony_blair_is_the_devil’
This method also requires that the class method attributes
be defined to return an Array of attributes for the class.
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/top4r/model.rb', line 72 def to_hash attrs = self.class.attributes result = {} attrs.each do |att| value = self.send(att) value = value.to_hash if value.respond_to?(:to_hash) result[att] = value if value end result end |
#to_i ⇒ Object
Returns integer representation of model object instance.
For example, product = Top4R::Product.new(:id => 234343) product.to_i #=> 234343
60 61 62 |
# File 'lib/top4r/model.rb', line 60 def to_i @id end |