Module: Top4R::ModelMixin::InstanceMethods

Defined in:
lib/top4r/model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#clientObject

Returns the value of attribute client.



36
37
38
# File 'lib/top4r/model.rb', line 36

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)



89
90
91
# File 'lib/top4r/model.rb', line 89

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.

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/top4r/model.rb', line 48

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_hashObject

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.



73
74
75
76
77
78
79
80
81
82
# File 'lib/top4r/model.rb', line 73

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_iObject

Returns integer representation of model object instance.

For example, product = Top4R::Product.new(:id => 234343) product.to_i #=> 234343



61
62
63
# File 'lib/top4r/model.rb', line 61

def to_i
  @id
end