module HOALife::Resources::Persistable
Persist an object
Public Instance Methods
create(attrs = {})
click to toggle source
# File lib/hoalife/resources/persistable.rb, line 9 def create(attrs = {}) new(attrs).tap(&:save) end
destroy()
click to toggle source
# File lib/hoalife/resources/persistable.rb, line 40 def destroy make_request! do response = HOALife::Client::Delete.new(update_url) response.status == 202 end end
persisted?()
click to toggle source
# File lib/hoalife/resources/persistable.rb, line 26 def persisted? !id.nil? end
save()
click to toggle source
# File lib/hoalife/resources/persistable.rb, line 14 def save self.errors = nil if !persisted? create! else update! end errors.nil? end
update(attrs = {})
click to toggle source
# File lib/hoalife/resources/persistable.rb, line 30 def update(attrs = {}) return false unless persisted? attrs.each do |key, value| send("#{key}=", value) end save end
Private Instance Methods
assign_errors!(err)
click to toggle source
# File lib/hoalife/resources/persistable.rb, line 84 def assign_errors!(err) self.errors = OpenStruct.new(err.details.dig('data', 'attributes')) end
assign_updated_data!(data)
click to toggle source
# File lib/hoalife/resources/persistable.rb, line 78 def assign_updated_data!(data) @obj = cast_attrs(data.dig('data', 'attributes')) data.dig('data', 'attributes').each { |k, v| send("#{k}=", v) } end
create!()
click to toggle source
# File lib/hoalife/resources/persistable.rb, line 58 def create! make_request! do response = HOALife::Client::Post.new(create_url, to_json) assign_updated_data!(response.json) end rescue HOALife::BadRequestError => e assign_errors!(e) end
create_url()
click to toggle source
# File lib/hoalife/resources/persistable.rb, line 50 def create_url HOALife.api_base + self.class.base_path end
update!()
click to toggle source
# File lib/hoalife/resources/persistable.rb, line 68 def update! make_request! do response = HOALife::Client::Put.new(update_url, to_json) assign_updated_data!(response.json) end rescue HOALife::BadRequestError => e assign_errors!(e) end
update_url()
click to toggle source
# File lib/hoalife/resources/persistable.rb, line 54 def update_url HOALife.api_base + self.class.base_path + "/#{id}" end