Class: Gamifier::Model
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- Gamifier::Model
- Extended by:
- ClassMethods
- Defined in:
- lib/gamifier/model.rb
Direct Known Subclasses
Activity, ActivityDefinition, ApiKey, Group, Network, Player, Reward, RewardDefinition, Site, Track, Unit, User
Defined Under Namespace
Modules: ClassMethods, FinderMethods
Instance Attribute Summary collapse
-
#engine ⇒ Object
The engine to use when creating or updating records.
Instance Method Summary collapse
- #_id ⇒ Object
- #attributes ⇒ Object
- #destroy ⇒ Object
-
#encode(key, value) ⇒ Object
Overwrite in descendants to specifically encode a key/value pair.
-
#new? ⇒ Boolean
Returns true if the current object does not exist on the server.
- #path ⇒ Object
- #payload_for_submission(opts = {}) ⇒ Object
- #replace_if_successful(response) ⇒ Object
- #save(options = {}) ⇒ Object
-
#update_attributes(opts = {}) ⇒ Object
Allow to update only the specific attributes given in the
optsHash.
Methods included from ClassMethods
container, mutable_attributes, reset!, special_attributes, store_or_get
Instance Attribute Details
Instance Method Details
#_id ⇒ Object
66 67 68 |
# File 'lib/gamifier/model.rb', line 66 def _id super || attributes[:id] end |
#attributes ⇒ Object
14 |
# File 'lib/gamifier/model.rb', line 14 def attributes; @table.dup; end |
#destroy ⇒ Object
70 71 72 73 |
# File 'lib/gamifier/model.rb', line 70 def destroy return true if new? engine.transmit :delete, path end |
#encode(key, value) ⇒ Object
Overwrite in descendants to specifically encode a key/value pair.
62 63 64 |
# File 'lib/gamifier/model.rb', line 62 def encode(key, value) return nil end |
#new? ⇒ Boolean
Returns true if the current object does not exist on the server.
85 86 87 |
# File 'lib/gamifier/model.rb', line 85 def new? self._id.nil? end |
#path ⇒ Object
89 90 91 |
# File 'lib/gamifier/model.rb', line 89 def path [self.class.path, self._id || "undefined"].join("/") end |
#payload_for_submission(opts = {}) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# 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) ⇒ Object
75 76 77 78 79 80 81 82 |
# 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 = {}) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/gamifier/model.rb', line 16 def save( = {}) if new? .has_key?(:async) ? async_create : create else update end end |
#update_attributes(opts = {}) ⇒ Object
Allow to update only the specific attributes given in the opts Hash.
25 26 27 28 |
# 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 |