module SP::Duh::JSONAPI::Model::Concerns::Persistence

Attributes

_data[RW]

Public Instance Methods

create!(exp_accounting_schema = nil, exp_accounting_prefix = nil) click to toggle source
# File lib/sp/duh/jsonapi/model/concerns/persistence.rb, line 164
def create!(exp_accounting_schema = nil, exp_accounting_prefix = nil)
  if self.class.autogenerated_id
    params = {
      data: {
        type: self.class.resource_name,
        attributes: get_persistent_json.reject { |k,v| k == :id || v.nil? }
      }
    }
  else
    params = {
      data: {
        type: self.class.resource_name,
        attributes: get_persistent_json.reject { |k,v| v.nil? }
      }
    }
  end
  result = if !exp_accounting_schema.blank? || !exp_accounting_prefix.blank?
    self.class.adapter.post_explicit!(exp_accounting_schema, exp_accounting_prefix, self.class.resource_name, params)
  else
    self.class.adapter.post(self.class.resource_name, params)
  end
  # Set the id to the newly created id
  self.id = result[:data][:id]
end
delete!()
Alias for: destroy!
destroy!() click to toggle source
# File lib/sp/duh/jsonapi/model/concerns/persistence.rb, line 150
def destroy!
  if !new_record?
    self.class.adapter.delete(path_for_id)
  end
end
Also aliased as: delete!
destroy_explicit!(exp_accounting_schema, exp_accounting_prefix) click to toggle source
# File lib/sp/duh/jsonapi/model/concerns/persistence.rb, line 156
def destroy_explicit!(exp_accounting_schema, exp_accounting_prefix)
  if !new_record?
    self.class.adapter.delete_explicit!(exp_accounting_schema, exp_accounting_prefix, path_for_id)
  end
end
get_persistent_json() click to toggle source
# File lib/sp/duh/jsonapi/model/concerns/persistence.rb, line 204
def get_persistent_json
  as_json.reject { |k| !k.in?(self.class.attributes) }
end
new_record?() click to toggle source

Instance methods

# File lib/sp/duh/jsonapi/model/concerns/persistence.rb, line 126
def new_record?
  if self.class.autogenerated_id || self.id.nil?
    self.id.nil?
  else
    self.class.find(self.id).nil?
  end
end
save!() click to toggle source
# File lib/sp/duh/jsonapi/model/concerns/persistence.rb, line 134
def save!
  if new_record?
    create!
  else
    update!
  end
end
save_explicit!(exp_accounting_schema, exp_accounting_prefix) click to toggle source
# File lib/sp/duh/jsonapi/model/concerns/persistence.rb, line 142
def save_explicit!(exp_accounting_schema, exp_accounting_prefix)
  if new_record?
    create!(exp_accounting_schema, exp_accounting_prefix)
  else
    update!(exp_accounting_schema, exp_accounting_prefix)
  end
end
update!(exp_accounting_schema = nil, exp_accounting_prefix = nil) click to toggle source
# File lib/sp/duh/jsonapi/model/concerns/persistence.rb, line 189
def update!(exp_accounting_schema = nil, exp_accounting_prefix = nil)
  params = {
    data: {
      type: self.class.resource_name,
      id: self.id.to_s,
      attributes: get_persistent_json.reject { |k,v| k == :id }
    }
  }
  result = if !exp_accounting_schema.blank? || !exp_accounting_prefix.blank?
    self.class.adapter.patch_explicit!(exp_accounting_schema, exp_accounting_prefix, path_for_id, params)
  else
    self.class.adapter.patch(path_for_id, params)
  end
end

Private Instance Methods

path_for_id() click to toggle source
# File lib/sp/duh/jsonapi/model/concerns/persistence.rb, line 214
def path_for_id ; "#{self.class.resource_name}/#{self.id.to_s}" ; end