attribool 2.0.2 → 2.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +1 -1
- data/.projections.json +40 -0
- data/Gemfile.lock +1 -1
- data/Rakefile +45 -0
- data/lib/attribool/attribute_list.rb +12 -10
- 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 +3 -3
- data/lib/attribool.rb +1 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d04f89c318e02c603522d41b99a64ef3cc228da4132ac58122abde616c955486
|
4
|
+
data.tar.gz: 9555723018f19c3a5fd8a77c86c975597c21ee8b525d9571977b8287d8685844
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 64c66f2a26f7b2280f961b97ed373703e55696a1ae741032909355565da7313b9c166b04037855c062f76681234478d8d230ab9bc36114e288ed6a5ce61ccba4
|
7
|
+
data.tar.gz: 6287a1d91606e76e595c8b1670d1dff3066e313dc25f70ba5fbb0722a1a2601e7648f2ff9cd6899df84da504c02d4caa263e7a2fef4a9eca140c9b867c01234b
|
data/.github/workflows/ruby.yml
CHANGED
data/.projections.json
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
{
|
2
|
+
"lib/attribool/validators/*_validator.rb": {
|
3
|
+
"command": "validator",
|
4
|
+
"related": "lib/attribool/validator_service.rb",
|
5
|
+
"alternate": "test/attribool/validators/{}_test.rb",
|
6
|
+
"template": [
|
7
|
+
"# frozen_string_literal: true",
|
8
|
+
"",
|
9
|
+
"module Attribool::Validators",
|
10
|
+
" ##",
|
11
|
+
" # Ensures that {blank} is validated.",
|
12
|
+
" class {capitalize|camelcase}Validator",
|
13
|
+
" ##",
|
14
|
+
" # Construct the validator.",
|
15
|
+
" #",
|
16
|
+
" # @param [Todo] todo",
|
17
|
+
" def initialize(todo)",
|
18
|
+
" @todo = todo",
|
19
|
+
" end",
|
20
|
+
"",
|
21
|
+
" ##",
|
22
|
+
" # Todo?",
|
23
|
+
" #",
|
24
|
+
" # @return [Boolean]",
|
25
|
+
" def valid?",
|
26
|
+
" # TODO",
|
27
|
+
" end",
|
28
|
+
"",
|
29
|
+
" ##",
|
30
|
+
" # The exception to raise if validations fail.",
|
31
|
+
" #",
|
32
|
+
" # @return [TodoError] the exception with message",
|
33
|
+
" def error",
|
34
|
+
" TodoError.new(\"todo\")",
|
35
|
+
" end",
|
36
|
+
" end",
|
37
|
+
"end"
|
38
|
+
]
|
39
|
+
}
|
40
|
+
}
|
data/Gemfile.lock
CHANGED
data/Rakefile
CHANGED
@@ -19,3 +19,48 @@ RDoc::Task.new do |rdoc|
|
|
19
19
|
end
|
20
20
|
|
21
21
|
task default: :test
|
22
|
+
|
23
|
+
namespace :version do
|
24
|
+
desc "Print the current version from the version.rb file"
|
25
|
+
task :current do
|
26
|
+
puts Attribool::VERSION
|
27
|
+
end
|
28
|
+
|
29
|
+
namespace :increment do
|
30
|
+
desc "Increment the version's PATCH level"
|
31
|
+
task :patch do
|
32
|
+
File.join(__dir__, "lib", "attribool", "version.rb").then do |version_file|
|
33
|
+
File.write(
|
34
|
+
version_file,
|
35
|
+
File.read(version_file).sub(/(PATCH\s=\s)(\d+)/) { "#{$1}#{$2.next}" }
|
36
|
+
)
|
37
|
+
end
|
38
|
+
system("bundle lock")
|
39
|
+
end
|
40
|
+
desc "Increment the version's MINOR level"
|
41
|
+
task :minor do
|
42
|
+
File.join(__dir__, "lib", "attribool", "version.rb").then do |version_file|
|
43
|
+
File.write(
|
44
|
+
version_file,
|
45
|
+
File.read(version_file)
|
46
|
+
.sub(/(PATCH\s=\s)(\d+)/) { "#{$1}0" }
|
47
|
+
.sub(/(MINOR\s=\s)(\d+)/) { "#{$1}#{$2.next}" }
|
48
|
+
)
|
49
|
+
end
|
50
|
+
system("bundle lock")
|
51
|
+
end
|
52
|
+
desc "Increment the version's MAJOR level"
|
53
|
+
task :major do
|
54
|
+
File.join(__dir__, "lib", "attribool", "version.rb").then do |version_file|
|
55
|
+
File.write(
|
56
|
+
version_file,
|
57
|
+
File.read(version_file)
|
58
|
+
.sub(/(PATCH\s=\s)(\d+)/) { "#{$1}0" }
|
59
|
+
.sub(/(MINOR\s=\s)(\d+)/) { "#{$1}0" }
|
60
|
+
.sub(/(MAJOR\s=\s)(\d+)/) { "#{$1}#{$2.next}" }
|
61
|
+
)
|
62
|
+
end
|
63
|
+
system("bundle lock")
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -1,11 +1,23 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "forwardable"
|
4
|
+
|
3
5
|
module Attribool
|
4
6
|
##
|
5
7
|
# Enumerable class that generates a list of attributes from a list of strings
|
6
8
|
# or symbols.
|
7
9
|
class AttributeList
|
8
10
|
include Enumerable
|
11
|
+
extend Forwardable
|
12
|
+
|
13
|
+
##
|
14
|
+
# :method: each
|
15
|
+
# Yield every entry in the list to a block.
|
16
|
+
#
|
17
|
+
# @param [block] &block
|
18
|
+
#
|
19
|
+
# @return [Enumerable]
|
20
|
+
def_delegators :@entries, :each
|
9
21
|
|
10
22
|
##
|
11
23
|
# Create an +AttributeList+ from a list of attribute names.
|
@@ -27,15 +39,5 @@ module Attribool
|
|
27
39
|
ValidatorService.call(:attribute_list, *attributes)
|
28
40
|
@entries = attributes
|
29
41
|
end
|
30
|
-
|
31
|
-
##
|
32
|
-
# Yield every entry in the list to a block.
|
33
|
-
#
|
34
|
-
# @param [block] &block
|
35
|
-
#
|
36
|
-
# @return [Enumerable]
|
37
|
-
def each(&block)
|
38
|
-
@entries.each(&block)
|
39
|
-
end
|
40
42
|
end
|
41
43
|
end
|
@@ -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 = 4
|
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
|
@@ -54,5 +54,5 @@ module Attribool
|
|
54
54
|
# The version, as a string.
|
55
55
|
#
|
56
56
|
# @return [String]
|
57
|
-
VERSION = Version.to_s
|
57
|
+
VERSION = Version.to_s
|
58
58
|
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,14 +1,14 @@
|
|
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.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Evan Gray
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-08-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry
|
@@ -100,6 +100,7 @@ extra_rdoc_files: []
|
|
100
100
|
files:
|
101
101
|
- ".github/workflows/ruby.yml"
|
102
102
|
- ".gitignore"
|
103
|
+
- ".projections.json"
|
103
104
|
- Gemfile
|
104
105
|
- Gemfile.lock
|
105
106
|
- LICENSE
|
@@ -111,6 +112,7 @@ files:
|
|
111
112
|
- lib/attribool/attribute_list.rb
|
112
113
|
- lib/attribool/reader_name.rb
|
113
114
|
- lib/attribool/validator_service.rb
|
115
|
+
- lib/attribool/validators.rb
|
114
116
|
- lib/attribool/validators/attribute_list_validator.rb
|
115
117
|
- lib/attribool/validators/condition_validator.rb
|
116
118
|
- lib/attribool/validators/method_name_validator.rb
|