class JsonApiModel::Scope

Attributes

client_scope[RW]

Public Class Methods

new( model_class ) click to toggle source
# File lib/json_api_model/scope.rb, line 3
def initialize( model_class )
  @model_class  = model_class
  @client_scope = JsonApiClient::Query::Builder.new( model_class.client_class )
  @cache        = {}
end

Public Instance Methods

all( args = {} )
Alias for: find
find( args = {} ) click to toggle source
# File lib/json_api_model/scope.rb, line 9
def find( args = {} )
  JsonApiModel.instrumenter.instrument 'find.json_api_model',
                                        args: args,
                                        url: url do
    cache_or_find args do
      results = @client_scope.find args
      ResultSet.new( results, @model_class )
    end
  end
end
Also aliased as: to_a, all
first() click to toggle source
# File lib/json_api_model/scope.rb, line 23
def first
  JsonApiModel.instrumenter.instrument 'first.json_api_model', url: url do
    # if the non-first query has already been executed, there's no need to make the call again
    if cached?
      cache.first
    else
      cache_or_find :first do
        @model_class.new_from_client @client_scope.first
      end
    end
  end
end
last() click to toggle source
# File lib/json_api_model/scope.rb, line 36
def last
  JsonApiModel.instrumenter.instrument 'last.json_api_model', url: url do
    # this is a separate call always because the last record may exceed page size
    cache_or_find :last do
      @model_class.new_from_client @client_scope.last
    end
  end
end
method_missing( m, *args, &block ) click to toggle source
# File lib/json_api_model/scope.rb, line 45
def method_missing( m, *args, &block )
  if @client_scope.respond_to? m
    _new_scope @client_scope.send( m, *args, &block )
  else
    all.send m, *args, &block
  end
end
to_a( args = {} )
Alias for: find

Private Instance Methods

_new_scope( client_scope ) click to toggle source
# File lib/json_api_model/scope.rb, line 58
def _new_scope( client_scope )
  self.class.new( @model_class ).tap do |scope|
    scope.client_scope = client_scope
  end
end
cache() click to toggle source
# File lib/json_api_model/scope.rb, line 68
def cache
  @cache[keyify]
end
cache_or_find( opts = {} ) { || ... } click to toggle source

because a scope can be modified and then resolved, we want to cache by the full param set

# File lib/json_api_model/scope.rb, line 73
def cache_or_find( opts = {} )
  key = keyify( opts )
  @cache.fetch key do |key|
    @cache[key] = yield
  end
end
cached?() click to toggle source
# File lib/json_api_model/scope.rb, line 64
def cached?
  @cache.has_key? keyify
end
hashify( opts = {} ) click to toggle source
# File lib/json_api_model/scope.rb, line 84
def hashify( opts = {} )
  case opts
  when Hash
    opts
  else
    { opt: opts }
  end
end
keyify( opts = {} ) click to toggle source
# File lib/json_api_model/scope.rb, line 80
def keyify( opts = {} )
  params.merge( hashify( opts ) ).sort.to_s
end
url() click to toggle source
# File lib/json_api_model/scope.rb, line 93
def url
  @model_class.client_class.requestor.send( :resource_path, params )
end