Skip to content

Commit d9c3f17

Browse files
committed
fixing sopes to be immutable
1 parent c6d0f37 commit d9c3f17

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

lib/json_api_client/query/builder.rb

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,60 +5,55 @@ class Builder
55
attr_reader :klass
66
delegate :key_formatter, to: :klass
77

8-
def initialize(klass)
9-
@klass = klass
10-
@primary_key = nil
11-
@pagination_params = {}
12-
@path_params = {}
13-
@additional_params = {}
14-
@filters = {}
15-
@includes = []
16-
@orders = []
17-
@fields = []
8+
def initialize(klass, opts = {})
9+
@klass = klass
10+
@primary_key = nil
11+
@pagination_params = opts.fetch( :pagination_params, {} )
12+
@path_params = opts.fetch( :path_params, {} )
13+
@additional_params = opts.fetch( :additional_params, {} )
14+
@filters = opts.fetch( :filters, {} )
15+
@includes = opts.fetch( :includes, [] )
16+
@orders = opts.fetch( :orders, [] )
17+
@fields = opts.fetch( :fields, [] )
1818
end
1919

2020
def where(conditions = {})
2121
# pull out any path params here
22-
@path_params.merge!(conditions.slice(*klass.prefix_params))
23-
@filters.merge!(conditions.except(*klass.prefix_params))
24-
self
22+
path_conditions = conditions.slice(*klass.prefix_params)
23+
unpathed_conditions = conditions.except(*klass.prefix_params)
24+
25+
_new_scope( path_params: path_conditions, filters: unpathed_conditions )
2526
end
2627

2728
def order(*args)
28-
@orders += parse_orders(*args)
29-
self
29+
_new_scope( orders: parse_orders(*args) )
3030
end
3131

3232
def includes(*tables)
33-
@includes += parse_related_links(*tables)
34-
self
33+
_new_scope( includes: parse_related_links(*tables) )
3534
end
3635

3736
def select(*fields)
38-
@fields += parse_fields(*fields)
39-
self
37+
_new_scope( fields: parse_fields(*fields) )
4038
end
4139

4240
def paginate(conditions = {})
43-
scope = self
41+
scope = _new_scope
4442
scope = scope.page(conditions[:page]) if conditions[:page]
4543
scope = scope.per(conditions[:per_page]) if conditions[:per_page]
4644
scope
4745
end
4846

4947
def page(number)
50-
@pagination_params[ klass.paginator.page_param ] = number || 1
51-
self
48+
_new_scope( pagination_params: { klass.paginator.page_param => number || 1 } )
5249
end
5350

5451
def per(size)
55-
@pagination_params[ klass.paginator.per_page_param ] = size
56-
self
52+
_new_scope( pagination_params: { klass.paginator.per_page_param => size } )
5753
end
5854

5955
def with_params(more_params)
60-
@additional_params.merge!(more_params)
61-
self
56+
_new_scope( additional_params: more_params )
6257
end
6358

6459
def first
@@ -106,6 +101,17 @@ def method_missing(method_name, *args, &block)
106101

107102
private
108103

104+
def _new_scope( opts = {} )
105+
self.class.new( @klass,
106+
pagination_params: @pagination_params.merge( opts.fetch( :pagination_params, {} ) ),
107+
path_params: @path_params.merge( opts.fetch( :path_params, {} ) ),
108+
additional_params: @additional_params.merge( opts.fetch( :additional_params, {} ) ),
109+
filters: @filters.merge( opts.fetch( :filters, {} ) ),
110+
includes: @includes + opts.fetch( :includes, [] ),
111+
orders: @orders + opts.fetch( :orders, [] ),
112+
fields: @fields + opts.fetch( :fields, [] ) )
113+
end
114+
109115
def path_params
110116
@path_params.empty? ? {} : {path: @path_params}
111117
end

0 commit comments

Comments
 (0)