json_api_client 1.6.1 → 1.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9d4a8de0c8279d6ea5d6b7a3bf1d3da4af650d66
4
- data.tar.gz: 84624c1a31f574187b0adb03f1db3800df2af6d4
3
+ metadata.gz: 5dc92bcac4713e71d886a74ff2e510a77cd938b2
4
+ data.tar.gz: 690731e4d4f2565c8f812f03fe7b7e0f0ba24543
5
5
  SHA512:
6
- metadata.gz: 2ea79d5d91d055556a3f26ca37d3d3447744b03858c668e676d11b787e5658bbf7b31965416c016ad46c4a42bff116df977876330852a8db48bb9f7eef477076
7
- data.tar.gz: c7a70e2f4fb4a969e47a2dec2be67b77b682523ca4d5a1ccde554c855c632163582395af8951047044bba128c17d0270f0383e93f30341ca649477dd19a257c6
6
+ metadata.gz: 52daf62f539cb691ef0edf517e00c6f9359c9d2afec757af24f1cd6a642f5bd54477c257802b4f65c0a058b67acf9cc5f372d87c58709d245e08bce4db2de07e
7
+ data.tar.gz: c53470bc17b38e6c2a8aaaa8bec98c1f37f782c2d1e198e8803c0b12b7b2851f81a4791d5df1c185d10a3e0627b8fad730a860ded10a6c7afa770dc91f07b1ac
@@ -1,15 +1,6 @@
1
1
  module JsonApiClient
2
2
  module Associations
3
3
  module BelongsTo
4
- extend ActiveSupport::Concern
5
-
6
- module ClassMethods
7
- def belongs_to(attr_name, options = {})
8
- # self.associations = self.associations + [HasOne::Association.new(attr_name, self, options)]
9
- self.associations += [BelongsTo::Association.new(attr_name, self, options)]
10
- end
11
- end
12
-
13
4
  class Association < BaseAssociation
14
5
  include Helpers::URI
15
6
  def param
@@ -1,14 +1,6 @@
1
1
  module JsonApiClient
2
2
  module Associations
3
3
  module HasMany
4
- extend ActiveSupport::Concern
5
-
6
- module ClassMethods
7
- def has_many(attr_name, options = {})
8
- self.associations = self.associations + [HasMany::Association.new(attr_name, self, options)]
9
- end
10
- end
11
-
12
4
  class Association < BaseAssociation
13
5
  end
14
6
  end
@@ -1,14 +1,6 @@
1
1
  module JsonApiClient
2
2
  module Associations
3
3
  module HasOne
4
- extend ActiveSupport::Concern
5
-
6
- module ClassMethods
7
- def has_one(attr_name, options = {})
8
- self.associations += [HasOne::Association.new(attr_name, self, options)]
9
- end
10
- end
11
-
12
4
  class Association < BaseAssociation
13
5
  def from_result_set(result_set)
14
6
  result_set.first
@@ -0,0 +1,64 @@
1
+ module JsonApiClient
2
+ module Helpers
3
+ module Associatable
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ class_attribute :associations, instance_accessor: false
8
+ self.associations = []
9
+ attr_accessor :__cached_associations
10
+ end
11
+
12
+ module ClassMethods
13
+ def _define_association(attr_name, association_klass, options = {})
14
+ attr_name = attr_name.to_sym
15
+ association = association_klass.new(attr_name, self, options)
16
+ self.associations += [association]
17
+
18
+ define_method(attr_name) do
19
+ _cached_relationship(attr_name) do
20
+ relationship_definition = relationship_definition_for(attr_name)
21
+ return unless relationship_definition
22
+ relationship_data_for(attr_name, relationship_definition)
23
+ end
24
+ end
25
+
26
+ define_method("#{attr_name}=") do |value|
27
+ _clear_cached_relationship(attr_name)
28
+ relationships.public_send("#{attr_name}=", value)
29
+ end
30
+ end
31
+
32
+ def belongs_to(attr_name, options = {})
33
+ _define_association(attr_name, JsonApiClient::Associations::BelongsTo::Association, options)
34
+ end
35
+
36
+ def has_many(attr_name, options = {})
37
+ _define_association(attr_name, JsonApiClient::Associations::HasMany::Association, options)
38
+ end
39
+
40
+ def has_one(attr_name, options = {})
41
+ _define_association(attr_name, JsonApiClient::Associations::HasOne::Association, options)
42
+ end
43
+ end
44
+
45
+ def _cached_associations
46
+ self.__cached_associations ||= {}
47
+ end
48
+
49
+ def _clear_cached_relationships
50
+ self.__cached_associations = {}
51
+ end
52
+
53
+ def _clear_cached_relationship(attr_name)
54
+ _cached_associations.delete(attr_name)
55
+ end
56
+
57
+ def _cached_relationship(attr_name)
58
+ return _cached_associations[attr_name] if _cached_associations.has_key?(attr_name)
59
+ _cached_associations[attr_name] = yield
60
+ end
61
+
62
+ end
63
+ end
64
+ end
@@ -39,12 +39,7 @@ module JsonApiClient
39
39
 
40
40
  def method_missing(method, *args, &block)
41
41
  if has_attribute?(method)
42
- self.class.class_eval do
43
- define_method(method) do
44
- attributes[method]
45
- end
46
- end
47
- return send(method)
42
+ return attributes[method]
48
43
  end
49
44
 
50
45
  normalized_method = safe_key_formatter.unformat(method.to_s)
@@ -4,5 +4,6 @@ module JsonApiClient
4
4
  autoload :Dirty, 'json_api_client/helpers/dirty'
5
5
  autoload :DynamicAttributes, 'json_api_client/helpers/dynamic_attributes'
6
6
  autoload :URI, 'json_api_client/helpers/uri'
7
+ autoload :Associatable, 'json_api_client/helpers/associatable'
7
8
  end
8
9
  end
@@ -29,7 +29,7 @@ module JsonApiClient
29
29
  when 409
30
30
  raise Errors::Conflict, env
31
31
  when 400..499
32
- # some other error
32
+ raise Errors::ClientError, env
33
33
  when 500..599
34
34
  raise Errors::ServerError, env
35
35
  else
@@ -12,6 +12,7 @@ module JsonApiClient
12
12
 
13
13
  include Helpers::DynamicAttributes
14
14
  include Helpers::Dirty
15
+ include Helpers::Associatable
15
16
 
16
17
  attr_accessor :last_result_set,
17
18
  :links,
@@ -29,7 +30,6 @@ module JsonApiClient
29
30
  :relationship_linker,
30
31
  :read_only_attributes,
31
32
  :requestor_class,
32
- :associations,
33
33
  :json_key_format,
34
34
  :route_format,
35
35
  :request_params_class,
@@ -47,7 +47,6 @@ module JsonApiClient
47
47
  self.relationship_linker = Relationships::Relations
48
48
  self.read_only_attributes = [:id, :type, :links, :meta, :relationships]
49
49
  self.requestor_class = Query::Requestor
50
- self.associations = []
51
50
  self.request_params_class = RequestParams
52
51
  self.keep_request_params = false
53
52
  self.add_defaults_to_changes = false
@@ -58,10 +57,6 @@ module JsonApiClient
58
57
  #:underscored_route, :camelized_route, :dasherized_route, or custom
59
58
  self.route_format = :underscored_route
60
59
 
61
- include Associations::BelongsTo
62
- include Associations::HasMany
63
- include Associations::HasOne
64
-
65
60
  class << self
66
61
  extend Forwardable
67
62
  def_delegators :_new_scope, :where, :order, :includes, :select, :all, :paginate, :page, :with_params, :first, :find, :last
@@ -253,6 +248,12 @@ module JsonApiClient
253
248
  # @option options [Symbol] :default The default value for the property
254
249
  def property(name, options = {})
255
250
  schema.add(name, options)
251
+ define_method(name) do
252
+ attributes[name]
253
+ end
254
+ define_method("#{name}=") do |value|
255
+ set_attribute(name, value)
256
+ end
256
257
  end
257
258
 
258
259
  # Declare multiple properties with the same optional options
@@ -430,8 +431,10 @@ module JsonApiClient
430
431
  self.attributes = updated.attributes
431
432
  self.links.attributes = updated.links.attributes
432
433
  self.relationships.attributes = updated.relationships.attributes
434
+ self.relationships.last_result_set = last_result_set
433
435
  clear_changes_information
434
436
  self.relationships.clear_changes_information
437
+ _clear_cached_relationships
435
438
  end
436
439
  true
437
440
  end
@@ -447,6 +450,9 @@ module JsonApiClient
447
450
  false
448
451
  else
449
452
  self.attributes.clear
453
+ self.relationships.attributes.clear
454
+ self.relationships.last_result_set = nil
455
+ _clear_cached_relationships
450
456
  true
451
457
  end
452
458
  end
@@ -491,27 +497,36 @@ module JsonApiClient
491
497
  end
492
498
  end
493
499
 
494
- def method_missing(method, *args)
495
- association = association_for(method)
496
-
497
- return super unless association || (relationships && relationships.has_attribute?(method))
500
+ def relationship_definition_for(name)
501
+ relationships[name] if relationships && relationships.has_attribute?(name)
502
+ end
498
503
 
499
- return nil unless relationship_definitions = relationships[method]
504
+ def included_data_for(name, relationship_definition)
505
+ last_result_set.included.data_for(name, relationship_definition)
506
+ end
500
507
 
508
+ def relationship_data_for(name, relationship_definition)
501
509
  # look in included data
502
- if relationship_definitions.key?("data")
503
- return last_result_set.included.data_for(method, relationship_definitions)
510
+ if relationship_definition.key?("data")
511
+ return included_data_for(name, relationship_definition)
504
512
  end
505
513
 
506
- if association = association_for(method)
507
- # look for a defined relationship url
508
- if relationship_definitions["links"] && url = relationship_definitions["links"]["related"]
509
- return association.data(url)
510
- end
514
+ url = relationship_definition["links"]["related"]
515
+ if relationship_definition["links"] && url
516
+ return association_for(name).data(url)
511
517
  end
518
+
512
519
  nil
513
520
  end
514
521
 
522
+ def method_missing(method, *args)
523
+ relationship_definition = relationship_definition_for(method)
524
+
525
+ return super unless relationship_definition
526
+
527
+ relationship_data_for(method, relationship_definition)
528
+ end
529
+
515
530
  def respond_to_missing?(symbol, include_all = false)
516
531
  return true if relationships && relationships.has_attribute?(symbol)
517
532
  return true if association_for(symbol)
@@ -1,3 +1,3 @@
1
1
  module JsonApiClient
2
- VERSION = "1.6.1"
2
+ VERSION = "1.6.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_api_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.1
4
+ version: 1.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Ching
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-08 00:00:00.000000000 Z
11
+ date: 2018-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -134,6 +134,7 @@ files:
134
134
  - lib/json_api_client/errors.rb
135
135
  - lib/json_api_client/formatter.rb
136
136
  - lib/json_api_client/helpers.rb
137
+ - lib/json_api_client/helpers/associatable.rb
137
138
  - lib/json_api_client/helpers/callbacks.rb
138
139
  - lib/json_api_client/helpers/dirty.rb
139
140
  - lib/json_api_client/helpers/dynamic_attributes.rb