attribeauty 0.3.4 → 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: 04be2abe193a7faca2c172bb90713086e2a656bada649e0356ada8a444f54993
4
- data.tar.gz: e86ca7fb0fbc2d8e20679be174a98eced95c6facf3d98d2b3d1983961e99a9c8
3
+ metadata.gz: d41ac9aebcbb1c28b939ef7c8da19280dac7135655bc9b8fdab6758aad4a76e8
4
+ data.tar.gz: eaabafa87e2315bb7ea25debb7b915dfcc7e200c07b556274ee1aa73c4b4fbc3
5
5
  SHA512:
6
- metadata.gz: 1fe0d80aca7c85b8a34bf233f8b72133f16b75eea1a1800e8046efe5df1b19612a060662dc4c80bbbcffd0462d8cf2dee229a1c36228bba155dcff1058d814a7
7
- data.tar.gz: 42ab8159a2117c43956b84699f7ba9025a156e854e355f389424b810ecdfa90ee83f2fbdbdf71b43b650f723d1310288c18dc0f1e03f0f4765f193a8d58a1cc0
6
+ metadata.gz: 844f5020c04d41dc2d9ce9fc67e678b66a2cfb1722e98ac9dbee57a01d0bfd7269ac4f939194661e6fa87646cc363358f4e791efedfc04b40036bc447837a804
7
+ data.tar.gz: fcaf05b3f5f450862d664d5419e4375b3419d6d2e6b9ce625aa71517bc314f466eee3b713a7f931d93f5c5dbbb18998351059acd03ba90138f4d71161d1910cf
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
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
+
3
9
  ## [0.3.4] - 2024-06-24
4
10
 
5
11
  - tiny refactor
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- attribeauty (0.3.4)
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)
@@ -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.4"
4
+ VERSION = "0.4.0"
5
5
  end
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.3.4
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toby