snfoil-controller 1.0.0 → 1.0.1

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: bd05e7dcc970820561431b9d85d19a5fd3420d00bc726b80adb26fdd01248d5b
4
- data.tar.gz: 6f7a84286eedb8bbf29efae538192239df2f2ca96d6aa3ae7ed7ee8e1f1703ff
3
+ metadata.gz: b5d020649fc151b8e6cdd45cdd7dbed9c139a47f043a5a77787c224ececb4fa7
4
+ data.tar.gz: 24a6a982f2133cafe7d6ee79c86b56420fc7cb48165b6abb6fb3bc5a6cb73874
5
5
  SHA512:
6
- metadata.gz: '08be4e5d500fbaa0b2ee35b8937aac5e6ab427c20df897272241679c24c2eb0a5e3172c38c25c6d3e53c195584a3520764f6814d89d1fa1d350690f263b46a76'
7
- data.tar.gz: 80e7866dcc05d23c35740ccb0eb8776a5e9828ccf63c329f574df40c87665e4469a1c8df6aebc34ed968f7ab7b9614337e99e34e0a0641de5ff8e37ba2ab9ce8
6
+ metadata.gz: 98923708de99e83490c25b69b66d2b2915e726e3bb843545c037c07b49f41cfcd64011a8fa626d80af63ffb1a67a534bb8746e37f60d0805abb62ad742db1fe9
7
+ data.tar.gz: 59af2628e1969f9653c50fbae80b4d3946fd96aad8e2c5da230cb15b18ed15b98577d869e7052551e36ea5ef37c19d1b3db2d2ebc72236d3218b130030f36c4a
data/README.md CHANGED
@@ -518,6 +518,36 @@ has_many :pets
518
518
  </tbody>
519
519
  </table>
520
520
 
521
+ ### JSON Deserializer
522
+
523
+ ##### Attrbute - Namespace
524
+
525
+ The JSON Deserializer has attribute namespacing that isn't available in JSONAPI due to its structured nature.
526
+
527
+ - `namespace` an array of the nested keys needed to access a value
528
+
529
+ This works with both `attribute` and `attributes`.
530
+
531
+
532
+ ```ruby
533
+ attribute :rank, namespace: [:military_information]
534
+ ```
535
+
536
+ Which would pull a nested field like in the following example.
537
+
538
+ ```json
539
+ {
540
+ "name":"John",
541
+ ...
542
+ "military-info": {
543
+ "branch":"Army",
544
+ "rank":"Private First Class"
545
+ }
546
+ ...
547
+ }
548
+ ```
549
+
550
+
521
551
  ## Development
522
552
 
523
553
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -16,6 +16,6 @@
16
16
 
17
17
  module SnFoil
18
18
  module Controller
19
- VERSION = '1.0.0'
19
+ VERSION = '1.0.1'
20
20
  end
21
21
  end
@@ -103,8 +103,10 @@ module SnFoil
103
103
  exec_deserialize(deserializer, params, **options)
104
104
  end
105
105
 
106
- def run_context(context: nil, context_action: nil, controller_action: nil, **_options)
107
- (context || self.class.snfoil_context).new(entity).send(context_action || controller_action)
106
+ def run_context(**options)
107
+ (options[:context] || self.class.snfoil_context)
108
+ .new(entity: entity)
109
+ .send(options[:context_action] || options[:controller_action], **options)
108
110
  end
109
111
 
110
112
  protected
@@ -75,17 +75,6 @@ module SnFoil
75
75
  @data = normalize_keys(input)
76
76
  @config = config
77
77
  end
78
-
79
- def parse
80
- raise '#parse not implemented'
81
- end
82
-
83
- def to_hash
84
- parse
85
- end
86
-
87
- alias_method :to_hash, :parse
88
- alias_method :to_h, :parse
89
78
  end
90
79
 
91
80
  protected
@@ -35,6 +35,13 @@ module SnFoil
35
35
  apply_transforms({}, data)
36
36
  end
37
37
 
38
+ def to_hash
39
+ parse
40
+ end
41
+
42
+ alias_method :to_hash, :parse
43
+ alias_method :to_h, :parse
44
+
38
45
  protected
39
46
 
40
47
  def apply_transforms(output, input)
@@ -86,9 +93,17 @@ module SnFoil
86
93
  elsif with
87
94
  send(with, input, key, **options)
88
95
  else
89
- value_key = options.fetch(:key) { key }
90
- value_key = "#{options[:prefix]}#{key}".to_sym if options[:prefix]
96
+ find_by_key(input, key, **options)
97
+ end
98
+ end
99
+
100
+ def find_by_key(input, key, **options)
101
+ value_key = options.fetch(:key) { key }
102
+ value_key = "#{options[:prefix]}#{key}".to_sym if options[:prefix]
91
103
 
104
+ if options[:namespace]
105
+ input.dig(*options[:namespace], value_key.to_sym)
106
+ else
92
107
  input[value_key.to_sym]
93
108
  end
94
109
  end
@@ -31,15 +31,20 @@ module SnFoil
31
31
  included do # rubocop:disable Metrics/BlockLength reason: These methods need to be in included to be overridable
32
32
  include SnFoil::Deserializer::JSON
33
33
 
34
- module_eval do
35
- def parse
36
- input = data[:data] || data
37
- return apply_transforms(data_id({}, input), input) unless input.is_a? Array
34
+ def parse
35
+ input = data[:data] || data
36
+ return apply_transforms(data_id({}, input), input) unless input.is_a? Array
38
37
 
39
- input.map { |d| apply_transforms(data_id({}, d), d) }
40
- end
38
+ input.map { |d| apply_transforms(data_id({}, d), d) }
39
+ end
40
+
41
+ def to_hash
42
+ parse
41
43
  end
42
44
 
45
+ alias_method :to_hash, :parse
46
+ alias_method :to_h, :parse
47
+
43
48
  def included
44
49
  @included ||= config[:included] || data[:included]
45
50
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: snfoil-controller
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Howes
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2022-05-11 00:00:00.000000000 Z
12
+ date: 2022-05-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport