attribool 2.0.1 → 2.0.3
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/Gemfile.lock +1 -1
 - data/lib/attribool/attribute_list.rb +15 -4
 - data/lib/attribool/validator_service.rb +26 -5
 - data/lib/attribool/validators/attribute_list_validator.rb +31 -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 +4 -2
 - 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: 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
    
    
| 
         @@ -8,13 +8,24 @@ module Attribool 
     | 
|
| 
       8 
8 
     | 
    
         
             
                include Enumerable
         
     | 
| 
       9 
9 
     | 
    
         | 
| 
       10 
10 
     | 
    
         
             
                ##
         
     | 
| 
       11 
     | 
    
         
            -
                #  
     | 
| 
      
 11 
     | 
    
         
            +
                # Create an +AttributeList+ from a list of attribute names.
         
     | 
| 
       12 
12 
     | 
    
         
             
                #
         
     | 
| 
       13 
     | 
    
         
            -
                # @param [String, Symbol] * 
     | 
| 
      
 13 
     | 
    
         
            +
                # @param [String, Symbol] *attribute_names
         
     | 
| 
       14 
14 
     | 
    
         
             
                #
         
     | 
| 
       15 
15 
     | 
    
         
             
                # @kwarg [nil, String, Symbol, Proc] method_name
         
     | 
| 
       16 
     | 
    
         
            -
                 
     | 
| 
       17 
     | 
    
         
            -
             
     | 
| 
      
 16 
     | 
    
         
            +
                #
         
     | 
| 
      
 17 
     | 
    
         
            +
                # @return [AttributeList]
         
     | 
| 
      
 18 
     | 
    
         
            +
                def self.build(*attribute_names, method_name: nil)
         
     | 
| 
      
 19 
     | 
    
         
            +
                  new(*attribute_names.map { |a| Attribool::Attribute.new(a, method_name) })
         
     | 
| 
      
 20 
     | 
    
         
            +
                end
         
     | 
| 
      
 21 
     | 
    
         
            +
             
     | 
| 
      
 22 
     | 
    
         
            +
                ##
         
     | 
| 
      
 23 
     | 
    
         
            +
                # Construct the list.
         
     | 
| 
      
 24 
     | 
    
         
            +
                #
         
     | 
| 
      
 25 
     | 
    
         
            +
                # @param [String, Symbol] *attributes
         
     | 
| 
      
 26 
     | 
    
         
            +
                def initialize(*attributes)
         
     | 
| 
      
 27 
     | 
    
         
            +
                  ValidatorService.call(:attribute_list, *attributes)
         
     | 
| 
      
 28 
     | 
    
         
            +
                  @entries = attributes
         
     | 
| 
       18 
29 
     | 
    
         
             
                end
         
     | 
| 
       19 
30 
     | 
    
         | 
| 
       20 
31 
     | 
    
         
             
                ##
         
     | 
| 
         @@ -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
         
     | 
| 
         @@ -0,0 +1,31 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # frozen_string_literal: true
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            module Attribool::Validators
         
     | 
| 
      
 4 
     | 
    
         
            +
              ##
         
     | 
| 
      
 5 
     | 
    
         
            +
              # Ensures that every item is an instance of +Attribool::Attribute+.
         
     | 
| 
      
 6 
     | 
    
         
            +
              class AttributeListValidator
         
     | 
| 
      
 7 
     | 
    
         
            +
                ##
         
     | 
| 
      
 8 
     | 
    
         
            +
                # Construct the validator.
         
     | 
| 
      
 9 
     | 
    
         
            +
                #
         
     | 
| 
      
 10 
     | 
    
         
            +
                # @param [Attribool::Attribute] *items
         
     | 
| 
      
 11 
     | 
    
         
            +
                def initialize(*items)
         
     | 
| 
      
 12 
     | 
    
         
            +
                  @items = items
         
     | 
| 
      
 13 
     | 
    
         
            +
                end
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
                ##
         
     | 
| 
      
 16 
     | 
    
         
            +
                # Are all items an instance of +Attribool::Attribute+?
         
     | 
| 
      
 17 
     | 
    
         
            +
                #
         
     | 
| 
      
 18 
     | 
    
         
            +
                # @return [Boolean]
         
     | 
| 
      
 19 
     | 
    
         
            +
                def valid?
         
     | 
| 
      
 20 
     | 
    
         
            +
                  @items.all?(Attribool::Attribute)
         
     | 
| 
      
 21 
     | 
    
         
            +
                end
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
                ##
         
     | 
| 
      
 24 
     | 
    
         
            +
                # The exception to raise if validations fail.
         
     | 
| 
      
 25 
     | 
    
         
            +
                #
         
     | 
| 
      
 26 
     | 
    
         
            +
                # @return [TypeError] the exception with message
         
     | 
| 
      
 27 
     | 
    
         
            +
                def error
         
     | 
| 
      
 28 
     | 
    
         
            +
                  TypeError.new("All items must be an instance of Attribool::Attribute")
         
     | 
| 
      
 29 
     | 
    
         
            +
                end
         
     | 
| 
      
 30 
     | 
    
         
            +
              end
         
     | 
| 
      
 31 
     | 
    
         
            +
            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"
         
     | 
| 
         @@ -10,6 +11,7 @@ require_relative "attribool/validators/condition_validator" 
     | 
|
| 
       10 
11 
     | 
    
         
             
            require_relative "attribool/validators/method_name_validator"
         
     | 
| 
       11 
12 
     | 
    
         
             
            require_relative "attribool/validators/nil_attribute_validator"
         
     | 
| 
       12 
13 
     | 
    
         
             
            require_relative "attribool/validators/strict_boolean_validator"
         
     | 
| 
      
 14 
     | 
    
         
            +
            require_relative "attribool/validators/attribute_list_validator"
         
     | 
| 
       13 
15 
     | 
    
         | 
| 
       14 
16 
     | 
    
         
             
            ##
         
     | 
| 
       15 
17 
     | 
    
         
             
            # Adds macros for dealing with boolean attributes.
         
     | 
| 
         @@ -44,7 +46,7 @@ module Attribool 
     | 
|
| 
       44 
46 
     | 
    
         
             
              def bool_reader(*attributes, allow_nil: true, method_name: nil, condition: nil)
         
     | 
| 
       45 
47 
     | 
    
         
             
                ValidatorService.call(:method_name, method_name, attributes.size)
         
     | 
| 
       46 
48 
     | 
    
         | 
| 
       47 
     | 
    
         
            -
                AttributeList. 
     | 
| 
      
 49 
     | 
    
         
            +
                AttributeList.build(*attributes, method_name: method_name).each do |attribute|
         
     | 
| 
       48 
50 
     | 
    
         
             
                  define_method(attribute.reader) do
         
     | 
| 
       49 
51 
     | 
    
         
             
                    instance_variable_get(attribute.ivar).then do |value|
         
     | 
| 
       50 
52 
     | 
    
         
             
                      ValidatorService.call(:nil_attribute, attribute.ivar, value, allow_nil)
         
     | 
| 
         @@ -63,7 +65,7 @@ module Attribool 
     | 
|
| 
       63 
65 
     | 
    
         
             
              #
         
     | 
| 
       64 
66 
     | 
    
         
             
              # @kwarg [Boolean] strict
         
     | 
| 
       65 
67 
     | 
    
         
             
              def bool_writer(*attributes, strict: false)
         
     | 
| 
       66 
     | 
    
         
            -
                AttributeList. 
     | 
| 
      
 68 
     | 
    
         
            +
                AttributeList.build(*attributes).each do |attribute|
         
     | 
| 
       67 
69 
     | 
    
         
             
                  define_method(attribute.writer) do |value|
         
     | 
| 
       68 
70 
     | 
    
         
             
                    ValidatorService.call(:strict_boolean, value, strict)
         
     | 
| 
       69 
71 
     | 
    
         | 
    
        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.3
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - Evan Gray
         
     | 
| 
       8 
8 
     | 
    
         
             
            autorequire:
         
     | 
| 
       9 
9 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       10 
10 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       11 
     | 
    
         
            -
            date: 2023- 
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2023-06-23 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: pry
         
     | 
| 
         @@ -111,6 +111,8 @@ 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
         
     | 
| 
      
 115 
     | 
    
         
            +
            - lib/attribool/validators/attribute_list_validator.rb
         
     | 
| 
       114 
116 
     | 
    
         
             
            - lib/attribool/validators/condition_validator.rb
         
     | 
| 
       115 
117 
     | 
    
         
             
            - lib/attribool/validators/method_name_validator.rb
         
     | 
| 
       116 
118 
     | 
    
         
             
            - lib/attribool/validators/nil_attribute_validator.rb
         
     |