jsonapi_compliable 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 58d8e742552f8d3c559e5717fd4e282846e4d904
4
- data.tar.gz: f297e2bf808e4a22596ac9fdc54e0350f6a5e132
3
+ metadata.gz: a2ba641517d1b4433f52881fcf610733d49eabff
4
+ data.tar.gz: 55dd7a70e1da568c2263eb957764c4adc402f9e4
5
5
  SHA512:
6
- metadata.gz: 42c2dff29d06844d41e3c1042065d33d627e41d4a84231e947d77ca416b63317c9dd5e97fa94da9327197c918c5675a53293257d4bbe53206ec7de35d98cf743
7
- data.tar.gz: bd862e16092f6e89f6652fc1c0dd814f0f30095329678776c9eb3045336b164a87360467b7f53d92b4fc5142f1aee3475e4a8bd7bdc1547373ae1cfb62277116
6
+ metadata.gz: 21ab6475b3792bde16e04e63c40079408e8fac5dd1f1b3001c8b1412f1b85bae1c555ed3b022a9000df025952f008ddea305c2102248ef3ab2f1e30d83e298f4
7
+ data.tar.gz: 8182598c1b91062d2a9bc2cca2c1cbfe5a8567995b28d3599dfa1433880f756466a75ce427c5f77a77055473f718da88bc6de5afe10e78741a1e14ac7089c3f9
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- jsonapi_compliable (0.4.0)
4
+ jsonapi_compliable (0.5.2)
5
5
  jsonapi-serializable (~> 0.1)
6
6
 
7
7
  GEM
@@ -196,7 +196,7 @@ DEPENDENCIES
196
196
  guard-rspec
197
197
  jsonapi-rails (~> 0.1)
198
198
  jsonapi_compliable!
199
- kaminari
199
+ kaminari (~> 0.17)
200
200
  pry
201
201
  pry-byebug
202
202
  rails (~> 4.1)
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../
3
3
  specs:
4
- jsonapi_compliable (0.4.0)
4
+ jsonapi_compliable (0.5.2)
5
5
  jsonapi-serializable (~> 0.1)
6
6
 
7
7
  GEM
@@ -200,7 +200,7 @@ DEPENDENCIES
200
200
  guard-rspec
201
201
  jsonapi-rails (~> 0.1)
202
202
  jsonapi_compliable!
203
- kaminari
203
+ kaminari (~> 0.17)
204
204
  pry
205
205
  pry-byebug
206
206
  rails (~> 5.0)
@@ -26,17 +26,24 @@ module JsonapiCompliable
26
26
  end
27
27
 
28
28
  def include_hash
29
- @include_hash ||= include_directive.to_hash
29
+ @include_hash ||= begin
30
+ requested = include_directive.to_hash
31
+ all_allowed = resource.sideloading.to_hash[:base]
32
+ whitelist = resource.sideload_whitelist.values.reduce(:merge)
33
+ allowed = whitelist ? Util::IncludeParams.scrub(all_allowed, whitelist) : all_allowed
34
+
35
+ Util::IncludeParams.scrub(requested, allowed)
36
+ end
30
37
  end
31
38
 
32
- def all_requested_association_names
33
- @all_requested_association_names ||= Util::Hash.keys(include_hash)
39
+ def association_names
40
+ @association_names ||= Util::Hash.keys(include_hash)
34
41
  end
35
42
 
36
43
  def to_hash
37
44
  hash = { resource.type => self.class.default_hash }
38
45
 
39
- all_requested_association_names.each do |name|
46
+ association_names.each do |name|
40
47
  hash[name] = self.class.default_hash
41
48
  end
42
49
 
@@ -50,7 +57,8 @@ module JsonapiCompliable
50
57
  parse_filter(hash)
51
58
  parse_sort(hash)
52
59
  parse_pagination(hash)
53
- parse_include(hash, include_hash)
60
+
61
+ parse_include(hash, include_hash, resource.type)
54
62
  parse_stats(hash)
55
63
 
56
64
  hash
@@ -68,13 +76,14 @@ module JsonapiCompliable
68
76
  resource.association_names.include?(name)
69
77
  end
70
78
 
71
- def parse_include(memo, incl_hash, namespace = nil)
72
- namespace ||= resource.type
79
+ def parse_include(memo, incl_hash, namespace)
80
+ memo[namespace].merge!(include: incl_hash)
73
81
 
74
- memo[namespace][:include] = incl_hash
75
82
  incl_hash.each_pair do |key, sub_hash|
76
- memo[key][:include] = parse_include(memo, sub_hash, key)
83
+ memo.merge!(parse_include(memo, sub_hash, key))
77
84
  end
85
+
86
+ memo
78
87
  end
79
88
 
80
89
  def parse_stats(hash)
@@ -1,41 +1,27 @@
1
1
  module JsonapiCompliable
2
2
  class Resource
3
- attr_reader :filters,
4
- :default_filters,
5
- :default_sort,
6
- :default_page_size,
7
- :default_page_number,
8
- :sorting,
9
- :stats,
10
- :sideload_whitelist,
11
- :pagination,
12
- :extra_fields,
13
- :sideloading,
14
- :adapter,
15
- :type,
16
- :context
3
+ attr_reader :context
17
4
 
18
5
  class << self
19
6
  attr_accessor :config
7
+
8
+ delegate :allow_sideload, to: :sideloading
9
+ delegate :has_many, to: :sideloading
10
+ delegate :has_one, to: :sideloading
11
+ delegate :belongs_to, to: :sideloading
12
+ delegate :has_and_belongs_to_many, to: :sideloading
13
+ delegate :polymorphic_belongs_to, to: :sideloading
14
+ delegate :polymorphic_has_many, to: :sideloading
20
15
  end
21
16
 
22
17
  delegate :sideload, to: :sideloading
23
18
 
24
- # Incorporate custom adapter methods
25
- def self.method_missing(meth, *args, &blk)
26
- if sideloading.respond_to?(meth)
27
- sideloading.send(meth, *args, &blk)
28
- else
29
- super
30
- end
31
- end
32
-
33
19
  def self.inherited(klass)
34
20
  klass.config = Util::Hash.deep_dup(self.config)
35
21
  end
36
22
 
37
23
  def self.sideloading
38
- config[:sideloading] ||= Sideload.new(:base, resource: self)
24
+ @sideloading ||= Sideload.new(:base, resource: self)
39
25
  end
40
26
 
41
27
  def self.sideload_whitelist(whitelist)
@@ -111,23 +97,6 @@ module JsonapiCompliable
111
97
  end
112
98
  end
113
99
 
114
- def copy
115
- self.class.new(@config)
116
- end
117
-
118
- def initialize(config = nil)
119
- config = config || self.class.config
120
- config = Util::Hash.deep_dup(config)
121
- set_config(config)
122
- end
123
-
124
- def set_config(config)
125
- @config = config
126
- config.each_pair do |key, value|
127
- instance_variable_set(:"@#{key}", value)
128
- end
129
- end
130
-
131
100
  def with_context(object, namespace = nil)
132
101
  begin
133
102
  prior = context
@@ -173,20 +142,56 @@ module JsonapiCompliable
173
142
  stats_dsl.calculation(calculation)
174
143
  end
175
144
 
145
+ def sideloading
146
+ self.class.sideloading
147
+ end
148
+
176
149
  def default_sort
177
- @default_sort || [{ id: :asc }]
150
+ self.class.config[:default_sort] || [{ id: :asc }]
178
151
  end
179
152
 
180
153
  def default_page_number
181
- @default_page_number || 1
154
+ self.class.config[:default_page_number] || 1
182
155
  end
183
156
 
184
157
  def default_page_size
185
- @default_page_size || 20
158
+ self.class.config[:default_page_size] || 20
186
159
  end
187
160
 
188
161
  def type
189
- @type || :undefined_jsonapi_type
162
+ self.class.config[:type] || :undefined_jsonapi_type
163
+ end
164
+
165
+ def filters
166
+ self.class.config[:filters]
167
+ end
168
+
169
+ def sorting
170
+ self.class.config[:sorting]
171
+ end
172
+
173
+ def stats
174
+ self.class.config[:stats]
175
+ end
176
+
177
+ def pagination
178
+ self.class.config[:pagination]
179
+ end
180
+
181
+ def extra_fields
182
+ self.class.config[:extra_fields]
183
+ end
184
+
185
+ def sideload_whitelist
186
+ self.class.config[:sideload_whitelist]
187
+ end
188
+
189
+ def default_filters
190
+ self.class.config[:default_filters]
191
+ end
192
+
193
+ def adapter
194
+ self.class.config[:adapter]
190
195
  end
191
196
  end
192
197
  end
@@ -1,7 +1,7 @@
1
1
  module JsonapiCompliable
2
2
  class Sideload
3
3
  attr_reader :name,
4
- :resource,
4
+ :resource_class,
5
5
  :polymorphic,
6
6
  :sideloads,
7
7
  :scope_proc,
@@ -10,12 +10,12 @@ module JsonapiCompliable
10
10
 
11
11
  def initialize(name, opts)
12
12
  @name = name
13
- @resource = (opts[:resource] || Class.new(Resource)).new
13
+ @resource_class = (opts[:resource] || Class.new(Resource))
14
14
  @sideloads = {}
15
15
  @polymorphic = !!opts[:polymorphic]
16
16
  @polymorphic_groups = {} if polymorphic?
17
17
 
18
- extend @resource.adapter.sideloading_module
18
+ extend @resource_class.config[:adapter].sideloading_module
19
19
  end
20
20
 
21
21
  def polymorphic?
@@ -65,10 +65,8 @@ module JsonapiCompliable
65
65
  @sideloads.each_pair do |key, sideload|
66
66
  hash[name][key] = sideload.to_hash[key]
67
67
 
68
- if sideloading = sideload.resource.sideloading
69
- sideloading.sideloads.each_pair do |k, s|
70
- hash[name][k] = s.to_hash[k]
71
- end
68
+ if sideloading = sideload.resource_class.sideloading
69
+ hash[name][key].merge!(sideloading.to_hash[:base])
72
70
  end
73
71
  end
74
72
  end
@@ -87,7 +85,7 @@ module JsonapiCompliable
87
85
 
88
86
  def resolve_basic(parents, query, namespace)
89
87
  sideload_scope = scope_proc.call(parents)
90
- sideload_scope = Scope.new(sideload_scope, resource, query, namespace: namespace)
88
+ sideload_scope = Scope.new(sideload_scope, resource_class.new, query, namespace: namespace)
91
89
  sideload_results = sideload_scope.resolve
92
90
  assign_proc.call(parents, sideload_results)
93
91
  end
@@ -1,8 +1,8 @@
1
1
  module JsonapiCompliable
2
2
  module Stats
3
3
  class Payload
4
- def initialize(dsl, query_hash, scope)
5
- @dsl = dsl
4
+ def initialize(resource, query_hash, scope)
5
+ @resource = resource
6
6
  @query_hash = query_hash[:stats]
7
7
  @scope = scope
8
8
  end
@@ -23,7 +23,7 @@ module JsonapiCompliable
23
23
 
24
24
  def each_calculation(name, calculations)
25
25
  calculations.each do |calc|
26
- function = @dsl.stat(name, calc)
26
+ function = @resource.stat(name, calc)
27
27
  yield calc, function
28
28
  end
29
29
  end
@@ -1,3 +1,3 @@
1
1
  module JsonapiCompliable
2
- VERSION = "0.5.2"
2
+ VERSION = "0.5.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi_compliable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lee Richmond
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2017-02-09 00:00:00.000000000 Z
12
+ date: 2017-02-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: jsonapi-serializable