attribool 2.0.2 → 2.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/attribool/validator_service.rb +26 -5
- data/lib/attribool/validators/attribute_list_validator.rb +14 -0
- data/lib/attribool/validators/condition_validator.rb +14 -0
- data/lib/attribool/validators/method_name_validator.rb +10 -0
- data/lib/attribool/validators/nil_attribute_validator.rb +18 -0
- data/lib/attribool/validators/strict_boolean_validator.rb +16 -0
- data/lib/attribool/validators.rb +25 -0
- data/lib/attribool/value.rb +12 -0
- data/lib/attribool/version.rb +2 -2
- data/lib/attribool.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 63c34a661a7575ad39598073bbfc995f6e18d822c9587ee4cf9b28ed6ead7d96
|
4
|
+
data.tar.gz: 4d3b761b8ed920a44e145727e7d07eb6d2fa2c22ea0b0d91d24569248362f03a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 927ecdcc2b35fa4448beda19e02c968441cd858933bfdbcb261b11e494e5a46676b9c36e17f61e9f4f9400f6af547662b84d11b28c17df664a710a840595ed79
|
7
|
+
data.tar.gz: d7a9895258e80fb76ba7eee27a8af15b16cef705097bbc2a1a77d459c100f3a54b52c3127251455308a5aa2f744a0e0596b802b24792c6e0ef0ff9d0ea2f1b95
|
data/Gemfile.lock
CHANGED
@@ -1,17 +1,38 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Attribool
|
4
|
+
##
|
5
|
+
# A simple interface to run validators, which should implement a +valid?+
|
6
|
+
# method which returns true if conditions are valid, and an +error+ method
|
7
|
+
# which returns an exception class and message to be raised when validations
|
8
|
+
# fail.
|
4
9
|
class ValidatorService
|
5
|
-
|
6
|
-
|
10
|
+
##
|
11
|
+
# Run the validator.
|
12
|
+
#
|
13
|
+
# @param [Symbol] validator_name
|
14
|
+
#
|
15
|
+
# @param [Object] *args to be forwarded to validator
|
16
|
+
def self.call(validator_name, *args)
|
17
|
+
new(::Attribool::Validators.fetch(validator_name), *args).validate
|
7
18
|
end
|
8
19
|
|
20
|
+
##
|
21
|
+
# Construct the service and inject the validator.
|
22
|
+
#
|
23
|
+
# @param [Class] Validator
|
24
|
+
#
|
25
|
+
# @param [Object] *args
|
9
26
|
def initialize(validator, *args)
|
10
|
-
@validator =
|
11
|
-
"#{validator.to_s.split("_").map(&:capitalize).join}Validator"
|
12
|
-
).new(*args)
|
27
|
+
@validator = validator.new(*args)
|
13
28
|
end
|
14
29
|
|
30
|
+
##
|
31
|
+
# Raises the validator's exception unless its conditions are met.
|
32
|
+
#
|
33
|
+
# @return [Boolean]
|
34
|
+
#
|
35
|
+
# @raise [Exception] if validation fails
|
15
36
|
def validate
|
16
37
|
@validator.valid? || raise(@validator.error)
|
17
38
|
end
|
@@ -1,15 +1,29 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Attribool::Validators
|
4
|
+
##
|
5
|
+
# Ensures that every item is an instance of +Attribool::Attribute+.
|
4
6
|
class AttributeListValidator
|
7
|
+
##
|
8
|
+
# Construct the validator.
|
9
|
+
#
|
10
|
+
# @param [Attribool::Attribute] *items
|
5
11
|
def initialize(*items)
|
6
12
|
@items = items
|
7
13
|
end
|
8
14
|
|
15
|
+
##
|
16
|
+
# Are all items an instance of +Attribool::Attribute+?
|
17
|
+
#
|
18
|
+
# @return [Boolean]
|
9
19
|
def valid?
|
10
20
|
@items.all?(Attribool::Attribute)
|
11
21
|
end
|
12
22
|
|
23
|
+
##
|
24
|
+
# The exception to raise if validations fail.
|
25
|
+
#
|
26
|
+
# @return [TypeError] the exception with message
|
13
27
|
def error
|
14
28
|
TypeError.new("All items must be an instance of Attribool::Attribute")
|
15
29
|
end
|
@@ -1,15 +1,29 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Attribool::Validators
|
4
|
+
##
|
5
|
+
# Ensures that a condition is either +nil+ or a +Proc+.
|
4
6
|
class ConditionValidator
|
7
|
+
##
|
8
|
+
# Construct the validator.
|
9
|
+
#
|
10
|
+
# @param [nil, Proc] condition
|
5
11
|
def initialize(condition)
|
6
12
|
@condition = condition
|
7
13
|
end
|
8
14
|
|
15
|
+
##
|
16
|
+
# Is the condition either +nil+ or a +Proc+?
|
17
|
+
#
|
18
|
+
# @return [Boolean]
|
9
19
|
def valid?
|
10
20
|
@condition.nil? || @condition.is_a?(Proc)
|
11
21
|
end
|
12
22
|
|
23
|
+
##
|
24
|
+
# The exception to raise if validations fail.
|
25
|
+
#
|
26
|
+
# @return [ArgumentError] the exception with message
|
13
27
|
def error
|
14
28
|
ArgumentError.new("Condition is not a proc")
|
15
29
|
end
|
@@ -5,15 +5,25 @@ module Attribool::Validators
|
|
5
5
|
# Ensures that if multiple attributes are being defined, and +method_name+
|
6
6
|
# is provided, that +method_name+ is a +Proc+.
|
7
7
|
class MethodNameValidator
|
8
|
+
##
|
9
|
+
# Construct the validator.
|
10
|
+
#
|
11
|
+
# @param [Attribool::Attribute] *items
|
8
12
|
def initialize(method_name, number_of_attributes)
|
9
13
|
@method_name = method_name
|
10
14
|
@number_of_attributes = number_of_attributes
|
11
15
|
end
|
12
16
|
|
17
|
+
##
|
18
|
+
# Is there either one attribute, or is +method_name+ +nil+ or a +Proc+?
|
13
19
|
def valid?
|
14
20
|
@number_of_attributes == 1 || nil_or_proc?
|
15
21
|
end
|
16
22
|
|
23
|
+
##
|
24
|
+
# The exception to raise if validations fail.
|
25
|
+
#
|
26
|
+
# @return [ArgumentError] the exception with message
|
17
27
|
def error
|
18
28
|
ArgumentError.new("Must use a Proc when creating multiple methods")
|
19
29
|
end
|
@@ -1,17 +1,35 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Attribool::Validators
|
4
|
+
##
|
5
|
+
# Ensures that a value is not +nil+, unless +nil+ is allowed as a value.
|
4
6
|
class NilAttributeValidator
|
7
|
+
##
|
8
|
+
# Construct the validator.
|
9
|
+
#
|
10
|
+
# @param [String, Symbol] ivar
|
11
|
+
#
|
12
|
+
# @param [Object] value
|
13
|
+
#
|
14
|
+
# @param [Boolean] allow_nil
|
5
15
|
def initialize(ivar, value, allow_nil)
|
6
16
|
@ivar = ivar
|
7
17
|
@value = value
|
8
18
|
@allow_nil = allow_nil
|
9
19
|
end
|
10
20
|
|
21
|
+
##
|
22
|
+
# Do we either allow values to be +nil+, or is the value not +nil+?
|
23
|
+
#
|
24
|
+
# @return [Boolean]
|
11
25
|
def valid?
|
12
26
|
@allow_nil || !@value.nil?
|
13
27
|
end
|
14
28
|
|
29
|
+
##
|
30
|
+
# The exception to raise if validations fail.
|
31
|
+
#
|
32
|
+
# @return [TypeError] the exception with message
|
15
33
|
def error
|
16
34
|
TypeError.new("#{@ivar} is nil")
|
17
35
|
end
|
@@ -1,16 +1,32 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Attribool::Validators
|
4
|
+
##
|
5
|
+
# Ensures that a value is a boolean, unless strictness isn't enforced.
|
4
6
|
class StrictBooleanValidator
|
7
|
+
##
|
8
|
+
# Construct the validator.
|
9
|
+
#
|
10
|
+
# @param [Object] value
|
11
|
+
#
|
12
|
+
# @param [Boolean] strict
|
5
13
|
def initialize(value, strict)
|
6
14
|
@value = value
|
7
15
|
@strict = strict
|
8
16
|
end
|
9
17
|
|
18
|
+
##
|
19
|
+
# Is +strict+ set to +false+, or is +@value+ a boolean?
|
20
|
+
#
|
21
|
+
# @return [Boolean]
|
10
22
|
def valid?
|
11
23
|
!@strict || [TrueClass, FalseClass].include?(@value.class)
|
12
24
|
end
|
13
25
|
|
26
|
+
##
|
27
|
+
# The exception to raise if validations fail.
|
28
|
+
#
|
29
|
+
# @return [ArgumentError] the exception with message
|
14
30
|
def error
|
15
31
|
ArgumentError.new("#{@value} is not a boolean")
|
16
32
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Attribool
|
4
|
+
##
|
5
|
+
# Namespace for Validators. Also provides a method for fetching a validator.
|
6
|
+
module Validators
|
7
|
+
module_function
|
8
|
+
|
9
|
+
##
|
10
|
+
# Fetches a Validator class.
|
11
|
+
#
|
12
|
+
# @param [String, Symbol] validator_name
|
13
|
+
#
|
14
|
+
# @return [Class]
|
15
|
+
#
|
16
|
+
# @example
|
17
|
+
# Attribool::Validators.fetch(:nil_attribute)
|
18
|
+
# # => NilAttributeValidator
|
19
|
+
def fetch(validator_name)
|
20
|
+
const_get(
|
21
|
+
"#{validator_name.to_s.split("_").map(&:capitalize).join}Validator"
|
22
|
+
)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/attribool/value.rb
CHANGED
@@ -1,13 +1,25 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Attribool
|
4
|
+
##
|
5
|
+
# An abstraction of any class that can convert itself to a boolean.
|
4
6
|
class Value
|
7
|
+
##
|
8
|
+
# Construct the value with an optional +Proc+ condition.
|
9
|
+
#
|
10
|
+
# @param [Object] value
|
11
|
+
#
|
12
|
+
# @param [Proc] condition (default: nil)
|
5
13
|
def initialize(value, condition = nil)
|
6
14
|
ValidatorService.call(:condition, condition)
|
7
15
|
@value = value
|
8
16
|
@condition = condition
|
9
17
|
end
|
10
18
|
|
19
|
+
##
|
20
|
+
# Convert the value or the condition to a boolean based off truthiness.
|
21
|
+
#
|
22
|
+
# @return [Boolean]
|
11
23
|
def to_boolean
|
12
24
|
!!(@condition ? @condition.call(@value) : @value)
|
13
25
|
end
|
data/lib/attribool/version.rb
CHANGED
@@ -21,14 +21,14 @@ module Attribool
|
|
21
21
|
# Patch version.
|
22
22
|
#
|
23
23
|
# @return [Integer]
|
24
|
-
PATCH =
|
24
|
+
PATCH = 3
|
25
25
|
|
26
26
|
module_function
|
27
27
|
|
28
28
|
##
|
29
29
|
# Version as +[MAJOR, MINOR, PATCH]+
|
30
30
|
#
|
31
|
-
# @return [Array]
|
31
|
+
# @return [Array<Integer>]
|
32
32
|
def to_a
|
33
33
|
[MAJOR, MINOR, PATCH]
|
34
34
|
end
|
data/lib/attribool.rb
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
require_relative "attribool/version"
|
4
4
|
require_relative "attribool/value"
|
5
5
|
require_relative "attribool/attribute"
|
6
|
+
require_relative "attribool/validators"
|
6
7
|
require_relative "attribool/reader_name"
|
7
8
|
require_relative "attribool/attribute_list"
|
8
9
|
require_relative "attribool/validator_service"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attribool
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Evan Gray
|
@@ -111,6 +111,7 @@ files:
|
|
111
111
|
- lib/attribool/attribute_list.rb
|
112
112
|
- lib/attribool/reader_name.rb
|
113
113
|
- lib/attribool/validator_service.rb
|
114
|
+
- lib/attribool/validators.rb
|
114
115
|
- lib/attribool/validators/attribute_list_validator.rb
|
115
116
|
- lib/attribool/validators/condition_validator.rb
|
116
117
|
- lib/attribool/validators/method_name_validator.rb
|