json-pie 0.0.2 → 0.0.3

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: 8bc688a815ec0c446b67a8f9a05bb742ff0ae19e9f3207b9241630b9d13acbd9
4
- data.tar.gz: ee0357b235fabfdc886ad9821cd8d4d096a59d0016b4acbc6cfc32c88ca8bf96
3
+ metadata.gz: edcde33d7c1de58eea685a86544b43bcc4492abda2b9a8807efa2848afe1336d
4
+ data.tar.gz: 2882246fa32e1f7c37a250fc5ccd6fc6ecabe79965f6199256af408e92f24ff6
5
5
  SHA512:
6
- metadata.gz: c9f9617599a2ff972f648d986436bbc645a605fc5e2ac396ffadb4527cb189ac5a0af0dae1c10cafbcab69424c2f951e0e9a204b9823bdd90eaea68dee56e840
7
- data.tar.gz: 471dd338d3caa975998bad5d77f03ee4c474801126825f13fb72a876d40e974583b9bba0dd9695c6d5cb96cd1533a3756aaa082e39bb8de287854d27ef6c288d
6
+ metadata.gz: 97e5df38f0da9985fc9dad48410f8786d4d65826db6ece95e9e62a39a18591300dadab1fc995e772d3a4dac8c153d94b9a4e8c73c3b5fee99b09d96aa747fac8
7
+ data.tar.gz: 1596d7b4116c18a288932cd2cf66534c3cec05778ca24a610e879cacb9e82366cdf99a35c3d2e0db70ec36bc5fd5180c812bbe7f8230b6d0918db3fc5618f134
data/CHANGELOG.md CHANGED
@@ -1,5 +1,5 @@
1
- ## [Unreleased]
1
+ ## [unreleased]
2
2
 
3
- ## [0.1.0] - 2022-02-27
3
+ ## [0.0.3] - 2022-03-03
4
4
 
5
- - Initial release
5
+ - Allow data structure to define resource ID when creating a new resource.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- json-pie (0.0.1)
4
+ json-pie (0.0.2)
5
5
  rails (<= 7.1)
6
6
 
7
7
  GEM
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
 
@@ -41,7 +41,7 @@ module JSON
41
41
  end
42
42
 
43
43
  def assign_attributes!
44
- instance.attributes = data.fetch :attributes, {}
44
+ instance.attributes = attributes_tuple.to_h
45
45
  rescue ActiveModel::UnknownAttributeError
46
46
  raise JSON::Pie::InvalidAttribute
47
47
  end
@@ -51,6 +51,13 @@ module JSON
51
51
  instance: instance,
52
52
  relationships: data.fetch(:relationships, {})
53
53
  end
54
+
55
+ def attributes_tuple
56
+ data.fetch(:attributes, {}).collect do |key, value|
57
+ key = options.dig(:attributes_map, data.fetch(:type), key).presence || key
58
+ [key, value]
59
+ end
60
+ end
54
61
  end
55
62
  end
56
63
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module JSON
4
4
  module Pie
5
- VERSION = "0.0.2"
5
+ VERSION = "0.0.3"
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.3
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-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails