jsonapi-resources 0.0.10 → 0.0.11

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: f0f3c0056192f7ce0100152a1365750eeec04e3b
4
- data.tar.gz: 5d2b1057fc7f624e91eee60ea8a5cbaff019516d
3
+ metadata.gz: 693f8ed867afa11d9d23f0631bdf2ccd7e19acbe
4
+ data.tar.gz: 5a0edbc2ab2a5f7c801c3120307c54c7c6a1142a
5
5
  SHA512:
6
- metadata.gz: 25ff4a750913e3a987e2e0c3b1e366b8195db07d4487d849608637cb0c3cdfc62bbae9ea0553284bba60a12b41de19b6d451f448dd68c32c9b92d8319cde5d9b
7
- data.tar.gz: 41a3b01c6367dd22b4ce0f285a2a4cbba91cb84868ab5dbb345498387cc2921c5565edc0c6f095de37a8a56b8b8c093460f6f83f2be3d9c55821869671dd8468
6
+ metadata.gz: e5f18920d6adee8dd748507071addacc51f58ba2719bf0961fb4fad01de7fb4af229755f90eb22b7c06ec20772fdff835fb6b7234b54c7a341d1eb6885ea879d
7
+ data.tar.gz: 83901cfc4f1d2dad3883e782bf90cef7cbf62873faba9b872fb84b756ec71c5020bca86acba72cbc72617a55b2cc58bab3b12152a89cf20e5f9e6a2c40aaa9aa
@@ -207,9 +207,7 @@ module JSONAPI
207
207
  elsif association.is_a?(JSONAPI::Association::HasMany)
208
208
  keys = []
209
209
  if value.is_a?(Array)
210
- value.each do |val|
211
- keys.push(@resource_klass.resource_for(association.type).verify_key(val, @context))
212
- end
210
+ keys = @resource_klass.resource_for(association.type).verify_keys(value, @context)
213
211
  else
214
212
  keys.push(@resource_klass.resource_for(association.type).verify_key(value, @context))
215
213
  end
@@ -387,11 +385,7 @@ module JSONAPI
387
385
  end
388
386
 
389
387
  def parse_key_array(raw)
390
- keys = []
391
- raw.split(/,/).collect do |key|
392
- keys.push @resource_klass.verify_key(key, @context)
393
- end
394
- return keys
388
+ return @resource_klass.verify_keys(raw.split(/,/), context)
395
389
  end
396
390
 
397
391
  def format_key(key)
@@ -127,6 +127,9 @@ module JSONAPI
127
127
 
128
128
  type = base.name.demodulize.sub(/Resource$/, '').underscore
129
129
  base._type = type.pluralize.to_sym
130
+
131
+ check_reserved_resource_name(base._type, base.name)
132
+
130
133
  # If eager loading is on this is how all the resource types are setup
131
134
  # If eager loading is off some resource types will be initialized in
132
135
  # _resource_name_from_type
@@ -159,6 +162,8 @@ module JSONAPI
159
162
  end
160
163
 
161
164
  def attribute(attr, options = {})
165
+ check_reserved_attribute_name(attr)
166
+
162
167
  @_attributes[attr] = options
163
168
  define_method attr do
164
169
  @model.send(attr)
@@ -302,6 +307,13 @@ module JSONAPI
302
307
  return key
303
308
  end
304
309
 
310
+ # override to allow for key processing and checking
311
+ def verify_keys(keys, context = nil)
312
+ return keys.collect do |key|
313
+ verify_key(key, context)
314
+ end
315
+ end
316
+
305
317
  # override to allow for custom filters
306
318
  def verify_custom_filter(filter, value, context = nil)
307
319
  return filter, value
@@ -388,10 +400,33 @@ module JSONAPI
388
400
 
389
401
  private
390
402
 
403
+ def check_reserved_resource_name(type, name)
404
+ if [:ids, :types, :hrefs, :links].include?(type)
405
+ warn "[NAME COLLISION] `#{name}` is a reserved resource name."
406
+ return
407
+ end
408
+ end
409
+
410
+ def check_reserved_attribute_name(name)
411
+ # Allow :id since it can be used to specify the format. Since it is a method on the base Resource
412
+ # an attribute method won't be created for it.
413
+ if [:type, :href, :links].include?(name.to_sym)
414
+ warn "[NAME COLLISION] `#{name}` is a reserved key in #{@@resource_types[_type]}."
415
+ end
416
+ end
417
+
418
+ def check_reserved_association_name(name)
419
+ if [:id, :ids, :type, :types, :href, :hrefs, :link, :links].include?(name.to_sym)
420
+ warn "[NAME COLLISION] `#{name}` is a reserved association name in #{@@resource_types[_type]}."
421
+ end
422
+ end
423
+
391
424
  def _associate(klass, *attrs)
392
425
  options = attrs.extract_options!
393
426
 
394
427
  attrs.each do |attr|
428
+ check_reserved_association_name(attr)
429
+
395
430
  @_associations[attr] = klass.new(attr, options)
396
431
 
397
432
  foreign_key = @_associations[attr].foreign_key
@@ -120,11 +120,7 @@ module JSONAPI
120
120
  end
121
121
 
122
122
  def parse_key_array(raw)
123
- keys = []
124
- raw.split(/,/).collect do |key|
125
- keys.push resource_klass.verify_key(key, context)
126
- end
127
- return keys
123
+ return resource_klass.verify_keys(raw.split(/,/), context)
128
124
  end
129
125
 
130
126
  # override to set context
@@ -1,5 +1,5 @@
1
1
  module JSONAPI
2
2
  module Resources
3
- VERSION = "0.0.10"
3
+ VERSION = "0.0.11"
4
4
  end
5
5
  end
@@ -379,9 +379,7 @@ class PostResource < JSONAPI::Resource
379
379
  def self.verify_custom_filter(filter, values, context = nil)
380
380
  case filter
381
381
  when :id
382
- values.each do |key|
383
- verify_key(key, context)
384
- end
382
+ verify_keys(values, context)
385
383
  end
386
384
  return filter, values
387
385
  end
@@ -476,6 +474,23 @@ class PreferencesResource < JSONAPI::Resource
476
474
  has_many :friends, class_name: 'Person'
477
475
  end
478
476
 
477
+ warn 'start testing Name Collisions'
478
+ # The name collisions only emmit warnings. Exceptions would change the flow of the tests
479
+
480
+ class LinksResource < JSONAPI::Resource
481
+
482
+ end
483
+
484
+ class BadlyNamedAttributesResource < JSONAPI::Resource
485
+ attributes :type, :href, :links
486
+
487
+ has_many :links
488
+ has_one :href
489
+ has_one :id
490
+ has_many :types
491
+ end
492
+ warn 'end testing Name Collisions'
493
+
479
494
  ### DATA
480
495
  javascript = Section.create(name: 'javascript')
481
496
  ruby = Section.create(name: 'ruby')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Gebhardt
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-11-11 00:00:00.000000000 Z
12
+ date: 2014-11-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler