Module: Ambry::Model::InstanceMethods

Defined in:
lib/ambry/model.rb

Instance Method Summary collapse

Instance Method Details

#<=>(instance) ⇒ Object

Ambry models implement the <=> method and mix in Comparable to provide sorting methods. This default implementation compares the result of #to_id. If the items being compared are not of the same kind.



124
125
126
# File 'lib/ambry/model.rb', line 124

def <=>(instance)
  to_id <=> instance.to_id if instance.kind_of? self.class
end

#deleteObject

Tell the mapper to delete the data for this instance.



164
165
166
# File 'lib/ambry/model.rb', line 164

def delete
  self.class.delete(self.to_id)
end

#id_changed?Boolean

Returns true is the model’s id field has been updated.

Returns:

  • (Boolean)


136
137
138
# File 'lib/ambry/model.rb', line 136

def id_changed?
  to_id != @attributes[self.class.id_method]
end

#initialize(attributes = nil) {|_self| ... } ⇒ Object

Ambry models can be instantiated with a hash of attribures, a block, or both. If both a hash and block are given, then the values set inside the block will take precedence over those set in the hash.

Examples:

Person.new :name => "Joe"
Person.new {|p| p.name = "Joe"}
Person.new(params[:person]) {|p| p.age = 38}

Yields:

  • (_self)

Yield Parameters:



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ambry/model.rb', line 109

def initialize(attributes = nil, &block)
  @attributes = {}.freeze
  return unless attributes || block_given?
  if attributes
    self.class.attribute_names.each do |name|
      value = attributes[name] || attributes[name.to_s]
      send("#{name}=", value) if value
    end
  end
  yield self if block_given?
end

#inspectObject



168
169
170
# File 'lib/ambry/model.rb', line 168

def inspect
  "#<#{self.class.name} #{self.class.attribute_names.map { |attr| "#{attr}: #{self.send(attr).inspect}" } * ', ' }>"
end

#saveObject

Tell the mapper to save the data for this model instance.



149
150
151
# File 'lib/ambry/model.rb', line 149

def save
  self.class.mapper.put(self)
end

#to_hashObject

Get a hash of the instance’s model attributes.



129
130
131
132
133
# File 'lib/ambry/model.rb', line 129

def to_hash
  self.class.attribute_names.inject({}) do |hash, key|
    hash[key] = self.send(key); hash
  end
end

#to_id(use_old = false) ⇒ Object

Invoke the model’s id method to return this instance’s unique key. If true is passed, then the id will be read from the attributes hash rather than from an instance variable. This allows you to retrieve the old id, in the event that the id has been changed.



144
145
146
# File 'lib/ambry/model.rb', line 144

def to_id(use_old = false)
  use_old ? @attributes[self.class.id_method] : send(self.class.id_method)
end

#update(attributes) ⇒ Object

Update this instance’s attributes and invoke #save.



154
155
156
157
158
159
160
161
# File 'lib/ambry/model.rb', line 154

def update(attributes)
  HashProxy.with(attributes) do |proxy|
    self.class.attribute_names.each do |name|
      send("#{name}=", proxy[name]) if proxy.key?(name)
    end
  end
  save
end