Module: Squirm::Model::InstanceMethods

Defined in:
lib/squirm/model.rb

Instance Method Summary collapse

Instance Method Details

#initialize(options = {}) ⇒ Object



98
99
100
# File 'lib/squirm/model.rb', line 98

def initialize(options = {})
  @__attributes = options
end

#persisted?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/squirm/model.rb', line 120

def persisted?
  !! self.id
end

#reloadObject



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

#saveObject



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_hashObject



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