Module: Squirm::Model::InstanceMethods
- Defined in:
- lib/squirm/model.rb
Instance Method Summary collapse
- #initialize(options = {}) ⇒ Object
- #persisted? ⇒ Boolean
- #reload ⇒ Object
- #save ⇒ Object
- #to_hash ⇒ Object
- #update(params) ⇒ Object
Instance Method Details
#initialize(options = {}) ⇒ Object
98 99 100 |
# File 'lib/squirm/model.rb', line 98 def initialize( = {}) @__attributes = end |
#persisted? ⇒ Boolean
120 121 122 |
# File 'lib/squirm/model.rb', line 120 def persisted? !! self.id end |
#reload ⇒ Object
124 125 126 127 128 129 130 131 |
# File 'lib/squirm/model.rb', line 124 def reload self.class.api.get.call(id) do |result| @__attributes = result.first @__attributes.keys.each do |key| remove_instance_variable :"@#{key}" end end end |
#save ⇒ Object
111 112 113 114 115 116 117 118 |
# File 'lib/squirm/model.rb', line 111 def save if persisted? update to_hash else @id = self.class.api.create[to_hash] reload end end |
#to_hash ⇒ Object
107 108 109 |
# File 'lib/squirm/model.rb', line 107 def to_hash Hash[self.class.sample.keys.map {|key| [key, send(key)]}] end |
#update(params) ⇒ Object
102 103 104 105 |
# File 'lib/squirm/model.rb', line 102 def update(params) procedure = self.class.api.update procedure.call to_hash.merge params end |