Class: Gamifier::Model

Inherits:
OpenStruct
  • Object
show all
Extended by:
ClassMethods
Defined in:
lib/gamifier/model.rb

Defined Under Namespace

Modules: ClassMethods, FinderMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

container, mutable_attributes, reset!, special_attributes, store_or_get

Instance Attribute Details

#engineObject

The engine to use when creating or updating records. Default to the global Gamifier engine if not set via #engine= method.



10
11
12
# File 'lib/gamifier/model.rb', line 10

def engine
  @engine ||= Gamifier.engine
end

Instance Method Details

#_idObject



66
67
68
# File 'lib/gamifier/model.rb', line 66

def _id
  super || attributes[:id]
end

#attributesObject



14
# File 'lib/gamifier/model.rb', line 14

def attributes; @table.dup; end

#destroyObject



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.

Returns:

  • (Boolean)


85
86
87
# File 'lib/gamifier/model.rb', line 85

def new?
  self._id.nil?
end

#pathObject



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(options = {})
  if new?
    options.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.

Raises:



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