json_api_client 1.0.0.beta6 → 1.0.0.beta7

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: c39c70dd7ca6c162a658718790120555ac010083
4
- data.tar.gz: 83eaa82464904a5761933f4b2f41576f280a281a
3
+ metadata.gz: 5100ace657cbe5030d9b34777b0afd41a8f5a734
4
+ data.tar.gz: 04ee78102f48525c3655be0cbcf5a1fd325b93c1
5
5
  SHA512:
6
- metadata.gz: 36612db285d7249f01b5cfbff6594297ed3c838faee90b5d646ad9c51767b6cbd1bc4511063416691f19db92e06fecf54d60aa47c1d0fa23701ca43eaced2b92
7
- data.tar.gz: ad590905aa3c8921ccd199525ad367465acfd048e3516da9722f364c39bdcbd0dbf01d5f4c84a12b9e43c3e9e68cebee16eb73c67436c69ac7ea014d7d270e42
6
+ metadata.gz: 286d424f711a4ba8a8aaa104b3ee331dd9519bdba4e2f2ff99099454ab4a83970d0d435ceac34665d32a71390b419f1499191e910863ac4a128cade13cd25e46
7
+ data.tar.gz: 38fae6bbffb734965cf1e0153d6426fb8998e2dc076000d7f798f00c8f5d1786d3d19def4352645b2bbb7dced029934a4f98a52a5ca6e5cd4df8bae8860abc12
data/README.md CHANGED
@@ -311,6 +311,8 @@ The basic types that we allow are:
311
311
 
312
312
  Also, we consider `nil` to be an acceptable value and will not cast the value.
313
313
 
314
+ Note : Do not map the primary key as int.
315
+
314
316
  ## Customizing
315
317
 
316
318
  ### Paths
@@ -13,6 +13,14 @@ module JsonApiClient
13
13
  attr_name.to_s.classify
14
14
  end)
15
15
  end
16
+
17
+ def data(url)
18
+ from_result_set(association_class.requestor.linked(url))
19
+ end
20
+
21
+ def from_result_set(result_set)
22
+ result_set.to_a
23
+ end
16
24
  end
17
25
  end
18
26
  end
@@ -11,10 +11,6 @@ module JsonApiClient
11
11
  end
12
12
 
13
13
  class Association < BaseAssociation
14
- def parse(params)
15
- params ? association_class.new(params) : nil
16
- end
17
-
18
14
  def param
19
15
  :"#{attr_name}_id"
20
16
  end
@@ -10,9 +10,6 @@ module JsonApiClient
10
10
  end
11
11
 
12
12
  class Association < BaseAssociation
13
- def parse(param)
14
- [param].flatten.map{|data| association_class.new(data) }
15
- end
16
13
  end
17
14
  end
18
15
  end
@@ -5,14 +5,13 @@ module JsonApiClient
5
5
 
6
6
  module ClassMethods
7
7
  def has_one(attr_name, options = {})
8
- # self.associations = self.associations + [HasOne::Association.new(attr_name, self, options)]
9
8
  self.associations += [HasOne::Association.new(attr_name, self, options)]
10
9
  end
11
10
  end
12
11
 
13
12
  class Association < BaseAssociation
14
- def parse(params)
15
- params ? association_class.new(params) : nil
13
+ def from_result_set(result_set)
14
+ result_set.first
16
15
  end
17
16
  end
18
17
  end
@@ -62,7 +62,7 @@ module JsonApiClient
62
62
  end
63
63
 
64
64
  def set_attribute(name, value)
65
- attribute_will_change!(name) if value != attributes[name]
65
+ attribute_will_change!(name) if value != attributes[name] || !attributes.has_key?(name)
66
66
  super
67
67
  end
68
68
 
@@ -19,24 +19,21 @@ module JsonApiClient
19
19
 
20
20
  def data_for(method_name, definition)
21
21
  # If data is defined, pull the record from the included data
22
- if data = definition["data"]
23
- if data.is_a?(Array)
24
- # has_many link
25
- data.map do |link_def|
26
- record_for(link_def)
27
- end
28
- else
29
- # has_one link
30
- record_for(data)
22
+ return nil unless data = definition["data"]
23
+
24
+ if data.is_a?(Array)
25
+ # has_many link
26
+ data.map do |link_def|
27
+ record_for(link_def)
31
28
  end
32
29
  else
33
- # TODO: if "related" URI is defined, fetch the delated object and stuff it in data
34
- nil
30
+ # has_one link
31
+ record_for(data)
35
32
  end
36
33
  end
37
34
 
38
35
  def has_link?(name)
39
- data.has_key?(name)
36
+ data.has_key?(name.to_s)
40
37
  end
41
38
 
42
39
  private
@@ -20,6 +20,12 @@ module JsonApiClient
20
20
  end.compact]
21
21
  end
22
22
 
23
+ def as_json
24
+ Hash[attributes.map do |k, v|
25
+ [k, v.slice("data")] if v.has_key?("data")
26
+ end.compact]
27
+ end
28
+
23
29
  def attributes_for_serialization
24
30
  attributes.slice(*changed)
25
31
  end
@@ -319,6 +319,15 @@ module JsonApiClient
319
319
  end
320
320
  end
321
321
 
322
+ def as_json(*)
323
+ attributes.slice(:id, :type).tap do |h|
324
+ relationships.as_json.tap do |r|
325
+ h[:relationships] = r unless r.empty?
326
+ end
327
+ h[:attributes] = attributes.except(:id, :type).as_json
328
+ end
329
+ end
330
+
322
331
  # Mark all attributes for this record as dirty
323
332
  def set_all_dirty!
324
333
  set_all_attributes_dirty
@@ -354,6 +363,7 @@ module JsonApiClient
354
363
  mark_as_persisted!
355
364
  if updated = last_result_set.first
356
365
  self.attributes = updated.attributes
366
+ clear_changes_information
357
367
  end
358
368
  true
359
369
  end
@@ -379,12 +389,28 @@ module JsonApiClient
379
389
  protected
380
390
 
381
391
  def method_missing(method, *args)
382
- return super unless relationships && relationships.has_attribute?(method) && last_result_set.included
383
- last_result_set.included.data_for(method, relationships[method])
392
+ association = association_for(method)
393
+
394
+ return super unless association || (relationships && relationships.has_attribute?(method))
395
+
396
+ return nil unless relationship_definitions = relationships[method]
397
+
398
+ # look in included data
399
+ data = last_result_set.included.data_for(method, relationship_definitions)
400
+ return data if data
401
+
402
+ if association = association_for(method)
403
+ # look for a defined relationship url
404
+ if relationship_definitions["links"] && url = relationship_definitions["links"]["related"]
405
+ return association.data(url)
406
+ end
407
+ end
408
+ nil
384
409
  end
385
410
 
386
411
  def respond_to_missing?(symbol, include_all = false)
387
412
  return true if relationships && relationships.has_attribute?(symbol)
413
+ return true if association_for(symbol)
388
414
  super
389
415
  end
390
416
 
@@ -402,6 +428,10 @@ module JsonApiClient
402
428
  self.class.schema.find(name)
403
429
  end
404
430
 
431
+ def association_for(name)
432
+ self.class.associations.detect{|association| association.attr_name == name}
433
+ end
434
+
405
435
  def attributes_for_serialization
406
436
  attributes.except(*self.class.read_only_attributes).slice(*changed)
407
437
  end
@@ -1,3 +1,3 @@
1
1
  module JsonApiClient
2
- VERSION = "1.0.0.beta6"
2
+ VERSION = "1.0.0.beta7"
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.0.0.beta6
4
+ version: 1.0.0.beta7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Ching
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-29 00:00:00.000000000 Z
11
+ date: 2015-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -175,7 +175,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
175
175
  version: 1.3.1
176
176
  requirements: []
177
177
  rubyforge_project:
178
- rubygems_version: 2.4.5
178
+ rubygems_version: 2.4.8
179
179
  signing_key:
180
180
  specification_version: 4
181
181
  summary: Build client libraries compliant with specification defined by jsonapi.org