attribeauty 0.3.2 → 0.3.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: 16789f41e6c0cfea21c44f2f71fc3fd0c9664791ed8d1c97648834be86db6e92
4
- data.tar.gz: e8c5ac4397043090870ffeacc5f4b1ae0565f0b7e85237a47254a41e31258032
3
+ metadata.gz: 812191110b6c7f397737c3a9b1cceab66819c07d549c02c31687b8260342f404
4
+ data.tar.gz: 2ce7d61531ba7dcb7a896994ef94a2fa970bd821eb62d0901cc2b1df5e70947b
5
5
  SHA512:
6
- metadata.gz: 06c7ca8c0f104cc00ac9854fd4d5c31c043daf65c4d993c32da3dc0472ce4be296ebc2640e2b8b759fb6d99ba07d54e4a8b3c251c183da2048b22eb7da23af78
7
- data.tar.gz: 13eb48b958c5cfd1da319a33326449e86683b90f9c89631964c4397d9447334a025b7b3647c87f40ad8c11465c51af7f73e32319ad0c9e344757d823f58e19a8
6
+ metadata.gz: dbe0d664e43e995eb4caf66f66fe2a242d2f40b9effda38f511775451c666c82e1596a9116197e5cc84848f0f63ab11f2bb0f537f2214e0d38c6f9b3ddba2e1f
7
+ data.tar.gz: 66fb433c37a8986c4f5cf3bec0dae55246360fb6f65d82e2e25d1f507b3f4526d5476c99fa7f3e5244d6d255cdb9f429c51e7a7c7c84504e61fe2083e864bf0b
data/.rubocop.yml CHANGED
@@ -29,3 +29,6 @@ Metrics/PerceivedComplexity:
29
29
 
30
30
  Metrics/CyclomaticComplexity:
31
31
  Enabled: false
32
+
33
+ Naming::BlockForwarding:
34
+ Enabled: false
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.3] - 2024-06-24
4
+
5
+ - handle nil in types. Big refactor
6
+
3
7
  ## [0.3.2] - 2024-06-24
4
8
 
5
9
  - added accept! method to raise error if "required's" are missing
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- attribeauty (0.3.2)
4
+ attribeauty (0.3.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -44,31 +44,18 @@ module Attribeauty
44
44
  yield
45
45
  end
46
46
 
47
- # rubocop:disable Naming::BlockForwarding
48
47
  def attribute(name, type = nil, **args, &block)
49
48
  value = request_params[name]
50
49
  return if value.nil? && args[:required].nil?
51
50
 
52
51
  if block_given?
53
- @to_h[name] =
54
- if value.is_a?(Array)
55
- value.map do |val|
56
- params = self.class.with(val).accept(&block)
57
- @errors.push(*params.errors)
58
- params
59
- end.reject(&:empty?)
60
- else
61
- params = self.class.with(value).accept(&block)
62
- @errors.push(*params.errors)
63
- params
64
- end
52
+ @to_h[name.to_sym] = vals_from_nested(value, &block)
65
53
  else
66
- validator = Validator.run(name, type, value, **args)
67
- @to_h[name.to_sym] = validator.value if validator.valid?
54
+ validator = Validator.run(name, value, type, **args)
68
55
  @errors.push(*validator.errors)
56
+ @to_h[name.to_sym] = validator.value if validator.valid?
69
57
  end
70
58
  end
71
- # rubocop:enable Naming::BlockForwarding
72
59
 
73
60
  def inspect
74
61
  to_h.inspect
@@ -81,5 +68,21 @@ module Attribeauty
81
68
  def strict?
82
69
  strict
83
70
  end
71
+
72
+ private
73
+
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)
78
+ @errors.push(*params.errors)
79
+ 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
+ end
86
+ end
84
87
  end
85
88
  end
@@ -16,6 +16,8 @@ module Attribeauty
16
16
  ].to_set.freeze
17
17
 
18
18
  def cast(value)
19
+ return false if value.nil?
20
+
19
21
  !FALSE_VALUES.include?(value)
20
22
  end
21
23
  end
@@ -5,6 +5,8 @@ module Attribeauty
5
5
  # custom float type
6
6
  class Float
7
7
  def cast(value)
8
+ return if value.nil?
9
+
8
10
  Float(value)
9
11
  end
10
12
  end
@@ -5,6 +5,8 @@ module Attribeauty
5
5
  # custom integer type
6
6
  class Integer
7
7
  def cast(value)
8
+ return if value.nil?
9
+
8
10
  Integer(value)
9
11
  end
10
12
  end
@@ -5,6 +5,8 @@ module Attribeauty
5
5
  # custom string type
6
6
  class String
7
7
  def cast(value)
8
+ return if value.nil?
9
+
8
10
  String(value)
9
11
  end
10
12
  end
@@ -5,6 +5,8 @@ module Attribeauty
5
5
  # custom Time type
6
6
  class Time
7
7
  def cast(value)
8
+ return if value.nil?
9
+
8
10
  case value
9
11
  when Time
10
12
  value
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "forwardable"
4
-
5
3
  module Attribeauty
6
4
  class Validator
7
5
  ALLOWS_HASH = {
@@ -9,55 +7,82 @@ module Attribeauty
9
7
  allow_empty: :empty?
10
8
  }.freeze
11
9
 
12
- def self.run(name, type, original_value, **args)
13
- new(name, type, original_value, **args).run
10
+ def self.run(name, type, original_val, **args)
11
+ new(name, type, original_val, **args).run
14
12
  end
15
13
 
16
- attr_reader :original_value, :errors, :name, :type, :required, :default, :predicate, :value
14
+ attr_reader :original_val, :errors, :name, :type, :required, :default, :allows, :value, :valid
17
15
 
18
- def initialize(name, type, original_value, **args)
16
+ def initialize(name, original_val, type = nil, **args)
19
17
  @name = name
20
18
  @type = type
21
- @original_value = original_value
22
- @errors = []
19
+ @original_val = original_val
23
20
  @default = args[:default]
24
- @required = args[:required] if [true, false].include?(args[:required])
25
- allows = args.slice(*allows_array)
26
- return if allows.empty?
21
+ @required = args[:required] if args[:required] == true
22
+ @allows = args.slice(*allows_array).delete_if { |_key, value| value == true }
27
23
 
28
- predicate_array = allows.first
29
- predicate_array[0] = :"#{ALLOWS_HASH[predicate_array[0]]}"
30
- @predicate = predicate_array
24
+ @valid = true
25
+ @errors = []
31
26
  end
32
27
 
33
28
  def run
34
- @original_value = default if original_value.nil? && !default.nil?
35
- @value = TypeCaster.run(original_value, type)
29
+ if type.nil?
30
+ @value = original_val
31
+ else
32
+ set_default
33
+ cast_value
34
+ handle_missing_required
35
+ handle_predicates
36
+ end
36
37
 
37
38
  self
38
39
  end
39
40
 
40
41
  def valid?
41
- if required? && original_value.nil?
42
- errors << "#{name} required"
43
- return false
44
- end
45
- return true if predicate.nil?
42
+ valid
43
+ end
46
44
 
47
- method, bool = predicate
48
- return true if bool
45
+ private
49
46
 
50
- !value.public_send(method)
47
+ def set_default
48
+ return unless original_val.nil? && !default.nil?
49
+
50
+ @original_val = default
51
51
  end
52
52
 
53
- private
53
+ def cast_value
54
+ @value = TypeCaster.run(original_val, type)
55
+ end
56
+
57
+ # only returning errors if required is missing, not if nil?, or :empty?
58
+ def handle_missing_required
59
+ return unless required? && original_val.nil?
60
+
61
+ errors << "#{name} required"
62
+ @valid = false
63
+ end
64
+
65
+ def handle_predicates
66
+ return if predicate.nil? || !valid?
54
67
 
55
- def set_args; end
68
+ @valid = !value.public_send(predicate)
69
+ end
56
70
 
57
71
  def allows_array
58
72
  ALLOWS_HASH.keys
59
73
  end
60
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?
81
+
82
+ key = allows.keys.first
83
+ ALLOWS_HASH[key]
84
+ end
85
+
61
86
  def required?
62
87
  required
63
88
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Attribeauty
4
- VERSION = "0.3.2"
4
+ VERSION = "0.3.3"
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.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toby