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.
10
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/json_api_client/query/builder.rb', line 10
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
108
109
110
|
# File 'lib/json_api_client/query/builder.rb', line 108
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.
7
8
9
|
# File 'lib/json_api_client/query/builder.rb', line 7
def klass
@klass
end
|
Instance Method Details
#==(other) ⇒ Object
Also known as:
eql?
119
120
121
122
123
|
# File 'lib/json_api_client/query/builder.rb', line 119
def ==(other)
return false unless other.is_a?(self.class)
hash == other.hash
end
|
#build(attrs = {}) ⇒ Object
69
70
71
|
# File 'lib/json_api_client/query/builder.rb', line 69
def build(attrs = {})
klass.new @path_params.merge(attrs.symbolize_keys)
end
|
#create(attrs = {}) ⇒ Object
73
74
75
|
# File 'lib/json_api_client/query/builder.rb', line 73
def create(attrs = {})
klass.create @path_params.merge(attrs.symbolize_keys)
end
|
#find(args = {}) ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/json_api_client/query/builder.rb', line 93
def find(args = {})
if klass.raise_on_blank_find_param && args.blank?
raise Errors::NotFound, 'blank .find param'
end
case args
when Hash
scope = where(args)
else
scope = _new_scope( primary_key: args )
end
scope._fetch
end
|
#first ⇒ Object
61
62
63
|
# File 'lib/json_api_client/query/builder.rb', line 61
def first
paginate(page: 1, per_page: 1).to_a.first
end
|
#hash ⇒ Object
112
113
114
115
116
117
|
# File 'lib/json_api_client/query/builder.rb', line 112
def hash
[
klass,
params
].hash
end
|
#includes(*tables) ⇒ Object
34
35
36
|
# File 'lib/json_api_client/query/builder.rb', line 34
def includes(*tables)
_new_scope( includes: parse_related_links(*tables) )
end
|
#last ⇒ Object
65
66
67
|
# File 'lib/json_api_client/query/builder.rb', line 65
def last
paginate(page: 1, per_page: 1).pages.last.to_a.last
end
|
#order(*args) ⇒ Object
30
31
32
|
# File 'lib/json_api_client/query/builder.rb', line 30
def order(*args)
_new_scope( orders: parse_orders(*args) )
end
|
#page(number) ⇒ Object
49
50
51
|
# File 'lib/json_api_client/query/builder.rb', line 49
def page(number)
_new_scope( pagination_params: { klass.paginator.page_param => number || 1 } )
end
|
#paginate(conditions = {}) ⇒ Object
42
43
44
45
46
47
|
# File 'lib/json_api_client/query/builder.rb', line 42
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
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/json_api_client/query/builder.rb', line 77
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
53
54
55
|
# File 'lib/json_api_client/query/builder.rb', line 53
def per(size)
_new_scope( pagination_params: { klass.paginator.per_page_param => size } )
end
|
#select(*fields) ⇒ Object
38
39
40
|
# File 'lib/json_api_client/query/builder.rb', line 38
def select(*fields)
_new_scope( fields: parse_fields(*fields) )
end
|
#to_a ⇒ Object
Also known as:
all
88
89
90
|
# File 'lib/json_api_client/query/builder.rb', line 88
def to_a
@to_a ||= _fetch
end
|
#where(conditions = {}) ⇒ Object
22
23
24
25
26
27
28
|
# File 'lib/json_api_client/query/builder.rb', line 22
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
57
58
59
|
# File 'lib/json_api_client/query/builder.rb', line 57
def with_params(more_params)
_new_scope( additional_params: more_params )
end
|