attribeauty 0.4.0 → 0.4.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: d41ac9aebcbb1c28b939ef7c8da19280dac7135655bc9b8fdab6758aad4a76e8
4
- data.tar.gz: eaabafa87e2315bb7ea25debb7b915dfcc7e200c07b556274ee1aa73c4b4fbc3
3
+ metadata.gz: a594dd2b01526d1b89bea9ceeea3217bf0cb311b236981be2e77bd03dc1b5b2f
4
+ data.tar.gz: 4f79df9cca230c2c0c29c34746f381818535095383c54902e8a7b90f8ea205de
5
5
  SHA512:
6
- metadata.gz: 844f5020c04d41dc2d9ce9fc67e678b66a2cfb1722e98ac9dbee57a01d0bfd7269ac4f939194661e6fa87646cc363358f4e791efedfc04b40036bc447837a804
7
- data.tar.gz: fcaf05b3f5f450862d664d5419e4375b3419d6d2e6b9ce625aa71517bc314f466eee3b713a7f931d93f5c5dbbb18998351059acd03ba90138f4d71161d1910cf
6
+ metadata.gz: 4ab16692f1f34715726b3777889c5ee4f1797eeff0431ce73a116c7e02c5faac34d0453188f2469938b1c98076e172dd37e5b18a5a2c1bff009bb483c067a984
7
+ data.tar.gz: 78f5e064bc51b85fbb57877ce9bd9a7d3894f7798fdfd7dcfe0dc7989fad2618f3d0358e22467d02351745710b92e81227f6b243a74254e90d377be474b1655a
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.4.1] - 2024-06-25
4
+
5
+ - Global default_args in accept, to apply your conditions to all attributes
6
+
3
7
  ## [0.4.0] - 2024-06-25
4
8
 
5
9
  - Breaking change:
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- attribeauty (0.4.0)
4
+ attribeauty (0.4.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -191,6 +191,43 @@ end
191
191
 
192
192
  ```
193
193
 
194
+ what if you want to require all attributes? If you pass the `required: true` or `exclude_if: :nil?` with the `accept`, it will be applied to all attributes.
195
+
196
+ ```
197
+ class MyController
198
+ def update
199
+ MyRecord.update(update_params)
200
+
201
+ redirect_to index_path
202
+ end
203
+
204
+ private
205
+
206
+ # your params look like this:
207
+ # { user: { profile: [{ address: { street_name: "Main St" } }] } }
208
+ #
209
+ def params_filter
210
+ Attribeauty::Params.with(request.params)
211
+ end
212
+
213
+ # exclude_if and required will be passed onto all attributes
214
+ #
215
+ def update_params
216
+ params_filter.accept exclude_if: :nil?, required: true do
217
+ container :user do
218
+ attribute :title, :string,
219
+ attribute :email do
220
+ attribute :address, :string
221
+ attribute :valid, :boolean
222
+ attribute :ip_address, :string
223
+ end
224
+ end
225
+ end
226
+ end
227
+ end
228
+
229
+ ```
230
+
194
231
  See `test/test_params.rb` for more examples
195
232
 
196
233
 
@@ -10,15 +10,16 @@ module Attribeauty
10
10
  new(request_params)
11
11
  end
12
12
 
13
- attr_reader :prefix, :request_params, :acceptables, :to_h, :errors, :strict
13
+ attr_reader :prefix, :request_params, :acceptables, :to_h, :errors, :strict, :default_args
14
14
 
15
15
  def initialize(request_params)
16
- @request_params = request_params.transform_keys(&:to_sym)
16
+ @request_params = (request_params || {}).transform_keys(&:to_sym)
17
17
  @to_h = {}
18
18
  @errors = []
19
19
  end
20
20
 
21
- def accept(&)
21
+ def accept(**args, &)
22
+ @default_args = args
22
23
  instance_eval(&)
23
24
 
24
25
  raise MissingAttributeError, errors.join(", ") if errors.any? && strict?
@@ -26,10 +27,10 @@ module Attribeauty
26
27
  self
27
28
  end
28
29
 
29
- def accept!(&)
30
+ def accept!(**args, &)
30
31
  @strict = true
31
32
 
32
- accept(&)
33
+ accept(**args, &)
33
34
  end
34
35
 
35
36
  def to_hash = to_h
@@ -47,7 +48,6 @@ module Attribeauty
47
48
  #
48
49
  def attribute(name, type = nil, **args, &block)
49
50
  value = request_params[name]
50
- return if required?(value, **args)
51
51
  return hash_from_nested(name, value, &block) if block_given?
52
52
 
53
53
  value_from_validator(name, value, type, **args)
@@ -67,29 +67,28 @@ module Attribeauty
67
67
 
68
68
  private
69
69
 
70
- def required?(value, **args)
71
- value.nil? && args[:required].nil?
72
- end
73
-
74
70
  def value_from_validator(name, value, type, **args)
75
- validator = Validator.run(name, value, type, **args)
71
+ merged_args = args.merge(default_args || {})
72
+ validator = Validator.run(name, value, type, **merged_args)
76
73
  @errors.push(*validator.errors)
77
74
  @to_h[name.to_sym] = validator.value if validator.valid?
78
75
  end
79
76
 
80
77
  def hash_from_nested(name, value, &block)
81
- @to_h[name.to_sym] =
78
+ result =
82
79
  if value.is_a?(Array)
83
80
  value.map do |val|
84
- params = self.class.with(val).accept(&block)
81
+ params = self.class.with(val).accept(**default_args, &block)
85
82
  @errors.push(*params.errors)
86
83
  params.to_h
87
84
  end.reject(&:empty?)
88
85
  else
89
- params = self.class.with(value).accept(&block)
86
+ params = self.class.with(value).accept(**default_args, &block)
90
87
  @errors.push(*params.errors)
91
88
  params.to_h
92
89
  end
90
+
91
+ @to_h[name.to_sym] = result unless result.empty?
93
92
  end
94
93
  end
95
94
  end
@@ -13,7 +13,8 @@ module Attribeauty
13
13
  @type = type
14
14
  @original_val = original_val
15
15
  @default = args[:default]
16
- @required = args[:required] if args[:required] == true
16
+ args.delete_if { |key, value| key == :required && value == false }
17
+ @required = args[:required]
17
18
  @excludes = args[:exclude_if]
18
19
 
19
20
  @valid = true
@@ -21,15 +22,16 @@ module Attribeauty
21
22
  end
22
23
 
23
24
  def run
24
- if type.nil?
25
- @value = original_val
26
- else
27
- set_default
28
- cast_value
29
- handle_missing_required
30
- handle_excludes
31
- end
25
+ handle_missing_original_val!
26
+ handle_missing_required!
27
+ handle_missing_type
32
28
 
29
+ set_default
30
+ cast_value
31
+ handle_excludes
32
+
33
+ self
34
+ rescue ValueInvalidError
33
35
  self
34
36
  end
35
37
 
@@ -37,24 +39,40 @@ module Attribeauty
37
39
  valid
38
40
  end
39
41
 
40
- private
42
+ def required?
43
+ required
44
+ end
41
45
 
42
- def set_default
43
- return unless original_val.nil? && !default.nil?
46
+ private
44
47
 
45
- @original_val = default
46
- end
48
+ def handle_missing_original_val!
49
+ return unless !required? && original_val.nil?
47
50
 
48
- def cast_value
49
- @value = TypeCaster.run(original_val, type)
51
+ @valid = false
52
+ raise ValueInvalidError
50
53
  end
51
54
 
52
55
  # only returning errors if required is missing, not if nil?, or :empty?
53
- def handle_missing_required
56
+ def handle_missing_required!
54
57
  return unless required? && original_val.nil?
55
58
 
56
59
  errors << "#{name} required"
57
60
  @valid = false
61
+ raise ValueInvalidError
62
+ end
63
+
64
+ def handle_missing_type
65
+ @value = original_val if type.nil?
66
+ end
67
+
68
+ def set_default
69
+ return unless original_val.nil? && !default.nil?
70
+
71
+ @original_val = default
72
+ end
73
+
74
+ def cast_value
75
+ @value ||= TypeCaster.run(original_val, type)
58
76
  end
59
77
 
60
78
  def handle_excludes
@@ -62,9 +80,5 @@ module Attribeauty
62
80
 
63
81
  @valid = ![*excludes].flatten.any? { |exclude| value.public_send(exclude) }
64
82
  end
65
-
66
- def required?
67
- required
68
- end
69
83
  end
70
84
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Attribeauty
4
- VERSION = "0.4.0"
4
+ VERSION = "0.4.1"
5
5
  end
data/lib/attribeauty.rb CHANGED
@@ -8,6 +8,7 @@ require "forwardable"
8
8
  module Attribeauty
9
9
  class Error < StandardError; end
10
10
  class MissingAttributeError < StandardError; end
11
+ class ValueInvalidError < StandardError; end
11
12
 
12
13
  class << self
13
14
  def configuration
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attribeauty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toby