attribeauty 0.4.1 → 0.4.3

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: a594dd2b01526d1b89bea9ceeea3217bf0cb311b236981be2e77bd03dc1b5b2f
4
- data.tar.gz: 4f79df9cca230c2c0c29c34746f381818535095383c54902e8a7b90f8ea205de
3
+ metadata.gz: 6815673fc0f4d65774d12f0dff065883a9243bb8495c3379f2adc293cbc2f988
4
+ data.tar.gz: 5201b83d32ada2367130f5c7de336c012b8c157a4aa9331db1acca883407d4df
5
5
  SHA512:
6
- metadata.gz: 4ab16692f1f34715726b3777889c5ee4f1797eeff0431ce73a116c7e02c5faac34d0453188f2469938b1c98076e172dd37e5b18a5a2c1bff009bb483c067a984
7
- data.tar.gz: 78f5e064bc51b85fbb57877ce9bd9a7d3894f7798fdfd7dcfe0dc7989fad2618f3d0358e22467d02351745710b92e81227f6b243a74254e90d377be474b1655a
6
+ metadata.gz: dd30d11344376e152a20d8723c977b1434db36111be25ccfda3bbad20206da75d28d4c7c02cf71c05f15a0912ebbcf3102c5a07bc8ae19031629e281d850ee21
7
+ data.tar.gz: f063c90c743d67436000eb357b6b3868a92f790bff1c80c8638e7ba849b9d7a452416970053329106d06b648efe8143c51dc56a5e2953ae960b67217662cde19
data/.rubocop.yml CHANGED
@@ -24,11 +24,17 @@ Metrics/AbcSize:
24
24
  Metrics/ClassLength:
25
25
  Enabled: false
26
26
 
27
+ Metrics/MethodLength:
28
+ Enabled: false
29
+
27
30
  Metrics/PerceivedComplexity:
28
31
  Enabled: false
29
32
 
30
33
  Metrics/CyclomaticComplexity:
31
34
  Enabled: false
32
35
 
33
- Naming::BlockForwarding:
36
+ Naming/BlockForwarding:
37
+ Enabled: false
38
+
39
+ Naming/MemoizedInstanceVariableName:
34
40
  Enabled: false
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.4.3] - 2024-06-25
4
+
5
+ - allow certain values with `allow` option
6
+
7
+ ## [0.4.2] - 2024-06-25
8
+
9
+ - getting and setting vals in params
10
+
3
11
  ## [0.4.1] - 2024-06-25
4
12
 
5
13
  - Global default_args in accept, to apply your conditions to all attributes
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- attribeauty (0.4.1)
4
+ attribeauty (0.4.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -192,6 +192,7 @@ end
192
192
  ```
193
193
 
194
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
+ You can also exclude a value from this by useing the `allows` option.
195
196
 
196
197
  ```
197
198
  class MyController
@@ -219,7 +220,7 @@ class MyController
219
220
  attribute :email do
220
221
  attribute :address, :string
221
222
  attribute :valid, :boolean
222
- attribute :ip_address, :string
223
+ attribute :ip_address, :string, allows: :nil?
223
224
  end
224
225
  end
225
226
  end
@@ -39,13 +39,16 @@ module Attribeauty
39
39
  to_h[key]
40
40
  end
41
41
 
42
+ def []=(key, value)
43
+ to_h[key] = value
44
+ end
45
+
42
46
  def container(name)
43
47
  @request_params = request_params[name]
44
48
 
45
49
  yield
46
50
  end
47
51
 
48
- #
49
52
  def attribute(name, type = nil, **args, &block)
50
53
  value = request_params[name]
51
54
  return hash_from_nested(name, value, &block) if block_given?
@@ -75,7 +78,7 @@ module Attribeauty
75
78
  end
76
79
 
77
80
  def hash_from_nested(name, value, &block)
78
- result =
81
+ result =
79
82
  if value.is_a?(Array)
80
83
  value.map do |val|
81
84
  params = self.class.with(val).accept(**default_args, &block)
@@ -86,7 +89,7 @@ module Attribeauty
86
89
  params = self.class.with(value).accept(**default_args, &block)
87
90
  @errors.push(*params.errors)
88
91
  params.to_h
89
- end
92
+ end
90
93
 
91
94
  @to_h[name.to_sym] = result unless result.empty?
92
95
  end
@@ -6,7 +6,8 @@ module Attribeauty
6
6
  new(name, type, original_val, **args).run
7
7
  end
8
8
 
9
- attr_reader :original_val, :errors, :name, :type, :required, :default, :excludes, :value, :valid
9
+ attr_reader :original_val, :errors, :name, :type, :required,
10
+ :default, :excludes, :value, :valid, :allows
10
11
 
11
12
  def initialize(name, original_val, type = nil, **args)
12
13
  @name = name
@@ -15,6 +16,7 @@ module Attribeauty
15
16
  @default = args[:default]
16
17
  args.delete_if { |key, value| key == :required && value == false }
17
18
  @required = args[:required]
19
+ @allows = args[:allow]
18
20
  @excludes = args[:exclude_if]
19
21
 
20
22
  @valid = true
@@ -22,10 +24,11 @@ module Attribeauty
22
24
  end
23
25
 
24
26
  def run
27
+ handle_allows
25
28
  handle_missing_original_val!
26
29
  handle_missing_required!
27
- handle_missing_type
28
30
 
31
+ handle_missing_type
29
32
  set_default
30
33
  cast_value
31
34
  handle_excludes
@@ -45,10 +48,17 @@ module Attribeauty
45
48
 
46
49
  private
47
50
 
51
+ def handle_allows
52
+ return if allows.nil?
53
+
54
+ @required = false if allows_array.include?(:nil?)
55
+ @excludes = excludes_array - allows_array
56
+ end
57
+
48
58
  def handle_missing_original_val!
49
- return unless !required? && original_val.nil?
59
+ return unless exclude_nil?
50
60
 
51
- @valid = false
61
+ @valid = false
52
62
  raise ValueInvalidError
53
63
  end
54
64
 
@@ -78,7 +88,21 @@ module Attribeauty
78
88
  def handle_excludes
79
89
  return if excludes.nil? || !valid?
80
90
 
81
- @valid = ![*excludes].flatten.any? { |exclude| value.public_send(exclude) }
91
+ @valid = excludes_array.none? { |exclude| value.public_send(exclude) }
92
+ end
93
+
94
+ def exclude_nil?
95
+ return false if allows_array.include?(:nil?)
96
+
97
+ !required? && original_val.nil?
98
+ end
99
+
100
+ def allows_array
101
+ [*allows].flatten.compact
102
+ end
103
+
104
+ def excludes_array
105
+ [*excludes].flatten
82
106
  end
83
107
  end
84
108
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Attribeauty
4
- VERSION = "0.4.1"
4
+ VERSION = "0.4.3"
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.4.1
4
+ version: 0.4.3
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-25 00:00:00.000000000 Z
11
+ date: 2024-06-26 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: