class Gamifier::Model
Attributes
engine[W]
Public Instance Methods
_id()
click to toggle source
Calls superclass method
# File lib/gamifier/model.rb, line 66 def _id super || attributes[:id] end
attributes()
click to toggle source
# File lib/gamifier/model.rb, line 14 def attributes; @table.dup; end
destroy()
click to toggle source
# File lib/gamifier/model.rb, line 70 def destroy return true if new? engine.transmit :delete, path end
encode(key, value)
click to toggle source
Overwrite in descendants to specifically encode a key/value pair.
# File lib/gamifier/model.rb, line 62 def encode(key, value) return nil end
engine()
click to toggle source
The engine to use when creating or updating records. Default to the global Gamifier
engine if not set via #engine=
method.
# File lib/gamifier/model.rb, line 10 def engine @engine ||= Gamifier.engine end
new?()
click to toggle source
Returns true if the current object does not exist on the server.
# File lib/gamifier/model.rb, line 85 def new? self._id.nil? end
path()
click to toggle source
# File lib/gamifier/model.rb, line 89 def path [self.class.path, self._id || "undefined"].join("/") end
payload_for_submission(opts = {})
click to toggle source
# File lib/gamifier/model.rb, line 30 def payload_for_submission(opts = {}) attr_to_keep = opts.empty? ? attributes : opts attr_to_keep = attr_to_keep.reject{|k,v| self.class.special_attributes.include?(k.to_sym) || ( !self.class.mutable_attributes.empty? && !self.class.mutable_attributes.include?(k.to_sym) ) } # Nested hashes are dumped as JSON payload. attr_to_keep.each do |k,v| attr_to_keep[k] = encode(k.to_sym,v) || case v when Hash MultiJson.dump(v) # Lazyloaded attributes when Proc v.call else v.to_s end end h = { self.class.container => attr_to_keep } self.class.special_attributes.each do |key| h[key] = send(key) end h end
replace_if_successful(response)
click to toggle source
# File lib/gamifier/model.rb, line 75 def replace_if_successful(response) if response.kind_of?(Hash) response.each{|k,v| send("#{k}=".to_sym, v)} self else response end end
save(options = {})
click to toggle source
# File lib/gamifier/model.rb, line 16 def save(options = {}) if new? options.has_key?(:async) ? async_create : create else update end end
update_attributes(opts = {})
click to toggle source
Allow to update only the specific attributes given in the opts
Hash.
# File lib/gamifier/model.rb, line 25 def update_attributes(opts = {}) raise Error, "Can't update an object if it has not been saved yet" if new? update(opts) end
Protected Instance Methods
async_create()
click to toggle source
# File lib/gamifier/model.rb, line 214 def async_create res = engine.transmit :post, 'activities/async_create', :body => payload_for_submission replace_if_successful(res) end
create()
click to toggle source
# File lib/gamifier/model.rb, line 209 def create res = engine.transmit :post, self.class.path, :body => payload_for_submission replace_if_successful(res) end
update(opts = {})
click to toggle source
# File lib/gamifier/model.rb, line 219 def update(opts = {}) res = engine.transmit :put, path, :body => payload_for_submission(opts) replace_if_successful(res) end