json-pie 0.0.2 → 0.0.5

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
  SHA256:
3
- metadata.gz: 8bc688a815ec0c446b67a8f9a05bb742ff0ae19e9f3207b9241630b9d13acbd9
4
- data.tar.gz: ee0357b235fabfdc886ad9821cd8d4d096a59d0016b4acbc6cfc32c88ca8bf96
3
+ metadata.gz: 0bedce91be54603fef3199e786dcf0a788a2b7d6b3681a8b7930fccb9d72fbf5
4
+ data.tar.gz: 65e7ad0412287ce4ca5996e2866db9de91a4488d26bd92c039d830371db7bd06
5
5
  SHA512:
6
- metadata.gz: c9f9617599a2ff972f648d986436bbc645a605fc5e2ac396ffadb4527cb189ac5a0af0dae1c10cafbcab69424c2f951e0e9a204b9823bdd90eaea68dee56e840
7
- data.tar.gz: 471dd338d3caa975998bad5d77f03ee4c474801126825f13fb72a876d40e974583b9bba0dd9695c6d5cb96cd1533a3756aaa082e39bb8de287854d27ef6c288d
6
+ metadata.gz: 1b087ae7f01aab0dbb2378d8e05c83f71e4ab001dea546b6d4e75b34c48ca6547f87da5fd07a524c9698a3f47d393e443ea7e8e9decf9eb689fff262cdd0971f
7
+ data.tar.gz: 4133435d3337cb72d1779fb42614d3370533414452ef98a0880440453289158cd57aa8fb78bc8c93197fb9d7f0903d52ac2b6174e1c87808513f94389f8b1886
data/.rubocop.yml CHANGED
@@ -24,3 +24,4 @@ Metrics/BlockLength:
24
24
  Exclude:
25
25
  - Guardfile
26
26
  - "**/*_spec.rb"
27
+ - json-pie.gemspec
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
- ## [Unreleased]
1
+ ## [unreleased]
2
2
 
3
- ## [0.1.0] - 2022-02-27
3
+ ## [0.0.5] - 2022-03-14
4
4
 
5
- - Initial release
5
+ - Handles nullifying relationships
6
+
7
+ ## [0.0.4] - 2022-03-03
8
+
9
+ - Imporved public description of the gem
10
+
11
+ ## [0.0.3] - 2022-03-03
12
+
13
+ - Allow data structure to define resource ID when creating a new resource.
data/Gemfile.lock CHANGED
@@ -1,73 +1,73 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- json-pie (0.0.1)
4
+ json-pie (0.0.4)
5
5
  rails (<= 7.1)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- actioncable (7.0.2.2)
11
- actionpack (= 7.0.2.2)
12
- activesupport (= 7.0.2.2)
10
+ actioncable (7.0.2.3)
11
+ actionpack (= 7.0.2.3)
12
+ activesupport (= 7.0.2.3)
13
13
  nio4r (~> 2.0)
14
14
  websocket-driver (>= 0.6.1)
15
- actionmailbox (7.0.2.2)
16
- actionpack (= 7.0.2.2)
17
- activejob (= 7.0.2.2)
18
- activerecord (= 7.0.2.2)
19
- activestorage (= 7.0.2.2)
20
- activesupport (= 7.0.2.2)
15
+ actionmailbox (7.0.2.3)
16
+ actionpack (= 7.0.2.3)
17
+ activejob (= 7.0.2.3)
18
+ activerecord (= 7.0.2.3)
19
+ activestorage (= 7.0.2.3)
20
+ activesupport (= 7.0.2.3)
21
21
  mail (>= 2.7.1)
22
22
  net-imap
23
23
  net-pop
24
24
  net-smtp
25
- actionmailer (7.0.2.2)
26
- actionpack (= 7.0.2.2)
27
- actionview (= 7.0.2.2)
28
- activejob (= 7.0.2.2)
29
- activesupport (= 7.0.2.2)
25
+ actionmailer (7.0.2.3)
26
+ actionpack (= 7.0.2.3)
27
+ actionview (= 7.0.2.3)
28
+ activejob (= 7.0.2.3)
29
+ activesupport (= 7.0.2.3)
30
30
  mail (~> 2.5, >= 2.5.4)
31
31
  net-imap
32
32
  net-pop
33
33
  net-smtp
34
34
  rails-dom-testing (~> 2.0)
35
- actionpack (7.0.2.2)
36
- actionview (= 7.0.2.2)
37
- activesupport (= 7.0.2.2)
35
+ actionpack (7.0.2.3)
36
+ actionview (= 7.0.2.3)
37
+ activesupport (= 7.0.2.3)
38
38
  rack (~> 2.0, >= 2.2.0)
39
39
  rack-test (>= 0.6.3)
40
40
  rails-dom-testing (~> 2.0)
41
41
  rails-html-sanitizer (~> 1.0, >= 1.2.0)
42
- actiontext (7.0.2.2)
43
- actionpack (= 7.0.2.2)
44
- activerecord (= 7.0.2.2)
45
- activestorage (= 7.0.2.2)
46
- activesupport (= 7.0.2.2)
42
+ actiontext (7.0.2.3)
43
+ actionpack (= 7.0.2.3)
44
+ activerecord (= 7.0.2.3)
45
+ activestorage (= 7.0.2.3)
46
+ activesupport (= 7.0.2.3)
47
47
  globalid (>= 0.6.0)
48
48
  nokogiri (>= 1.8.5)
49
- actionview (7.0.2.2)
50
- activesupport (= 7.0.2.2)
49
+ actionview (7.0.2.3)
50
+ activesupport (= 7.0.2.3)
51
51
  builder (~> 3.1)
52
52
  erubi (~> 1.4)
53
53
  rails-dom-testing (~> 2.0)
54
54
  rails-html-sanitizer (~> 1.1, >= 1.2.0)
55
- activejob (7.0.2.2)
56
- activesupport (= 7.0.2.2)
55
+ activejob (7.0.2.3)
56
+ activesupport (= 7.0.2.3)
57
57
  globalid (>= 0.3.6)
58
- activemodel (7.0.2.2)
59
- activesupport (= 7.0.2.2)
60
- activerecord (7.0.2.2)
61
- activemodel (= 7.0.2.2)
62
- activesupport (= 7.0.2.2)
63
- activestorage (7.0.2.2)
64
- actionpack (= 7.0.2.2)
65
- activejob (= 7.0.2.2)
66
- activerecord (= 7.0.2.2)
67
- activesupport (= 7.0.2.2)
58
+ activemodel (7.0.2.3)
59
+ activesupport (= 7.0.2.3)
60
+ activerecord (7.0.2.3)
61
+ activemodel (= 7.0.2.3)
62
+ activesupport (= 7.0.2.3)
63
+ activestorage (7.0.2.3)
64
+ actionpack (= 7.0.2.3)
65
+ activejob (= 7.0.2.3)
66
+ activerecord (= 7.0.2.3)
67
+ activesupport (= 7.0.2.3)
68
68
  marcel (~> 1.0)
69
69
  mini_mime (>= 1.1.0)
70
- activesupport (7.0.2.2)
70
+ activesupport (7.0.2.3)
71
71
  concurrent-ruby (~> 1.0, >= 1.0.2)
72
72
  i18n (>= 1.6, < 2)
73
73
  minitest (>= 5.1)
@@ -148,28 +148,28 @@ GEM
148
148
  rack (2.2.3)
149
149
  rack-test (1.1.0)
150
150
  rack (>= 1.0, < 3)
151
- rails (7.0.2.2)
152
- actioncable (= 7.0.2.2)
153
- actionmailbox (= 7.0.2.2)
154
- actionmailer (= 7.0.2.2)
155
- actionpack (= 7.0.2.2)
156
- actiontext (= 7.0.2.2)
157
- actionview (= 7.0.2.2)
158
- activejob (= 7.0.2.2)
159
- activemodel (= 7.0.2.2)
160
- activerecord (= 7.0.2.2)
161
- activestorage (= 7.0.2.2)
162
- activesupport (= 7.0.2.2)
151
+ rails (7.0.2.3)
152
+ actioncable (= 7.0.2.3)
153
+ actionmailbox (= 7.0.2.3)
154
+ actionmailer (= 7.0.2.3)
155
+ actionpack (= 7.0.2.3)
156
+ actiontext (= 7.0.2.3)
157
+ actionview (= 7.0.2.3)
158
+ activejob (= 7.0.2.3)
159
+ activemodel (= 7.0.2.3)
160
+ activerecord (= 7.0.2.3)
161
+ activestorage (= 7.0.2.3)
162
+ activesupport (= 7.0.2.3)
163
163
  bundler (>= 1.15.0)
164
- railties (= 7.0.2.2)
164
+ railties (= 7.0.2.3)
165
165
  rails-dom-testing (2.0.3)
166
166
  activesupport (>= 4.2.0)
167
167
  nokogiri (>= 1.6)
168
168
  rails-html-sanitizer (1.4.2)
169
169
  loofah (~> 2.3)
170
- railties (7.0.2.2)
171
- actionpack (= 7.0.2.2)
172
- activesupport (= 7.0.2.2)
170
+ railties (7.0.2.3)
171
+ actionpack (= 7.0.2.3)
172
+ activesupport (= 7.0.2.3)
173
173
  method_source
174
174
  rake (>= 12.2)
175
175
  thor (~> 1.0)
data/README.md CHANGED
@@ -2,6 +2,9 @@
2
2
 
3
3
  Parse JSON:API data structures into Rails ActiveRecord structures.
4
4
 
5
+ ![Tests](https://github.com/ekampp/json-pie/actions/workflows/tests.yml/badge.svg)
6
+ ![Linting](https://github.com/ekampp/json-pie/actions/workflows/linting.yml/badge.svg)
7
+
5
8
  ## Installation
6
9
 
7
10
  ```
@@ -14,9 +14,8 @@ module JSON
14
14
  def initialize(type:, id: nil, **options)
15
15
  @options = options
16
16
  klass = determine_type(type).to_s.classify.constantize
17
- @instance = id ? klass.find(id) : klass.new
17
+ @instance = klass.find_or_initialize_by klass.primary_key => id
18
18
  rescue NameError
19
- pp options
20
19
  raise JSON::Pie::InvalidType, "#{type}(#{id})"
21
20
  end
22
21
 
@@ -24,6 +24,8 @@ module JSON
24
24
  end
25
25
 
26
26
  def instance
27
+ return nil if data.blank?
28
+
27
29
  @instance ||= JSON::Pie::ResourceIdentity.find_or_initialize(**data.slice(:id, :type), **options)
28
30
  end
29
31
 
@@ -41,7 +43,7 @@ module JSON
41
43
  end
42
44
 
43
45
  def assign_attributes!
44
- instance.attributes = data.fetch :attributes, {}
46
+ instance&.attributes = attributes_tuple.to_h
45
47
  rescue ActiveModel::UnknownAttributeError
46
48
  raise JSON::Pie::InvalidAttribute
47
49
  end
@@ -51,6 +53,13 @@ module JSON
51
53
  instance: instance,
52
54
  relationships: data.fetch(:relationships, {})
53
55
  end
56
+
57
+ def attributes_tuple
58
+ data.fetch(:attributes, {}).collect do |key, value|
59
+ key = options.dig(:attributes_map, data.fetch(:type), key).presence || key
60
+ [key, value]
61
+ end
62
+ end
54
63
  end
55
64
  end
56
65
  end
@@ -19,7 +19,7 @@ module JSON
19
19
 
20
20
  def assign
21
21
  relationships.each do |rel, data|
22
- relationship_instance = ResourceObject.parse data.fetch(:data)
22
+ relationship_instance = ResourceObject.parse (data || {}).fetch(:data, {})
23
23
  instance.public_send "#{rel}=", relationship_instance
24
24
  end
25
25
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module JSON
4
4
  module Pie
5
- VERSION = "0.0.2"
5
+ VERSION = "0.0.5"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-pie
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emil Kampp
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-02-28 00:00:00.000000000 Z
11
+ date: 2022-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -24,7 +24,10 @@ dependencies:
24
24
  - - "<="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '7.1'
27
- description: Allow a Rails API to use the JSON:API interface
27
+ description: |
28
+ Easily parse JSON:API data structures into ActiveRecord resources.
29
+
30
+ This will parse deeply nested relationships as well as attributes.
28
31
  email:
29
32
  - emil@kampp.me
30
33
  executables: []
@@ -73,5 +76,5 @@ requirements: []
73
76
  rubygems_version: 3.2.32
74
77
  signing_key:
75
78
  specification_version: 4
76
- summary: JSON:API rails plugin
79
+ summary: Easily parse JSON:API structures into ActiveRecord resources
77
80
  test_files: []