class Pinkman::Model::Base

Public Class Methods

get(query) click to toggle source

Get: Pinkman way of fetching records

# File lib/pinkman/model/base.rb, line 19
  def self.get query
    if (begin Integer(query) rescue false end)
      [find(query)]
    elsif query.is_a? Hash
      where(query)
    elsif query.is_a? Array
      where(id: query)
    elsif query.is_a? String
      search(query)
    else
      []
    end
  end
  
  def self.optimize(scope, options = {})
    if  (options.keys - [:includes, :select]).any?
      raise ArgumentError, "#{self}.optimize called with invalid options. Options hash accepts :includes and :select only."
    end
    options_includes = options[:includes] || []
    options_select = options[:select] || []
    
    scope_obj = serializer.scope(scope)
    including_array = scope_obj.including + scope_obj.associations_inclusion + options_includes
    selecting_array = scope_obj.selecting + options_select
    
    includes(including_array).select(selecting_array)
  end
  def self.optim(*args)
    optimize(*args)
  end
  
  def serialize_for scope_name, params_hash = {}
    options = {scope: scope_name}.merge(params: params_hash)
    self.class.serializer.new(self,options)
  end

  def json scope_name=:public, params_hash={}
    serialize_for(scope_name, params_hash).to_json
  end
  
  def json_for *args, &block
    ActiveSupport::Deprecation.warn('"json_for" deprecated. Use "json" instead.')
    json(*args, &block)
  end
  
  def has_json_key? key, scope=:public, options={}
    json_version(scope, options).has_key?(key.to_s) and json_version(scope, options)[key.to_s].present?
  end
  
  def json_version *args
    JSON.parse(json(*args))
  end
  alias json_hash json_version
  
  def json_key key, scope=:public, options={}
    json_version(scope, options)[key.to_s]
  end
  
  
end
optim(*args) click to toggle source
# File lib/pinkman/model/base.rb, line 46
def self.optim(*args)
  optimize(*args)
end
optimize(scope, options = {}) click to toggle source
# File lib/pinkman/model/base.rb, line 33
def self.optimize(scope, options = {})
  if  (options.keys - [:includes, :select]).any?
    raise ArgumentError, "#{self}.optimize called with invalid options. Options hash accepts :includes and :select only."
  end
  options_includes = options[:includes] || []
  options_select = options[:select] || []
  
  scope_obj = serializer.scope(scope)
  including_array = scope_obj.including + scope_obj.associations_inclusion + options_includes
  selecting_array = scope_obj.selecting + options_select
  
  includes(including_array).select(selecting_array)
end
serializer() click to toggle source
# File lib/pinkman/model/base.rb, line 14
    def self.serializer
      @serializer || (begin eval(self.to_s + 'Serializer') rescue nil end)
    end
    
    # Get: Pinkman way of fetching records
    def self.get query
      if (begin Integer(query) rescue false end)
        [find(query)]
      elsif query.is_a? Hash
        where(query)
      elsif query.is_a? Array
        where(id: query)
      elsif query.is_a? String
        search(query)
      else
        []
      end
    end
    
    def self.optimize(scope, options = {})
      if  (options.keys - [:includes, :select]).any?
        raise ArgumentError, "#{self}.optimize called with invalid options. Options hash accepts :includes and :select only."
      end
      options_includes = options[:includes] || []
      options_select = options[:select] || []
      
      scope_obj = serializer.scope(scope)
      including_array = scope_obj.including + scope_obj.associations_inclusion + options_includes
      selecting_array = scope_obj.selecting + options_select
      
      includes(including_array).select(selecting_array)
    end
    def self.optim(*args)
      optimize(*args)
    end
    
    def serialize_for scope_name, params_hash = {}
      options = {scope: scope_name}.merge(params: params_hash)
      self.class.serializer.new(self,options)
    end

    def json scope_name=:public, params_hash={}
      serialize_for(scope_name, params_hash).to_json
    end
    
    def json_for *args, &block
      ActiveSupport::Deprecation.warn('"json_for" deprecated. Use "json" instead.')
      json(*args, &block)
    end
    
    def has_json_key? key, scope=:public, options={}
      json_version(scope, options).has_key?(key.to_s) and json_version(scope, options)[key.to_s].present?
    end
    
    def json_version *args
      JSON.parse(json(*args))
    end
    alias json_hash json_version
    
    def json_key key, scope=:public, options={}
      json_version(scope, options)[key.to_s]
    end
    
    
  end
end
serializer=(value) click to toggle source

Serializer

# File lib/pinkman/model/base.rb, line 10
def self.serializer= value
  @serializer = value
end

Public Instance Methods

has_json_key?(key, scope=:public, options={}) click to toggle source
# File lib/pinkman/model/base.rb, line 64
def has_json_key? key, scope=:public, options={}
  json_version(scope, options).has_key?(key.to_s) and json_version(scope, options)[key.to_s].present?
end
json(scope_name=:public, params_hash={}) click to toggle source
# File lib/pinkman/model/base.rb, line 55
def json scope_name=:public, params_hash={}
  serialize_for(scope_name, params_hash).to_json
end
json_for(*args, &block) click to toggle source
# File lib/pinkman/model/base.rb, line 59
def json_for *args, &block
  ActiveSupport::Deprecation.warn('"json_for" deprecated. Use "json" instead.')
  json(*args, &block)
end
json_key(key, scope=:public, options={}) click to toggle source
# File lib/pinkman/model/base.rb, line 73
def json_key key, scope=:public, options={}
  json_version(scope, options)[key.to_s]
end
json_version(*args) click to toggle source
# File lib/pinkman/model/base.rb, line 68
def json_version *args
  JSON.parse(json(*args))
end
serialize_for(scope_name, params_hash = {}) click to toggle source
# File lib/pinkman/model/base.rb, line 50
def serialize_for scope_name, params_hash = {}
  options = {scope: scope_name}.merge(params: params_hash)
  self.class.serializer.new(self,options)
end