snfoil-controller 1.0.0 → 1.1.1

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: bd05e7dcc970820561431b9d85d19a5fd3420d00bc726b80adb26fdd01248d5b
4
- data.tar.gz: 6f7a84286eedb8bbf29efae538192239df2f2ca96d6aa3ae7ed7ee8e1f1703ff
3
+ metadata.gz: c972e74246450b38ea6f71e0acb425565f85d5a4f2304a6832246b79eb0e0313
4
+ data.tar.gz: 4d048b56d294d390bd8c3dd71e5f3a52dafacc2e3392b51aa29e0eb788c43ba4
5
5
  SHA512:
6
- metadata.gz: '08be4e5d500fbaa0b2ee35b8937aac5e6ab427c20df897272241679c24c2eb0a5e3172c38c25c6d3e53c195584a3520764f6814d89d1fa1d350690f263b46a76'
7
- data.tar.gz: 80e7866dcc05d23c35740ccb0eb8776a5e9828ccf63c329f574df40c87665e4469a1c8df6aebc34ed968f7ab7b9614337e99e34e0a0641de5ff8e37ba2ab9ce8
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.
@@ -16,6 +16,6 @@
16
16
 
17
17
  module SnFoil
18
18
  module Controller
19
- VERSION = '1.0.0'
19
+ VERSION = '1.1.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(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
- 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
@@ -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.7'
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', '~> 1.0'
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.29'
42
- spec.add_development_dependency 'rubocop-performance', '~> 1.11'
43
- spec.add_development_dependency 'rubocop-rspec', '~> 2.5'
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.0.0
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-05-11 00:00:00.000000000 Z
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: '1.0'
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: '1.0'
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.29'
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.29'
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: '1.11'
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: '1.11'
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: '2.5'
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: '2.5'
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.7'
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: []