attribeauty 0.4.2 → 0.4.4

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: b78499787a41322da17eca60e3a8af5c5d55b081aaefc6eb8bef788e60368c78
4
- data.tar.gz: e660efa7ce1508a9d322256b7a3e598bce5e1e8a778dc7da2a39fa58166cfe20
3
+ metadata.gz: cea80fa1b1b1eedce5673a87e9a4a34e65e897d4f1f1756d044a8ee37c4d961e
4
+ data.tar.gz: 798d5bab20443753941eebb76810a667700e09e70b9c82019e2a20ec2a64a2d5
5
5
  SHA512:
6
- metadata.gz: 06471cfb5f5f3f76dcc9bf09545a7c976577b893c08562aa2ecea98f42a3a13c0be5df26ca00e187f81eccaf33728ae41cf5b90226df9c29f113faa550865b21
7
- data.tar.gz: 3ec534f72b7977af852f649489a7b8265d5a055171f0130c25e8887e796368bd1998a40c747bfdd7bff413c5acc184c46fc331f14b4b091ded2fc9785503f141
6
+ metadata.gz: 232c14b463c2f6fcffc213079b0df03c61078f6b3f4c680784a4613c4025fb36249d87f17c5fc144749863cb0baa3af8ff1fa08d04d3ec399286f4c3c5e31175
7
+ data.tar.gz: e2e7426248fdd68bc46c5efb5b3aea9d4841e6012c92ed105e1b1c7d7db7965025acf0622945dc109dc20f257564593663bb67588ade867274200c0bd09901ac
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.4] - 2024-06-26
4
+
5
+ - get `default:` working. Not expecting to use them since db will handle that, but nice to have?
6
+
7
+ ## [0.4.3] - 2024-06-25
8
+
9
+ - allow certain values with `allow` option
10
+
3
11
  ## [0.4.2] - 2024-06-25
4
12
 
5
13
  - getting and setting vals in params
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- attribeauty (0.4.2)
4
+ attribeauty (0.4.4)
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
@@ -49,7 +49,6 @@ module Attribeauty
49
49
  yield
50
50
  end
51
51
 
52
- #
53
52
  def attribute(name, type = nil, **args, &block)
54
53
  value = request_params[name]
55
54
  return hash_from_nested(name, value, &block) if block_given?
@@ -79,7 +78,7 @@ module Attribeauty
79
78
  end
80
79
 
81
80
  def hash_from_nested(name, value, &block)
82
- result =
81
+ result =
83
82
  if value.is_a?(Array)
84
83
  value.map do |val|
85
84
  params = self.class.with(val).accept(**default_args, &block)
@@ -90,7 +89,7 @@ module Attribeauty
90
89
  params = self.class.with(value).accept(**default_args, &block)
91
90
  @errors.push(*params.errors)
92
91
  params.to_h
93
- end
92
+ end
94
93
 
95
94
  @to_h[name.to_sym] = result unless result.empty?
96
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,22 @@ 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
+ return false unless original_val.nil? && default.nil?
97
+
98
+ original_val.nil? && !required?
99
+ end
100
+
101
+ def allows_array
102
+ [*allows].flatten.compact
103
+ end
104
+
105
+ def excludes_array
106
+ [*excludes].flatten
82
107
  end
83
108
  end
84
109
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Attribeauty
4
- VERSION = "0.4.2"
4
+ VERSION = "0.4.4"
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.2
4
+ version: 0.4.4
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: