Class: JsonApiClient::Query::Builder
- Inherits:
-
Object
- Object
- JsonApiClient::Query::Builder
show all
- Defined in:
- lib/json_api_client/query/builder.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(klass, opts = {}) ⇒ Builder
Returns a new instance of Builder.
8
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/json_api_client/query/builder.rb', line 8
def initialize(klass, opts = {})
@klass = klass
@primary_key = opts.fetch( :primary_key, nil )
@pagination_params = opts.fetch( :pagination_params, {} )
@path_params = opts.fetch( :path_params, {} )
@additional_params = opts.fetch( :additional_params, {} )
@filters = opts.fetch( :filters, {} )
@includes = opts.fetch( :includes, [] )
@orders = opts.fetch( :orders, [] )
@fields = opts.fetch( :fields, [] )
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
98
99
100
|
# File 'lib/json_api_client/query/builder.rb', line 98
def method_missing(method_name, *args, &block)
to_a.send(method_name, *args, &block)
end
|
Instance Attribute Details
#klass ⇒ Object
Returns the value of attribute klass.
5
6
7
|
# File 'lib/json_api_client/query/builder.rb', line 5
def klass
@klass
end
|
Instance Method Details
#build ⇒ Object
67
68
69
|
# File 'lib/json_api_client/query/builder.rb', line 67
def build
klass.new(params)
end
|
#find(args = {}) ⇒ Object
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/json_api_client/query/builder.rb', line 87
def find(args = {})
case args
when Hash
scope = where(args)
else
scope = _new_scope( primary_key: args )
end
klass.requestor.get(scope.params)
end
|
#first ⇒ Object
59
60
61
|
# File 'lib/json_api_client/query/builder.rb', line 59
def first
paginate(page: 1, per_page: 1).to_a.first
end
|
#includes(*tables) ⇒ Object
32
33
34
|
# File 'lib/json_api_client/query/builder.rb', line 32
def includes(*tables)
_new_scope( includes: parse_related_links(*tables) )
end
|
#last ⇒ Object
63
64
65
|
# File 'lib/json_api_client/query/builder.rb', line 63
def last
paginate(page: 1, per_page: 1).pages.last.to_a.last
end
|
#order(*args) ⇒ Object
28
29
30
|
# File 'lib/json_api_client/query/builder.rb', line 28
def order(*args)
_new_scope( orders: parse_orders(*args) )
end
|
#page(number) ⇒ Object
47
48
49
|
# File 'lib/json_api_client/query/builder.rb', line 47
def page(number)
_new_scope( pagination_params: { klass.paginator.page_param => number || 1 } )
end
|
#paginate(conditions = {}) ⇒ Object
40
41
42
43
44
45
|
# File 'lib/json_api_client/query/builder.rb', line 40
def paginate(conditions = {})
scope = _new_scope
scope = scope.page(conditions[:page]) if conditions[:page]
scope = scope.per(conditions[:per_page]) if conditions[:per_page]
scope
end
|
#params ⇒ Object
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/json_api_client/query/builder.rb', line 71
def params
filter_params
.merge()
.merge(includes_params)
.merge(order_params)
.merge(select_params)
.merge(primary_key_params)
.merge(path_params)
.merge(additional_params)
end
|
#per(size) ⇒ Object
51
52
53
|
# File 'lib/json_api_client/query/builder.rb', line 51
def per(size)
_new_scope( pagination_params: { klass.paginator.per_page_param => size } )
end
|
#select(*fields) ⇒ Object
36
37
38
|
# File 'lib/json_api_client/query/builder.rb', line 36
def select(*fields)
_new_scope( fields: parse_fields(*fields) )
end
|
#to_a ⇒ Object
Also known as:
all
82
83
84
|
# File 'lib/json_api_client/query/builder.rb', line 82
def to_a
@to_a ||= find
end
|
#where(conditions = {}) ⇒ Object
20
21
22
23
24
25
26
|
# File 'lib/json_api_client/query/builder.rb', line 20
def where(conditions = {})
path_conditions = conditions.slice(*klass.prefix_params)
unpathed_conditions = conditions.except(*klass.prefix_params)
_new_scope( path_params: path_conditions, filters: unpathed_conditions )
end
|
#with_params(more_params) ⇒ Object
55
56
57
|
# File 'lib/json_api_client/query/builder.rb', line 55
def with_params(more_params)
_new_scope( additional_params: more_params )
end
|