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

Attributes

autogenerated_id[RW]
resource_name[RW]

Define resource configuration accessors at the class (and subclass) level (static). These attribute values are NOT inherited by subclasses, each subclass MUST define their own. Instances can access these attributes at the class level only.

Public Instance Methods

all() click to toggle source
# File lib/sp/duh/jsonapi/model/concerns/persistence.rb, line 69
def all
  begin
    get_all("")
  rescue Exception => e
    nil
  end
end
all!() click to toggle source
# File lib/sp/duh/jsonapi/model/concerns/persistence.rb, line 67
def all! ; get_all("") ; end
find(id, conditions = nil) click to toggle source
# File lib/sp/duh/jsonapi/model/concerns/persistence.rb, line 34
def find(id, conditions = nil)
  begin
    get(id, conditions)
  rescue Exception => e
    return nil
  end
end
find!(id, conditions = nil) click to toggle source
# File lib/sp/duh/jsonapi/model/concerns/persistence.rb, line 28
def find!(id, conditions = nil) ; get(id, conditions) ; end
find_explicit!(exp_accounting_schema, exp_accounting_prefix, id, conditions = nil) click to toggle source
# File lib/sp/duh/jsonapi/model/concerns/persistence.rb, line 30
def find_explicit!(exp_accounting_schema, exp_accounting_prefix, id, conditions = nil)
  get_explicit(exp_accounting_schema, exp_accounting_prefix, id, conditions)
end
first(condition = "") click to toggle source
# File lib/sp/duh/jsonapi/model/concerns/persistence.rb, line 58
def first(condition = "")
  begin
    condition += (condition.blank? ? "" : "&") + "page[size]=1"
    get_all(condition).first
  rescue Exception => e
    nil
  end
end
first!(condition = "") click to toggle source
# File lib/sp/duh/jsonapi/model/concerns/persistence.rb, line 53
def first!(condition = "")
  condition += (condition.blank? ? "" : "&") + "page[size]=1"
  get_all(condition).first
end
query(condition) click to toggle source
# File lib/sp/duh/jsonapi/model/concerns/persistence.rb, line 45
def query(condition)
  begin
    get_all(condition)
  rescue Exception => e
    nil
  end
end
query!(condition) click to toggle source
# File lib/sp/duh/jsonapi/model/concerns/persistence.rb, line 42
def query!(condition) ; get_all(condition) ; end
query_explicit!(exp_accounting_schema, exp_accounting_prefix, condition) click to toggle source
# File lib/sp/duh/jsonapi/model/concerns/persistence.rb, line 43
def query_explicit!(exp_accounting_schema, exp_accounting_prefix, condition) ; get_all_explicit(exp_accounting_schema, exp_accounting_prefix, condition) ; end

Private Instance Methods

get(id, conditions = nil) click to toggle source
# File lib/sp/duh/jsonapi/model/concerns/persistence.rb, line 84
def get(id, conditions = nil)
  result = self.adapter.get("#{self.resource_name}/#{id.to_s}", conditions)
  jsonapi_result_to_instance(result[:data], result)
end
get_all(condition) click to toggle source
# File lib/sp/duh/jsonapi/model/concerns/persistence.rb, line 89
def get_all(condition)
  got = []
  result = self.adapter.get(self.resource_name, condition)
  if result
    got = result[:data].map do |item|
      data = { data: item }
      data.merge(included: result[:included]) if result[:included]
      jsonapi_result_to_instance(item, data)
    end
  end
  got
end
get_all_explicit(exp_accounting_schema, exp_accounting_prefix, condition) click to toggle source
# File lib/sp/duh/jsonapi/model/concerns/persistence.rb, line 102
def get_all_explicit(exp_accounting_schema, exp_accounting_prefix, condition)
  got = []
  result = self.adapter.get_explicit!(exp_accounting_schema, exp_accounting_prefix, self.resource_name, condition)
  if result
    got = result[:data].map do |item|
      data = { data: item }
      data.merge(included: result[:included]) if result[:included]
      jsonapi_result_to_instance(item, data)
    end
  end
  got
end
get_explicit(exp_accounting_schema, exp_accounting_prefix, id, conditions = nil) click to toggle source
# File lib/sp/duh/jsonapi/model/concerns/persistence.rb, line 79
def get_explicit(exp_accounting_schema, exp_accounting_prefix, id, conditions = nil)
  result = self.adapter.get_explicit!(exp_accounting_schema, exp_accounting_prefix, "#{self.resource_name}/#{id.to_s}", conditions)
  jsonapi_result_to_instance(result[:data], result)
end
jsonapi_result_to_instance(result, data) click to toggle source
# File lib/sp/duh/jsonapi/model/concerns/persistence.rb, line 115
def jsonapi_result_to_instance(result, data)
  if result
    instance = self.new(result.merge(result[:attributes]).except(:attributes))
    instance.send :_data=, data
  end
  instance
end