attribool 2.0.0 → 2.0.1

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: f70889d0edf2407bfaee163da31ab791d35bea5921024e54fbab658792c81d46
4
+ data.tar.gz: 0ed3735087e12402627c1773564efa42e2710696d9e932dcd5073a369cf0f6d8
5
5
  SHA512:
6
- metadata.gz: 4904af389ad68348764f519eb058aaff66cb022dcc3de3d9f1ee3ce07b620eb08552bb9f03c9316943693370ed79131d11b91950433a4950bbdc4ce7619d34a4
7
- data.tar.gz: 0f0d3622f6214668c8fa150d7e1b89ab828a803803aaa4707c25fd53b72a734d5e07f3ecc9d6d6a600c6e47522729ad0097da600065f76ad43fd6a2675efa203
6
+ metadata.gz: df790e07a5164be0da050dce51747bfa73a5884637304854706ffc5948347fc4faf3cb300a8cef35849b3ad65f145813ee8dc3a98fde5234fb8fb0fd7c493a36
7
+ data.tar.gz: b6740fa6386e012e9a39040abde54e9e091f194ac3c1b7bc1a4127d72cd4eff829b27d76a1ee1c34e507330dfcb0804555d7aba98980608851b7e4ccf6522f88
@@ -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.1)
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"
@@ -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 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 = 1
25
25
 
26
26
  module_function
27
27
 
data/lib/attribool.rb CHANGED
@@ -1,10 +1,12 @@
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"
@@ -40,14 +42,14 @@ module Attribool
40
42
  #
41
43
  # @kwarg [Symbol, String, Proc] method_name
42
44
  def bool_reader(*attributes, allow_nil: true, method_name: nil, condition: nil)
43
- Validators::MethodNameValidator.validate(method_name, attributes.size)
45
+ ValidatorService.call(:method_name, method_name, attributes.size)
44
46
 
45
47
  AttributeList.new(*attributes, method_name: method_name).each do |attribute|
46
48
  define_method(attribute.reader) do
47
49
  instance_variable_get(attribute.ivar).then do |value|
48
- Validators::NilAttributeValidator.validate(attribute.ivar, value, allow_nil)
50
+ ValidatorService.call(:nil_attribute, attribute.ivar, value, allow_nil)
49
51
 
50
- !!(condition ? condition.call(value) : value)
52
+ Value.new(value, condition).to_boolean
51
53
  end
52
54
  end
53
55
  end
@@ -63,9 +65,9 @@ module Attribool
63
65
  def bool_writer(*attributes, strict: false)
64
66
  AttributeList.new(*attributes).each do |attribute|
65
67
  define_method(attribute.writer) do |value|
66
- Validators::StrictBooleanValidator.validate(value, strict)
68
+ ValidatorService.call(:strict_boolean, value, strict)
67
69
 
68
- instance_variable_set(attribute.ivar, !!value)
70
+ instance_variable_set(attribute.ivar, Value.new(value).to_boolean)
69
71
  end
70
72
  end
71
73
  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.1
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-05-26 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,12 @@ 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/condition_validator.rb
99
115
  - lib/attribool/validators/method_name_validator.rb
100
116
  - lib/attribool/validators/nil_attribute_validator.rb
101
117
  - lib/attribool/validators/strict_boolean_validator.rb
102
- - lib/attribool/validators/validator.rb
118
+ - lib/attribool/value.rb
103
119
  - lib/attribool/version.rb
104
120
  homepage: https://github.com/evanthegrayt/attribool
105
121
  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