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 +4 -4
- data/README.md +30 -0
- data/lib/snfoil/controller/version.rb +1 -1
- data/lib/snfoil/controller.rb +4 -2
- data/lib/snfoil/deserializer/base.rb +0 -11
- data/lib/snfoil/deserializer/json.rb +17 -2
- data/lib/snfoil/deserializer/jsonapi.rb +11 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b5d020649fc151b8e6cdd45cdd7dbed9c139a47f043a5a77787c224ececb4fa7
|
4
|
+
data.tar.gz: 24a6a982f2133cafe7d6ee79c86b56420fc7cb48165b6abb6fb3bc5a6cb73874
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
data/lib/snfoil/controller.rb
CHANGED
@@ -103,8 +103,10 @@ module SnFoil
|
|
103
103
|
exec_deserialize(deserializer, params, **options)
|
104
104
|
end
|
105
105
|
|
106
|
-
def run_context(
|
107
|
-
(context || self.class.snfoil_context)
|
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
|
-
|
90
|
-
|
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
|
-
|
35
|
-
|
36
|
-
|
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
|
-
|
40
|
-
|
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.
|
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-
|
12
|
+
date: 2022-05-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|