Class: JsonApiModel::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/json_api_model/scope.rb

Instance Method Summary collapse

Constructor Details

#initialize(resource_class) ⇒ Scope

Returns a new instance of Scope.



3
4
5
6
# File 'lib/json_api_model/scope.rb', line 3

def initialize( resource_class )
  @resource_class = resource_class
  @client_scope   = JsonApiClient::Query::Builder.new( resource_class.client_class )
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/json_api_model/scope.rb', line 44

def method_missing( m, *args, &block )
  if @client_scope.respond_to? m
    @client_scope.send m, *args, &block
    self
  else
    all.send m, *args, &block
  end
end

Instance Method Details

#buildObject



36
37
38
# File 'lib/json_api_model/scope.rb', line 36

def build
  @resource_class.new_from_client @client_scope.build
end

#find(args = {}) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/json_api_model/scope.rb', line 15

def find( args = {} )
  JsonApiModel.instrumenter.instrument 'find.json_api_model',
                                        args: args,
                                        url: url do
    results = @client_scope.find args
    ResultSet.new( results, @resource_class )
  end
end

#firstObject



24
25
26
27
28
# File 'lib/json_api_model/scope.rb', line 24

def first
  JsonApiModel.instrumenter.instrument 'first.json_api_model', url: url do
    @resource_class.new_from_client @client_scope.first
  end
end

#lastObject



30
31
32
33
34
# File 'lib/json_api_model/scope.rb', line 30

def last
  JsonApiModel.instrumenter.instrument 'last.json_api_model', url: url do
    @resource_class.new_from_client @client_scope.last
  end
end

#paramsObject



40
41
42
# File 'lib/json_api_model/scope.rb', line 40

def params
  @client_scope.params
end

#to_aObject Also known as: all



8
9
10
# File 'lib/json_api_model/scope.rb', line 8

def to_a
  @to_a ||= find
end