attribeauty 0.3.3 → 0.4.0

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: 812191110b6c7f397737c3a9b1cceab66819c07d549c02c31687b8260342f404
4
- data.tar.gz: 2ce7d61531ba7dcb7a896994ef94a2fa970bd821eb62d0901cc2b1df5e70947b
3
+ metadata.gz: d41ac9aebcbb1c28b939ef7c8da19280dac7135655bc9b8fdab6758aad4a76e8
4
+ data.tar.gz: eaabafa87e2315bb7ea25debb7b915dfcc7e200c07b556274ee1aa73c4b4fbc3
5
5
  SHA512:
6
- metadata.gz: dbe0d664e43e995eb4caf66f66fe2a242d2f40b9effda38f511775451c666c82e1596a9116197e5cc84848f0f63ab11f2bb0f537f2214e0d38c6f9b3ddba2e1f
7
- data.tar.gz: 66fb433c37a8986c4f5cf3bec0dae55246360fb6f65d82e2e25d1f507b3f4526d5476c99fa7f3e5244d6d255cdb9f429c51e7a7c7c84504e61fe2083e864bf0b
6
+ metadata.gz: 844f5020c04d41dc2d9ce9fc67e678b66a2cfb1722e98ac9dbee57a01d0bfd7269ac4f939194661e6fa87646cc363358f4e791efedfc04b40036bc447837a804
7
+ data.tar.gz: fcaf05b3f5f450862d664d5419e4375b3419d6d2e6b9ce625aa71517bc314f466eee3b713a7f931d93f5c5dbbb18998351059acd03ba90138f4d71161d1910cf
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.4.0] - 2024-06-25
4
+
5
+ - Breaking change:
6
+ - `:allow_nil`, and `:allow_empty` have been replaced with `:exclude_if`
7
+ - see readme for details
8
+
9
+ ## [0.3.4] - 2024-06-24
10
+
11
+ - tiny refactor
12
+
3
13
  ## [0.3.3] - 2024-06-24
4
14
 
5
15
  - handle nil in types. Big refactor
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- attribeauty (0.3.3)
4
+ attribeauty (0.4.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -103,17 +103,18 @@ class MyController
103
103
 
104
104
  def update_params
105
105
  params_filter.accept do
106
- attribute :title, :string, allow_nil: false, required: true
106
+ attribute :title, :string, required: true
107
107
  attribute :email do
108
- attribute :address, :string, allow_empty: false
109
- attribute :valid, :boolean, allow_nil: false
110
- attribute :ip_address, :string, allow_blank: true
108
+ attribute :address, :string
109
+ attribute :valid, :boolean
110
+ attribute :ip_address, :string, exclude_if: :empty?
111
111
  end
112
112
  end
113
113
  end
114
114
  ```
115
115
 
116
- If you have a "head" param, like in rails, you can exclude it like this:
116
+ If you have a "head" param, like in rails, you can exclude it, also note the `exclude_if` option, this will exclude the value completely, if it evaluates to true.
117
+ `exclude_if` will accept a single method call (`:nil?`) or an array (`[:nil?, :empty?]`)
117
118
 
118
119
  ```
119
120
  class MyController
@@ -126,7 +127,7 @@ class MyController
126
127
  private
127
128
 
128
129
  # your params look like this:
129
- # { user: { title: "woo", email: { address: "hmm@yep.com" } } }
130
+ # { user: { title: "woo", email: { address: "hmm@yep.com", ip_address: "" } } }
130
131
  #
131
132
  def params_filter
132
133
  Attribeauty::Params.with(request.params)
@@ -138,11 +139,11 @@ class MyController
138
139
  def update_params
139
140
  params_filter.accept do
140
141
  container :user do
141
- attribute :title, :string, allow_nil: false, required: true
142
+ attribute :title, :string, required: true
142
143
  attribute :email do
143
- attribute :address, :string, allow_empty: false
144
- attribute :valid, :boolean, allow_nil: false
145
- attribute :ip_address, :string, allow_blank: true
144
+ attribute :address, :string, exclude_if: [:empty?, :nil?]
145
+ attribute :valid, :boolean
146
+ attribute :ip_address, :string, exclude_if: :empty?
146
147
  end
147
148
  end
148
149
  end
@@ -177,11 +178,11 @@ class MyController
177
178
  def update_params
178
179
  params_filter.accept! do
179
180
  container :user do
180
- attribute :title, :string, allow_nil: false, required: true
181
+ attribute :title, :string, exclude_if: :nil?, required: true
181
182
  attribute :email do
182
- attribute :address, :string, allow_empty: false
183
- attribute :valid, :boolean, allow_nil: false
184
- attribute :ip_address, :string, allow_blank: true
183
+ attribute :address, :string
184
+ attribute :valid, :boolean
185
+ attribute :ip_address, :string
185
186
  end
186
187
  end
187
188
  end
@@ -10,7 +10,7 @@ module Attribeauty
10
10
  new(request_params)
11
11
  end
12
12
 
13
- attr_reader :allow_nil, :prefix, :request_params, :acceptables, :to_h, :errors, :strict
13
+ attr_reader :prefix, :request_params, :acceptables, :to_h, :errors, :strict
14
14
 
15
15
  def initialize(request_params)
16
16
  @request_params = request_params.transform_keys(&:to_sym)
@@ -44,17 +44,13 @@ module Attribeauty
44
44
  yield
45
45
  end
46
46
 
47
+ #
47
48
  def attribute(name, type = nil, **args, &block)
48
49
  value = request_params[name]
49
- return if value.nil? && args[:required].nil?
50
-
51
- if block_given?
52
- @to_h[name.to_sym] = vals_from_nested(value, &block)
53
- else
54
- validator = Validator.run(name, value, type, **args)
55
- @errors.push(*validator.errors)
56
- @to_h[name.to_sym] = validator.value if validator.valid?
57
- end
50
+ return if required?(value, **args)
51
+ return hash_from_nested(name, value, &block) if block_given?
52
+
53
+ value_from_validator(name, value, type, **args)
58
54
  end
59
55
 
60
56
  def inspect
@@ -71,17 +67,28 @@ module Attribeauty
71
67
 
72
68
  private
73
69
 
74
- def vals_from_nested(value, &block)
75
- if value.is_a?(Array)
76
- value.map do |val|
77
- params = self.class.with(val).accept(&block)
70
+ def required?(value, **args)
71
+ value.nil? && args[:required].nil?
72
+ end
73
+
74
+ def value_from_validator(name, value, type, **args)
75
+ validator = Validator.run(name, value, type, **args)
76
+ @errors.push(*validator.errors)
77
+ @to_h[name.to_sym] = validator.value if validator.valid?
78
+ end
79
+
80
+ def hash_from_nested(name, value, &block)
81
+ @to_h[name.to_sym] =
82
+ if value.is_a?(Array)
83
+ value.map do |val|
84
+ params = self.class.with(val).accept(&block)
85
+ @errors.push(*params.errors)
86
+ params.to_h
87
+ end.reject(&:empty?)
88
+ else
89
+ params = self.class.with(value).accept(&block)
78
90
  @errors.push(*params.errors)
79
91
  params.to_h
80
- end.reject(&:empty?)
81
- else
82
- params = self.class.with(value).accept(&block)
83
- @errors.push(*params.errors)
84
- params.to_h
85
92
  end
86
93
  end
87
94
  end
@@ -2,16 +2,11 @@
2
2
 
3
3
  module Attribeauty
4
4
  class Validator
5
- ALLOWS_HASH = {
6
- allow_nil: :nil?,
7
- allow_empty: :empty?
8
- }.freeze
9
-
10
5
  def self.run(name, type, original_val, **args)
11
6
  new(name, type, original_val, **args).run
12
7
  end
13
8
 
14
- attr_reader :original_val, :errors, :name, :type, :required, :default, :allows, :value, :valid
9
+ attr_reader :original_val, :errors, :name, :type, :required, :default, :excludes, :value, :valid
15
10
 
16
11
  def initialize(name, original_val, type = nil, **args)
17
12
  @name = name
@@ -19,7 +14,7 @@ module Attribeauty
19
14
  @original_val = original_val
20
15
  @default = args[:default]
21
16
  @required = args[:required] if args[:required] == true
22
- @allows = args.slice(*allows_array).delete_if { |_key, value| value == true }
17
+ @excludes = args[:exclude_if]
23
18
 
24
19
  @valid = true
25
20
  @errors = []
@@ -32,7 +27,7 @@ module Attribeauty
32
27
  set_default
33
28
  cast_value
34
29
  handle_missing_required
35
- handle_predicates
30
+ handle_excludes
36
31
  end
37
32
 
38
33
  self
@@ -62,25 +57,10 @@ module Attribeauty
62
57
  @valid = false
63
58
  end
64
59
 
65
- def handle_predicates
66
- return if predicate.nil? || !valid?
67
-
68
- @valid = !value.public_send(predicate)
69
- end
70
-
71
- def allows_array
72
- ALLOWS_HASH.keys
73
- end
74
-
75
- # convert allow_nil -> :nil? or allow_empty -> :empty?
76
- # this will be used to public_send
77
- # NOTE: only one will be checked, if you pass both:
78
- # allow_nil and allow_empty, one will be ignored
79
- def predicate
80
- return if allows.empty?
60
+ def handle_excludes
61
+ return if excludes.nil? || !valid?
81
62
 
82
- key = allows.keys.first
83
- ALLOWS_HASH[key]
63
+ @valid = ![*excludes].flatten.any? { |exclude| value.public_send(exclude) }
84
64
  end
85
65
 
86
66
  def required?
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Attribeauty
4
- VERSION = "0.3.3"
4
+ VERSION = "0.4.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attribeauty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toby
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-06-24 00:00:00.000000000 Z
11
+ date: 2024-06-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: There are so many of these, I just needed this one.
14
14
  email: