snfoil-controller 1.0.0 → 1.1.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 +42 -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
- data/snfoil-controller.gemspec +5 -5
- metadata +26 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c972e74246450b38ea6f71e0acb425565f85d5a4f2304a6832246b79eb0e0313
|
4
|
+
data.tar.gz: 4d048b56d294d390bd8c3dd71e5f3a52dafacc2e3392b51aa29e0eb788c43ba4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5669ecd96671cefc89e5ba4bd17d77a803ef87354a0517465f44f0a693b0a2272df77901a2107da0e1d7d25d2c14cf8b82703a32e7d934e92946877cabca9888
|
7
|
+
data.tar.gz: a04525d6fa57c7e4b7c75c23dd7a7d18f121e23aec90927d16924a3b92b40864ebaca553c77cb5f535c69a8541640c8351d5e31f8fb0fc4bf5482960111fbbef
|
data/README.md
CHANGED
@@ -208,6 +208,18 @@ end
|
|
208
208
|
</tbody>
|
209
209
|
</table>
|
210
210
|
|
211
|
+
You can directly call the context using the `#run_context` method and pass it the options. It will automatically process either the `:context_action` or the `:controller_action` from the options. This can be overridden by passing a method name to `#run_context`.
|
212
|
+
|
213
|
+
```ruby
|
214
|
+
...
|
215
|
+
context PeopleContext
|
216
|
+
|
217
|
+
def some_method(**options)
|
218
|
+
run_context(:elevate, **options) #=> Calls PeopleContext#elevate
|
219
|
+
end
|
220
|
+
...
|
221
|
+
```
|
222
|
+
|
211
223
|
#### Serializer
|
212
224
|
|
213
225
|
The main serializer intended to be called by the Controller. Also the default serializer and block used by the '#serialize` method.
|
@@ -518,6 +530,36 @@ has_many :pets
|
|
518
530
|
</tbody>
|
519
531
|
</table>
|
520
532
|
|
533
|
+
### JSON Deserializer
|
534
|
+
|
535
|
+
##### Attrbute - Namespace
|
536
|
+
|
537
|
+
The JSON Deserializer has attribute namespacing that isn't available in JSONAPI due to its structured nature.
|
538
|
+
|
539
|
+
- `namespace` an array of the nested keys needed to access a value
|
540
|
+
|
541
|
+
This works with both `attribute` and `attributes`.
|
542
|
+
|
543
|
+
|
544
|
+
```ruby
|
545
|
+
attribute :rank, namespace: [:military_information]
|
546
|
+
```
|
547
|
+
|
548
|
+
Which would pull a nested field like in the following example.
|
549
|
+
|
550
|
+
```json
|
551
|
+
{
|
552
|
+
"name":"John",
|
553
|
+
...
|
554
|
+
"military-info": {
|
555
|
+
"branch":"Army",
|
556
|
+
"rank":"Private First Class"
|
557
|
+
}
|
558
|
+
...
|
559
|
+
}
|
560
|
+
```
|
561
|
+
|
562
|
+
|
521
563
|
## Development
|
522
564
|
|
523
565
|
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(method = nil, **options)
|
107
|
+
(options[:context] || self.class.snfoil_context)
|
108
|
+
.new(entity: entity)
|
109
|
+
.send(method || 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
|
data/snfoil-controller.gemspec
CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
|
|
12
12
|
spec.description = 'A context-like experience for your controllers'
|
13
13
|
spec.homepage = 'https://github.com/limited-effort/snfoil-controller'
|
14
14
|
spec.license = 'Apache-2.0'
|
15
|
-
spec.required_ruby_version = '>= 2.
|
15
|
+
spec.required_ruby_version = '>= 2.6'
|
16
16
|
|
17
17
|
spec.metadata['homepage_uri'] = spec.homepage
|
18
18
|
spec.metadata['source_code_uri'] = spec.homepage
|
@@ -30,7 +30,7 @@ Gem::Specification.new do |spec|
|
|
30
30
|
|
31
31
|
spec.add_dependency 'activesupport', '>= 5.2.6'
|
32
32
|
spec.add_dependency 'jsonapi-serializer', '~> 2.2'
|
33
|
-
spec.add_dependency 'snfoil-context', '
|
33
|
+
spec.add_dependency 'snfoil-context', '>= 1.0.1', '< 2.0'
|
34
34
|
|
35
35
|
spec.add_development_dependency 'bundle-audit', '~> 0.1.0'
|
36
36
|
spec.add_development_dependency 'fasterer', '~> 0.10.0'
|
@@ -38,7 +38,7 @@ Gem::Specification.new do |spec|
|
|
38
38
|
spec.add_development_dependency 'pry-byebug', '~> 3.9'
|
39
39
|
spec.add_development_dependency 'rake', '~> 13.0'
|
40
40
|
spec.add_development_dependency 'rspec', '~> 3.10'
|
41
|
-
spec.add_development_dependency 'rubocop', '1.
|
42
|
-
spec.add_development_dependency 'rubocop-performance', '
|
43
|
-
spec.add_development_dependency 'rubocop-rspec', '
|
41
|
+
spec.add_development_dependency 'rubocop', '1.33'
|
42
|
+
spec.add_development_dependency 'rubocop-performance', '1.14.3'
|
43
|
+
spec.add_development_dependency 'rubocop-rspec', '2.12.1'
|
44
44
|
end
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: snfoil-controller
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Howes
|
8
8
|
- Cliff Campbell
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2022-
|
12
|
+
date: 2022-08-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -43,16 +43,22 @@ dependencies:
|
|
43
43
|
name: snfoil-context
|
44
44
|
requirement: !ruby/object:Gem::Requirement
|
45
45
|
requirements:
|
46
|
-
- - "
|
46
|
+
- - ">="
|
47
47
|
- !ruby/object:Gem::Version
|
48
|
-
version:
|
48
|
+
version: 1.0.1
|
49
|
+
- - "<"
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '2.0'
|
49
52
|
type: :runtime
|
50
53
|
prerelease: false
|
51
54
|
version_requirements: !ruby/object:Gem::Requirement
|
52
55
|
requirements:
|
53
|
-
- - "
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: 1.0.1
|
59
|
+
- - "<"
|
54
60
|
- !ruby/object:Gem::Version
|
55
|
-
version: '
|
61
|
+
version: '2.0'
|
56
62
|
- !ruby/object:Gem::Dependency
|
57
63
|
name: bundle-audit
|
58
64
|
requirement: !ruby/object:Gem::Requirement
|
@@ -143,42 +149,42 @@ dependencies:
|
|
143
149
|
requirements:
|
144
150
|
- - '='
|
145
151
|
- !ruby/object:Gem::Version
|
146
|
-
version: '1.
|
152
|
+
version: '1.33'
|
147
153
|
type: :development
|
148
154
|
prerelease: false
|
149
155
|
version_requirements: !ruby/object:Gem::Requirement
|
150
156
|
requirements:
|
151
157
|
- - '='
|
152
158
|
- !ruby/object:Gem::Version
|
153
|
-
version: '1.
|
159
|
+
version: '1.33'
|
154
160
|
- !ruby/object:Gem::Dependency
|
155
161
|
name: rubocop-performance
|
156
162
|
requirement: !ruby/object:Gem::Requirement
|
157
163
|
requirements:
|
158
|
-
- -
|
164
|
+
- - '='
|
159
165
|
- !ruby/object:Gem::Version
|
160
|
-
version:
|
166
|
+
version: 1.14.3
|
161
167
|
type: :development
|
162
168
|
prerelease: false
|
163
169
|
version_requirements: !ruby/object:Gem::Requirement
|
164
170
|
requirements:
|
165
|
-
- -
|
171
|
+
- - '='
|
166
172
|
- !ruby/object:Gem::Version
|
167
|
-
version:
|
173
|
+
version: 1.14.3
|
168
174
|
- !ruby/object:Gem::Dependency
|
169
175
|
name: rubocop-rspec
|
170
176
|
requirement: !ruby/object:Gem::Requirement
|
171
177
|
requirements:
|
172
|
-
- -
|
178
|
+
- - '='
|
173
179
|
- !ruby/object:Gem::Version
|
174
|
-
version:
|
180
|
+
version: 2.12.1
|
175
181
|
type: :development
|
176
182
|
prerelease: false
|
177
183
|
version_requirements: !ruby/object:Gem::Requirement
|
178
184
|
requirements:
|
179
|
-
- -
|
185
|
+
- - '='
|
180
186
|
- !ruby/object:Gem::Version
|
181
|
-
version:
|
187
|
+
version: 2.12.1
|
182
188
|
description: A context-like experience for your controllers
|
183
189
|
email:
|
184
190
|
- howeszy@gmail.com
|
@@ -206,7 +212,7 @@ metadata:
|
|
206
212
|
source_code_uri: https://github.com/limited-effort/snfoil-controller
|
207
213
|
changelog_uri: https://github.com/limited-effort/snfoil-controller/blob/main/CHANGELOG.md
|
208
214
|
rubygems_mfa_required: 'true'
|
209
|
-
post_install_message:
|
215
|
+
post_install_message:
|
210
216
|
rdoc_options: []
|
211
217
|
require_paths:
|
212
218
|
- lib
|
@@ -214,7 +220,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
214
220
|
requirements:
|
215
221
|
- - ">="
|
216
222
|
- !ruby/object:Gem::Version
|
217
|
-
version: '2.
|
223
|
+
version: '2.6'
|
218
224
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
219
225
|
requirements:
|
220
226
|
- - ">="
|
@@ -222,7 +228,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
222
228
|
version: '0'
|
223
229
|
requirements: []
|
224
230
|
rubygems_version: 3.1.6
|
225
|
-
signing_key:
|
231
|
+
signing_key:
|
226
232
|
specification_version: 4
|
227
233
|
summary: Seperate Display Logic from Business Logic
|
228
234
|
test_files: []
|