jsonapi-object-mapper 0.3.0 → 0.4.0

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
  SHA256:
3
- metadata.gz: 8ac8001aaaee12db86481bcda0c0808b9a2675210fc9279597ba8275c9bcd082
4
- data.tar.gz: 1cba087107c90277c06b5f936f7a4aeeb47913ee32453d1af617518c70d73e81
3
+ metadata.gz: 35390dfcd2da06f3fadc628d41986b07891efa2590a4271896a9717ed44ed7b6
4
+ data.tar.gz: eb48c0ab5e7409c53ba03828704adcef9e27377443498b4f5a97430cde8cd5ad
5
5
  SHA512:
6
- metadata.gz: b43b966095f8a26f1dd0d80d288d0b8c8ca0095e58ee1bb85084e8715396ad66d345961825e11734d30121a7f2181558ff5284a86d7ce1ee78aff9c8c2112748
7
- data.tar.gz: 3ca0fd6fc8b238a5b45cc7ccaab5c7c1bc00c3d67956916efcd3289c78ca88629c9f18f98ab400a9580f54d7bb5a30e0fdd030a5cb3653bc8574e074fe73deab
6
+ metadata.gz: 96a5b2682366e3d45ff87b768c1ace3463847721293a0237b7661fe4c0d19b2049ed2f1616b73f9d47eea4a62b69d7fb359e1e86753ff1123388c171a7f62e97
7
+ data.tar.gz: 5d75d3a36a2cd47ef118dcd4e51f31129e5f7cd8e8e7b0daaa93077e8d809551261f52a2ef321e4c20a30a8a82d75dad2344056413e95d740234978b45d6fe72
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- jsonapi-object-mapper (0.3.0)
4
+ jsonapi-object-mapper (0.4.0)
5
5
  oj (~> 3.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -33,8 +33,9 @@ class User < JsonAPIObjectMapper::Deserializer::Resource
33
33
  # Embedding with another Resource class, will deserialize the `included` resource with the given class
34
34
  has_one :photo, embed_with: Photo
35
35
 
36
- # By default the value will be assigned whatever in located in the `included` selection.
37
- # Other wise basic relationship resources will be added.
36
+ # By default the value will be assigned whatever is located in the `included` selection.
37
+ # Otherwise basic relationship resource information will be added.
38
+ # - IE: { "type" = "friend", "id" = "10" }
38
39
  has_one :friend
39
40
 
40
41
  # This will accept the default value
@@ -45,8 +46,7 @@ class User < JsonAPIObjectMapper::Deserializer::Resource
45
46
  attr_value.upcase
46
47
  end
47
48
 
48
- # You can mass assign attributes using the `attributes` method instead if blocks don't matter
49
-
49
+ # You can mass-assign attributes using the `attributes` method instead if blocks don't matter
50
50
  attributes :ssn, :passport, :more_person_info
51
51
  end
52
52
 
@@ -54,7 +54,7 @@ end
54
54
  User.call_collection(json_payload) #=> [<#User:123>, <#User:432>]
55
55
  user = User.call(json_payload) #=> <#User:123>
56
56
 
57
- user.first_name #=> "Foo"
57
+ user.first_name #=> "FOOER"
58
58
  user.last_name #=> "Bar"
59
59
 
60
60
  ```
@@ -16,10 +16,12 @@ module JsonAPIObjectMapper
16
16
 
17
17
  def id(&block)
18
18
  self.id_block = block || DEFAULT_ID_BLOCK
19
+ define_method(:id) { fetch_attribute(:id) }
19
20
  end
20
21
 
21
22
  def type(&block)
22
23
  self.type_block = block || DEFAULT_TYPE_BLOCK
24
+ define_method(:type) { fetch_attribute(:type) }
23
25
  end
24
26
 
25
27
  def attribute(attribute_name, &block)
@@ -9,7 +9,7 @@ module JsonAPIObjectMapper
9
9
  extend DSL
10
10
 
11
11
  class << self
12
- attr_accessor :rel_blocks, :rel_options, :attr_blocks
12
+ attr_accessor :rel_blocks, :rel_options, :attr_blocks, :id_block, :type_block
13
13
  end
14
14
  instance_variable_set("@attr_blocks", {})
15
15
  instance_variable_set("@rel_blocks", {})
@@ -20,6 +20,8 @@ module JsonAPIObjectMapper
20
20
  klass.instance_variable_set("@attr_blocks", attr_blocks.dup)
21
21
  klass.instance_variable_set("@rel_blocks", rel_blocks.dup)
22
22
  klass.instance_variable_set("@rel_options", rel_options.dup)
23
+ klass.instance_variable_set("@id_block", id_block)
24
+ klass.instance_variable_set("@type_block", type_block)
23
25
  end
24
26
 
25
27
  def self.call_collection(document)
@@ -41,6 +43,8 @@ module JsonAPIObjectMapper
41
43
  def initialize(payload = nil, included = nil)
42
44
  super()
43
45
  data = payload || {}
46
+ @id = data["id"]
47
+ @type = data["type"]
44
48
  @attributes = data["attributes"] || {}
45
49
  @relationships = data["relationships"] || {}
46
50
  @includes = IncludedResources.load(included)
@@ -52,10 +56,16 @@ module JsonAPIObjectMapper
52
56
  private
53
57
 
54
58
  def deserialize!
59
+ deserialize_id_type!
55
60
  deserialize_attributes!
56
61
  deserialize_relationships!
57
62
  end
58
63
 
64
+ def deserialize_id_type!
65
+ assign_attribute("id", self.class.id_block&.call(@id))
66
+ assign_attribute("type", self.class.type_block&.call(@id))
67
+ end
68
+
59
69
  def deserialize_attributes!
60
70
  return if @attributes.empty?
61
71
  @attributes.each_pair(&method(:initialize_attribute))
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonAPIObjectMapper
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-object-mapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - George Protacio-Karaszi