Module: SparkApi::Models::Concerns::Savable

Instance Method Summary collapse

Instance Method Details

#create!(arguments = {}) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/spark_api/models/concerns/savable.rb', line 23

def create!(arguments = {})
  results = connection.post self.path, post_data.merge(params_for_save), arguments
  update_attributes_after_create(results.first)
  reset_dirty
  params_for_save.clear
  true
end

#params_for_saveObject

extra params to be passed when saving



45
46
47
# File 'lib/spark_api/models/concerns/savable.rb', line 45

def params_for_save
  @params_for_save ||= {}
end

#post_dataObject

update/create hash (can be overridden)



50
51
52
# File 'lib/spark_api/models/concerns/savable.rb', line 50

def post_data
  { resource_pluralized => [ attributes ] }
end

#save(arguments = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/spark_api/models/concerns/savable.rb', line 7

def save(arguments = {})
  self.errors = [] # clear the errors hash
  begin
    return save!(arguments)
  rescue BadResourceRequest => e
    self.errors << {:code => e.code, :message => e.message}
    SparkApi.logger.warn("Failed to save resource #{self}: #{e.message}")
  rescue NotFound => e
    SparkApi.logger.error("Failed to save resource #{self}: #{e.message}")
  end
  false
end

#save!(arguments = {}) ⇒ Object



19
20
21
# File 'lib/spark_api/models/concerns/savable.rb', line 19

def save!(arguments = {})
  persisted? ? update!(arguments) : create!(arguments)
end

#update!(arguments = {}) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/spark_api/models/concerns/savable.rb', line 36

def update!(arguments = {})
  return true unless changed? && persisted?
  connection.put resource_uri, dirty_attributes, arguments
  reset_dirty
  params_for_save.clear
  true
end

#update_attributes(attrs = {}, options = {}) ⇒ Object



31
32
33
34
# File 'lib/spark_api/models/concerns/savable.rb', line 31

def update_attributes(attrs = {}, options = {})
  load(attrs, options)
  save!
end