attribool 2.0.0 → 2.0.2

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: cee1c79ed2d93c1f4d77b14f42d59dfd7c805afe569d9e43c999484d552c64ff
4
- data.tar.gz: becb227aebe9b9878f40b3a3de4a9737623a10f364d2788830f3b440524ce0fb
3
+ metadata.gz: d928a7ac0b90deb248cfcbb3a9b638baa6a38a8c62691c884df1e2db2f7c6d04
4
+ data.tar.gz: 525256019fd56a8bc53695c1772e6367fcb6dac0ac05da4a4f457431029d21d9
5
5
  SHA512:
6
- metadata.gz: 4904af389ad68348764f519eb058aaff66cb022dcc3de3d9f1ee3ce07b620eb08552bb9f03c9316943693370ed79131d11b91950433a4950bbdc4ce7619d34a4
7
- data.tar.gz: 0f0d3622f6214668c8fa150d7e1b89ab828a803803aaa4707c25fd53b72a734d5e07f3ecc9d6d6a600c6e47522729ad0097da600065f76ad43fd6a2675efa203
6
+ metadata.gz: 3719316d9feaa069f64748a23ef1ba9531dbecb6b7cb3188e8292f9f70a7cdd3a262522b63a8a6c7b9673a232a3c6e2dc86953cba6527ea6c9727d5e32360c0f
7
+ data.tar.gz: 792822e7f8e5ee0242d8a60cd5ab0870613794a26652bcb7025dbdda5dd3500bee8004c66fdd297c8e0a26c0e6ec1a458967e69186a91a1795bb5ee92e65a4d6
@@ -1,11 +1,4 @@
1
- # This workflow uses actions that are not certified by GitHub.
2
- # They are provided by a third-party and are governed by
3
- # separate terms of service, privacy policy, and support
4
- # documentation.
5
- # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
- # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
-
8
- name: Ruby
1
+ name: Ruby CI
9
2
 
10
3
  on:
11
4
  push:
@@ -17,19 +10,18 @@ jobs:
17
10
  test:
18
11
 
19
12
  runs-on: ubuntu-latest
13
+
20
14
  strategy:
21
15
  matrix:
22
- ruby-version: ['2.6', '2.7', '3.0']
16
+ ruby-version: ['3.2', '3.1', '3.0', '2.7', '2.6']
23
17
 
24
18
  steps:
25
- - uses: actions/checkout@v2
26
- - name: Set up Ruby
27
- # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
28
- # change this to (see https://github.com/ruby/setup-ruby#versioning):
29
- # uses: ruby/setup-ruby@v1
30
- uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
31
- with:
32
- ruby-version: ${{ matrix.ruby-version }}
33
- bundler-cache: true # runs 'bundle install' and caches installed gems automatically
34
- - name: Run tests
35
- run: bundle exec rake
19
+ - uses: actions/checkout@v3
20
+ - name: Set up Ruby ${{ matrix.ruby-version }}
21
+ uses: ruby/setup-ruby@ec02537da5712d66d4d50a0f33b7eb52773b5ed1
22
+ with:
23
+ ruby-version: ${{ matrix.ruby-version }}
24
+ - name: Install dependencies
25
+ run: bundle install
26
+ - name: Run tests
27
+ run: bundle exec rake
data/Gemfile.lock CHANGED
@@ -1,19 +1,24 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- attribool (2.0.0)
4
+ attribool (2.0.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
9
  ast (2.4.2)
10
+ coderay (1.1.3)
10
11
  docile (1.3.5)
11
12
  json (2.6.3)
12
13
  language_server-protocol (3.17.0.2)
14
+ method_source (1.0.0)
13
15
  parallel (1.22.1)
14
16
  parser (3.1.3.0)
15
17
  ast (~> 2.4.1)
16
18
  power_assert (2.0.0)
19
+ pry (0.14.2)
20
+ coderay (~> 1.1)
21
+ method_source (~> 1.0)
17
22
  rainbow (3.1.1)
18
23
  rake (13.0.3)
19
24
  regexp_parser (2.6.1)
@@ -53,6 +58,7 @@ PLATFORMS
53
58
 
54
59
  DEPENDENCIES
55
60
  attribool!
61
+ pry
56
62
  rake (~> 13.0, >= 13.0.1)
57
63
  simplecov
58
64
  standard
data/attribool.gemspec CHANGED
@@ -27,6 +27,7 @@ Gem::Specification.new do |spec|
27
27
  spec.bindir = "bin"
28
28
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
29
29
  spec.require_paths = ["lib"]
30
+ spec.add_development_dependency "pry"
30
31
  spec.add_development_dependency "rake", "~> 13.0", ">= 13.0.1"
31
32
  spec.add_development_dependency "simplecov"
32
33
  spec.add_development_dependency "standard"
@@ -8,13 +8,24 @@ module Attribool
8
8
  include Enumerable
9
9
 
10
10
  ##
11
- # Generate the list.
11
+ # Create an +AttributeList+ from a list of attribute names.
12
12
  #
13
- # @param [String, Symbol] *attributes
13
+ # @param [String, Symbol] *attribute_names
14
14
  #
15
15
  # @kwarg [nil, String, Symbol, Proc] method_name
16
- def initialize(*attributes, method_name: nil)
17
- @entries = attributes.map { |a| Attribool::Attribute.new(a, method_name) }
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
  ##
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Attribool
4
+ class ValidatorService
5
+ def self.call(validator, *args)
6
+ new(validator, *args).validate
7
+ end
8
+
9
+ def initialize(validator, *args)
10
+ @validator = ::Attribool::Validators.const_get(
11
+ "#{validator.to_s.split("_").map(&:capitalize).join}Validator"
12
+ ).new(*args)
13
+ end
14
+
15
+ def validate
16
+ @validator.valid? || raise(@validator.error)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Attribool::Validators
4
+ class AttributeListValidator
5
+ def initialize(*items)
6
+ @items = items
7
+ end
8
+
9
+ def valid?
10
+ @items.all?(Attribool::Attribute)
11
+ end
12
+
13
+ def error
14
+ TypeError.new("All items must be an instance of Attribool::Attribute")
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Attribool::Validators
4
+ class ConditionValidator
5
+ def initialize(condition)
6
+ @condition = condition
7
+ end
8
+
9
+ def valid?
10
+ @condition.nil? || @condition.is_a?(Proc)
11
+ end
12
+
13
+ def error
14
+ ArgumentError.new("Condition is not a proc")
15
+ end
16
+ end
17
+ end
@@ -4,11 +4,10 @@ module Attribool::Validators
4
4
  ##
5
5
  # Ensures that if multiple attributes are being defined, and +method_name+
6
6
  # is provided, that +method_name+ is a +Proc+.
7
- class MethodNameValidator < Attribool::Validators::Validator
7
+ class MethodNameValidator
8
8
  def initialize(method_name, number_of_attributes)
9
9
  @method_name = method_name
10
10
  @number_of_attributes = number_of_attributes
11
- super
12
11
  end
13
12
 
14
13
  def valid?
@@ -1,12 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Attribool::Validators
4
- class NilAttributeValidator < Attribool::Validators::Validator
4
+ class NilAttributeValidator
5
5
  def initialize(ivar, value, allow_nil)
6
6
  @ivar = ivar
7
7
  @value = value
8
8
  @allow_nil = allow_nil
9
- super
10
9
  end
11
10
 
12
11
  def valid?
@@ -1,11 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Attribool::Validators
4
- class StrictBooleanValidator < Attribool::Validators::Validator
4
+ class StrictBooleanValidator
5
5
  def initialize(value, strict)
6
6
  @value = value
7
7
  @strict = strict
8
- super
9
8
  end
10
9
 
11
10
  def valid?
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Attribool
4
+ class Value
5
+ def initialize(value, condition = nil)
6
+ ValidatorService.call(:condition, condition)
7
+ @value = value
8
+ @condition = condition
9
+ end
10
+
11
+ def to_boolean
12
+ !!(@condition ? @condition.call(@value) : @value)
13
+ end
14
+ end
15
+ end
@@ -21,7 +21,7 @@ module Attribool
21
21
  # Patch version.
22
22
  #
23
23
  # @return [Integer]
24
- PATCH = 0
24
+ PATCH = 2
25
25
 
26
26
  module_function
27
27
 
data/lib/attribool.rb CHANGED
@@ -1,13 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "attribool/version"
4
+ require_relative "attribool/value"
4
5
  require_relative "attribool/attribute"
5
6
  require_relative "attribool/reader_name"
6
7
  require_relative "attribool/attribute_list"
7
- require_relative "attribool/validators/validator"
8
+ require_relative "attribool/validator_service"
9
+ require_relative "attribool/validators/condition_validator"
8
10
  require_relative "attribool/validators/method_name_validator"
9
11
  require_relative "attribool/validators/nil_attribute_validator"
10
12
  require_relative "attribool/validators/strict_boolean_validator"
13
+ require_relative "attribool/validators/attribute_list_validator"
11
14
 
12
15
  ##
13
16
  # Adds macros for dealing with boolean attributes.
@@ -40,14 +43,14 @@ module Attribool
40
43
  #
41
44
  # @kwarg [Symbol, String, Proc] method_name
42
45
  def bool_reader(*attributes, allow_nil: true, method_name: nil, condition: nil)
43
- Validators::MethodNameValidator.validate(method_name, attributes.size)
46
+ ValidatorService.call(:method_name, method_name, attributes.size)
44
47
 
45
- AttributeList.new(*attributes, method_name: method_name).each do |attribute|
48
+ AttributeList.build(*attributes, method_name: method_name).each do |attribute|
46
49
  define_method(attribute.reader) do
47
50
  instance_variable_get(attribute.ivar).then do |value|
48
- Validators::NilAttributeValidator.validate(attribute.ivar, value, allow_nil)
51
+ ValidatorService.call(:nil_attribute, attribute.ivar, value, allow_nil)
49
52
 
50
- !!(condition ? condition.call(value) : value)
53
+ Value.new(value, condition).to_boolean
51
54
  end
52
55
  end
53
56
  end
@@ -61,11 +64,11 @@ module Attribool
61
64
  #
62
65
  # @kwarg [Boolean] strict
63
66
  def bool_writer(*attributes, strict: false)
64
- AttributeList.new(*attributes).each do |attribute|
67
+ AttributeList.build(*attributes).each do |attribute|
65
68
  define_method(attribute.writer) do |value|
66
- Validators::StrictBooleanValidator.validate(value, strict)
69
+ ValidatorService.call(:strict_boolean, value, strict)
67
70
 
68
- instance_variable_set(attribute.ivar, !!value)
71
+ instance_variable_set(attribute.ivar, Value.new(value).to_boolean)
69
72
  end
70
73
  end
71
74
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attribool
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan Gray
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-12-19 00:00:00.000000000 Z
11
+ date: 2023-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rake
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -96,10 +110,13 @@ files:
96
110
  - lib/attribool/attribute.rb
97
111
  - lib/attribool/attribute_list.rb
98
112
  - lib/attribool/reader_name.rb
113
+ - lib/attribool/validator_service.rb
114
+ - lib/attribool/validators/attribute_list_validator.rb
115
+ - lib/attribool/validators/condition_validator.rb
99
116
  - lib/attribool/validators/method_name_validator.rb
100
117
  - lib/attribool/validators/nil_attribute_validator.rb
101
118
  - lib/attribool/validators/strict_boolean_validator.rb
102
- - lib/attribool/validators/validator.rb
119
+ - lib/attribool/value.rb
103
120
  - lib/attribool/version.rb
104
121
  homepage: https://github.com/evanthegrayt/attribool
105
122
  licenses:
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Attribool::Validators
4
- class Validator
5
- def self.validate(*args)
6
- new(*args).validate
7
- end
8
-
9
- def initialize(*)
10
- raise "#{self.class} is an abstract class" if base_class?
11
- end
12
-
13
- def validate
14
- valid? || raise(error)
15
- end
16
-
17
- def valid?
18
- raise NoMethodError, "Validator must implement `valid?'"
19
- end
20
-
21
- def error
22
- raise NoMethodError, "Validator must implement `error'"
23
- end
24
-
25
- private
26
-
27
- def base_class?
28
- instance_of?(Attribool::Validators::Validator)
29
- end
30
- end
31
- end