attribool 1.0.3 → 2.0.0
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 +32 -1
- data/README.md +15 -18
- data/Rakefile +9 -9
- data/attribool.gemspec +22 -22
- data/lib/attribool/attribute.rb +34 -59
- data/lib/attribool/attribute_list.rb +30 -0
- data/lib/attribool/reader_name.rb +30 -0
- data/lib/attribool/validators/method_name_validator.rb +28 -0
- data/lib/attribool/validators/nil_attribute_validator.rb +20 -0
- data/lib/attribool/validators/strict_boolean_validator.rb +19 -0
- data/lib/attribool/validators/validator.rb +31 -0
- data/lib/attribool/version.rb +8 -6
- data/lib/attribool.rb +35 -19
- metadata +23 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cee1c79ed2d93c1f4d77b14f42d59dfd7c805afe569d9e43c999484d552c64ff
|
4
|
+
data.tar.gz: becb227aebe9b9878f40b3a3de4a9737623a10f364d2788830f3b440524ce0fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4904af389ad68348764f519eb058aaff66cb022dcc3de3d9f1ee3ce07b620eb08552bb9f03c9316943693370ed79131d11b91950433a4950bbdc4ce7619d34a4
|
7
|
+
data.tar.gz: 0f0d3622f6214668c8fa150d7e1b89ab828a803803aaa4707c25fd53b72a734d5e07f3ecc9d6d6a600c6e47522729ad0097da600065f76ad43fd6a2675efa203
|
data/Gemfile.lock
CHANGED
@@ -1,22 +1,52 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
attribool (
|
4
|
+
attribool (2.0.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
8
8
|
specs:
|
9
|
+
ast (2.4.2)
|
9
10
|
docile (1.3.5)
|
11
|
+
json (2.6.3)
|
12
|
+
language_server-protocol (3.17.0.2)
|
13
|
+
parallel (1.22.1)
|
14
|
+
parser (3.1.3.0)
|
15
|
+
ast (~> 2.4.1)
|
10
16
|
power_assert (2.0.0)
|
17
|
+
rainbow (3.1.1)
|
11
18
|
rake (13.0.3)
|
19
|
+
regexp_parser (2.6.1)
|
20
|
+
rexml (3.2.5)
|
21
|
+
rubocop (1.39.0)
|
22
|
+
json (~> 2.3)
|
23
|
+
parallel (~> 1.10)
|
24
|
+
parser (>= 3.1.2.1)
|
25
|
+
rainbow (>= 2.2.2, < 4.0)
|
26
|
+
regexp_parser (>= 1.8, < 3.0)
|
27
|
+
rexml (>= 3.2.5, < 4.0)
|
28
|
+
rubocop-ast (>= 1.23.0, < 2.0)
|
29
|
+
ruby-progressbar (~> 1.7)
|
30
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
31
|
+
rubocop-ast (1.24.0)
|
32
|
+
parser (>= 3.1.1.0)
|
33
|
+
rubocop-performance (1.15.1)
|
34
|
+
rubocop (>= 1.7.0, < 2.0)
|
35
|
+
rubocop-ast (>= 0.4.0)
|
36
|
+
ruby-progressbar (1.11.0)
|
12
37
|
simplecov (0.21.2)
|
13
38
|
docile (~> 1.1)
|
14
39
|
simplecov-html (~> 0.11)
|
15
40
|
simplecov_json_formatter (~> 0.1)
|
16
41
|
simplecov-html (0.12.3)
|
17
42
|
simplecov_json_formatter (0.1.2)
|
43
|
+
standard (1.19.1)
|
44
|
+
language_server-protocol (~> 3.17.0.2)
|
45
|
+
rubocop (= 1.39.0)
|
46
|
+
rubocop-performance (= 1.15.1)
|
18
47
|
test-unit (3.4.0)
|
19
48
|
power_assert
|
49
|
+
unicode-display_width (2.3.0)
|
20
50
|
|
21
51
|
PLATFORMS
|
22
52
|
ruby
|
@@ -25,6 +55,7 @@ DEPENDENCIES
|
|
25
55
|
attribool!
|
26
56
|
rake (~> 13.0, >= 13.0.1)
|
27
57
|
simplecov
|
58
|
+
standard
|
28
59
|
test-unit (~> 3.3, >= 3.3.5)
|
29
60
|
|
30
61
|
BUNDLED WITH
|
data/README.md
CHANGED
@@ -20,8 +20,6 @@ you'd like for your boolean method to be called `named?`, you can use
|
|
20
20
|
with lambdas via the `condition:` keyword argument.
|
21
21
|
|
22
22
|
The first argument is always the instance variable to check for truthiness.
|
23
|
-
Because of this, it is also valid syntax to use `bool_reader :@name`, if it
|
24
|
-
makes more sense to you.
|
25
23
|
|
26
24
|
Macros also exist for `bool_writer` and `bool_accessor`. When a writer
|
27
25
|
method is defined, the value will always be coerced into a boolean before
|
@@ -32,7 +30,7 @@ You can read the documentation [here](https://evanthegrayt.github.io/attribool/)
|
|
32
30
|
## Installation
|
33
31
|
#### Via Gemfile
|
34
32
|
```ruby
|
35
|
-
gem
|
33
|
+
gem "attribool"
|
36
34
|
```
|
37
35
|
|
38
36
|
#### Via rubygems
|
@@ -50,24 +48,20 @@ bundle exec rake install
|
|
50
48
|
## Examples
|
51
49
|
#### Standard bool_reader
|
52
50
|
```ruby
|
53
|
-
require
|
51
|
+
require "attribool"
|
54
52
|
|
55
53
|
class Person
|
56
54
|
extend Attribool
|
57
55
|
|
58
56
|
attr_accessor :name
|
59
57
|
bool_reader :name
|
60
|
-
# OR
|
61
|
-
# bool_reader :@name
|
62
|
-
# bool_reader 'name'
|
63
|
-
# bool_reader '@name'
|
64
58
|
end
|
65
59
|
|
66
60
|
person = Person.new
|
67
61
|
person.name?
|
68
62
|
# false, because @name is nil.
|
69
63
|
|
70
|
-
person.name =
|
64
|
+
person.name = "John Smith"
|
71
65
|
person.name
|
72
66
|
# "John Smith"
|
73
67
|
person.name?
|
@@ -76,7 +70,7 @@ person.name?
|
|
76
70
|
|
77
71
|
#### A bool_reader with method name
|
78
72
|
```ruby
|
79
|
-
require
|
73
|
+
require "attribool"
|
80
74
|
|
81
75
|
class Person
|
82
76
|
extend Attribool
|
@@ -89,14 +83,14 @@ person = Person.new
|
|
89
83
|
person.named?
|
90
84
|
# false, because @name is nil.
|
91
85
|
|
92
|
-
person.name =
|
86
|
+
person.name = "John Smith"
|
93
87
|
person.named?
|
94
88
|
# true, because @name is truthy.
|
95
89
|
```
|
96
90
|
|
97
91
|
#### A bool_reader with method name and conditional
|
98
92
|
```ruby
|
99
|
-
require
|
93
|
+
require "attribool"
|
100
94
|
|
101
95
|
class Person
|
102
96
|
extend Attribool
|
@@ -119,9 +113,12 @@ person.adult?
|
|
119
113
|
# true, because @age is greater than 18.
|
120
114
|
```
|
121
115
|
|
116
|
+
Note that, the result of the proc will also be coerced into a boolean, so the
|
117
|
+
condition can just return a truthy or falsey value.
|
118
|
+
|
122
119
|
#### Assigning more than one bool_reader with a method name at once
|
123
120
|
```ruby
|
124
|
-
require
|
121
|
+
require "attribool"
|
125
122
|
|
126
123
|
class Person
|
127
124
|
extend Attribool
|
@@ -142,14 +139,14 @@ person.age = 10
|
|
142
139
|
person.has_age?
|
143
140
|
# true, because @age is not nil.
|
144
141
|
|
145
|
-
person.name =
|
142
|
+
person.name = "Bob"
|
146
143
|
person.has_name?
|
147
144
|
# true, because @name is not nil.
|
148
145
|
```
|
149
146
|
|
150
147
|
#### Standard bool_accessor
|
151
148
|
```ruby
|
152
|
-
require
|
149
|
+
require "attribool"
|
153
150
|
|
154
151
|
class Person
|
155
152
|
extend Attribool
|
@@ -172,7 +169,7 @@ In most cases where you'd use a `bool_writer`, you'd probably want to just use
|
|
172
169
|
`bool_accessor`. This example is to show that, even when using `bool_accessor`,
|
173
170
|
the value is coerced to a boolean when the value is set by `bool_writer`.
|
174
171
|
```ruby
|
175
|
-
require
|
172
|
+
require "attribool"
|
176
173
|
|
177
174
|
class Person
|
178
175
|
extend Attribool
|
@@ -185,9 +182,9 @@ person = Person.new
|
|
185
182
|
person.living
|
186
183
|
# nil
|
187
184
|
|
188
|
-
person.living =
|
185
|
+
person.living = "blah"
|
189
186
|
person.living
|
190
|
-
# true, because
|
187
|
+
# true, because "blah" was coerced into a boolean because strings are truthy.
|
191
188
|
```
|
192
189
|
|
193
190
|
## Reporting Bugs and Requesting Features
|
data/Rakefile
CHANGED
@@ -1,21 +1,21 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
3
|
+
require_relative "lib/attribool"
|
4
|
+
require "bundler/gem_tasks"
|
5
|
+
require "rdoc/task"
|
6
|
+
require "rake/testtask"
|
7
7
|
|
8
8
|
Rake::TestTask.new do |t|
|
9
|
-
t.libs = [
|
9
|
+
t.libs = ["lib"]
|
10
10
|
t.warning = true
|
11
11
|
t.verbose = true
|
12
|
-
t.test_files = FileList[
|
12
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
13
13
|
end
|
14
14
|
|
15
15
|
RDoc::Task.new do |rdoc|
|
16
|
-
rdoc.main =
|
17
|
-
rdoc.rdoc_dir =
|
18
|
-
rdoc.rdoc_files.include(
|
16
|
+
rdoc.main = "README.md"
|
17
|
+
rdoc.rdoc_dir = "docs"
|
18
|
+
rdoc.rdoc_files.include("README.md", "lib/**/*.rb")
|
19
19
|
end
|
20
20
|
|
21
21
|
task default: :test
|
data/attribool.gemspec
CHANGED
@@ -1,34 +1,34 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative "lib/attribool/version"
|
2
2
|
|
3
3
|
Gem::Specification.new do |spec|
|
4
|
-
spec.name
|
5
|
-
spec.version
|
6
|
-
spec.authors
|
7
|
-
spec.email
|
8
|
-
spec.license
|
9
|
-
spec.date = Time.now.strftime('%Y-%m-%d')
|
4
|
+
spec.name = "attribool"
|
5
|
+
spec.version = Attribool::VERSION
|
6
|
+
spec.authors = ["Evan Gray"]
|
7
|
+
spec.email = "evanthegrayt@vivaldi.net"
|
8
|
+
spec.license = "MIT"
|
10
9
|
|
11
|
-
spec.summary
|
12
|
-
spec.description
|
13
|
-
spec.homepage
|
10
|
+
spec.summary = %(Macros for creating boolean attribute methods)
|
11
|
+
spec.description = %(Macros for creating boolean attribute methods)
|
12
|
+
spec.homepage = "https://github.com/evanthegrayt/attribool"
|
14
13
|
|
15
14
|
unless spec.respond_to?(:metadata)
|
16
|
-
raise
|
17
|
-
|
15
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
16
|
+
"public gem pushes."
|
18
17
|
end
|
19
18
|
|
20
|
-
spec.metadata[
|
21
|
-
spec.metadata[
|
22
|
-
spec.metadata[
|
23
|
-
spec.metadata[
|
19
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
20
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
21
|
+
spec.metadata["source_code_uri"] = "https://github.com/evanthegrayt/attribool"
|
22
|
+
spec.metadata["documentation_uri"] = "https://evanthegrayt.github.io/attribool/"
|
24
23
|
|
25
24
|
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
26
25
|
f.match(%r{^(test|spec|features)/})
|
27
26
|
end
|
28
|
-
spec.bindir
|
29
|
-
spec.executables
|
30
|
-
spec.require_paths = [
|
31
|
-
spec.add_development_dependency
|
32
|
-
spec.add_development_dependency
|
33
|
-
spec.add_development_dependency
|
27
|
+
spec.bindir = "bin"
|
28
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ["lib"]
|
30
|
+
spec.add_development_dependency "rake", "~> 13.0", ">= 13.0.1"
|
31
|
+
spec.add_development_dependency "simplecov"
|
32
|
+
spec.add_development_dependency "standard"
|
33
|
+
spec.add_development_dependency "test-unit", "~> 3.3", ">= 3.3.5"
|
34
34
|
end
|
data/lib/attribool/attribute.rb
CHANGED
@@ -1,72 +1,47 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
# Abstraction for an attribute to determine its name, reader, writer, and
|
5
|
-
# instance variable name.
|
6
|
-
class Attribool::Attribute
|
3
|
+
module Attribool
|
7
4
|
##
|
8
|
-
#
|
9
|
-
#
|
10
|
-
|
11
|
-
|
5
|
+
# Abstraction for an attribute to determine its name, reader, writer, and
|
6
|
+
# instance variable name.
|
7
|
+
class Attribute
|
8
|
+
##
|
9
|
+
# The name of the attribute, without the leading "@".
|
10
|
+
#
|
11
|
+
# @return [String]
|
12
|
+
attr_reader :name
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
##
|
15
|
+
# The name of the instance variable for the attribute, with the leading "@".
|
16
|
+
#
|
17
|
+
# @return [String]
|
18
|
+
attr_reader :ivar
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
20
|
+
##
|
21
|
+
# The name of the reader for the attribute.
|
22
|
+
#
|
23
|
+
# @return [String]
|
24
|
+
attr_reader :reader
|
24
25
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
26
|
+
##
|
27
|
+
# The name of the writer for the attribute.
|
28
|
+
#
|
29
|
+
# @return [String]
|
30
|
+
attr_reader :writer
|
30
31
|
|
31
|
-
class << self
|
32
32
|
##
|
33
|
-
#
|
34
|
-
# is provided, that +method_name+ is a +Proc+.
|
33
|
+
# Create an Attribute. The attribute can either be a String or a Symbol.
|
35
34
|
#
|
36
|
-
# @param [
|
35
|
+
# @param [String, Symbol] attribute
|
37
36
|
#
|
38
|
-
# @param [String, Symbol, Proc]
|
39
|
-
def
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
def nil_or_proc?(method_name) # :nodoc:
|
48
|
-
method_name.nil? || (method_name.is_a?(Proc) && method_name.arity == 1)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
##
|
53
|
-
# Create an Attribute. The attribute can either be a String or a Symbol, and
|
54
|
-
# can also start with an "@", or not.
|
55
|
-
#
|
56
|
-
# @param [String, Symbol] attribute
|
57
|
-
#
|
58
|
-
# @param [String, Symbol, Proc, nil] reader_name
|
59
|
-
def initialize(attribute, reader_name = nil)
|
60
|
-
attribute.to_s.tap do |a|
|
61
|
-
@ivar = a.start_with?('@') ? a : "@#{a}"
|
62
|
-
@name = @ivar.delete_prefix('@')
|
63
|
-
@reader =
|
64
|
-
case reader_name
|
65
|
-
when Proc then reader_name.call(name)
|
66
|
-
when nil then "#{name}?"
|
67
|
-
else reader_name.to_s
|
68
|
-
end
|
69
|
-
@writer = "#{name}="
|
37
|
+
# @param [String, Symbol, Proc, nil] reader_name
|
38
|
+
def initialize(attribute, reader_name = nil)
|
39
|
+
attribute.to_s.then do |a|
|
40
|
+
@name = a
|
41
|
+
@ivar = "@#{a}"
|
42
|
+
@reader = Attribool::ReaderName.new(name, reader_name).to_s
|
43
|
+
@writer = "#{name}="
|
44
|
+
end
|
70
45
|
end
|
71
46
|
end
|
72
47
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Attribool
|
4
|
+
##
|
5
|
+
# Enumerable class that generates a list of attributes from a list of strings
|
6
|
+
# or symbols.
|
7
|
+
class AttributeList
|
8
|
+
include Enumerable
|
9
|
+
|
10
|
+
##
|
11
|
+
# Generate the list.
|
12
|
+
#
|
13
|
+
# @param [String, Symbol] *attributes
|
14
|
+
#
|
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) }
|
18
|
+
end
|
19
|
+
|
20
|
+
##
|
21
|
+
# Yield every entry in the list to a block.
|
22
|
+
#
|
23
|
+
# @param [block] &block
|
24
|
+
#
|
25
|
+
# @return [Enumerable]
|
26
|
+
def each(&block)
|
27
|
+
@entries.each(&block)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Attribool
|
4
|
+
##
|
5
|
+
# Abstraction of a reader's method name.
|
6
|
+
class ReaderName
|
7
|
+
##
|
8
|
+
# Instantiate new instance of ReaderName.
|
9
|
+
#
|
10
|
+
# @param [String, Syumbol] attribute
|
11
|
+
#
|
12
|
+
# @param [proc, nil, String, Symbol] name
|
13
|
+
def initialize(attribute, name)
|
14
|
+
@attribute = attribute
|
15
|
+
@name = name
|
16
|
+
end
|
17
|
+
|
18
|
+
##
|
19
|
+
# Convert instance to a string.
|
20
|
+
#
|
21
|
+
# @return [String]
|
22
|
+
def to_s
|
23
|
+
case @name
|
24
|
+
when Proc then @name.call(@attribute)
|
25
|
+
when nil then "#{@attribute}?"
|
26
|
+
else @name.to_s
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Attribool::Validators
|
4
|
+
##
|
5
|
+
# Ensures that if multiple attributes are being defined, and +method_name+
|
6
|
+
# is provided, that +method_name+ is a +Proc+.
|
7
|
+
class MethodNameValidator < Attribool::Validators::Validator
|
8
|
+
def initialize(method_name, number_of_attributes)
|
9
|
+
@method_name = method_name
|
10
|
+
@number_of_attributes = number_of_attributes
|
11
|
+
super
|
12
|
+
end
|
13
|
+
|
14
|
+
def valid?
|
15
|
+
@number_of_attributes == 1 || nil_or_proc?
|
16
|
+
end
|
17
|
+
|
18
|
+
def error
|
19
|
+
ArgumentError.new("Must use a Proc when creating multiple methods")
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def nil_or_proc? # :nodoc:
|
25
|
+
@method_name.nil? || (@method_name.is_a?(Proc) && @method_name.arity == 1)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Attribool::Validators
|
4
|
+
class NilAttributeValidator < Attribool::Validators::Validator
|
5
|
+
def initialize(ivar, value, allow_nil)
|
6
|
+
@ivar = ivar
|
7
|
+
@value = value
|
8
|
+
@allow_nil = allow_nil
|
9
|
+
super
|
10
|
+
end
|
11
|
+
|
12
|
+
def valid?
|
13
|
+
@allow_nil || !@value.nil?
|
14
|
+
end
|
15
|
+
|
16
|
+
def error
|
17
|
+
TypeError.new("#{@ivar} is nil")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Attribool::Validators
|
4
|
+
class StrictBooleanValidator < Attribool::Validators::Validator
|
5
|
+
def initialize(value, strict)
|
6
|
+
@value = value
|
7
|
+
@strict = strict
|
8
|
+
super
|
9
|
+
end
|
10
|
+
|
11
|
+
def valid?
|
12
|
+
!@strict || [TrueClass, FalseClass].include?(@value.class)
|
13
|
+
end
|
14
|
+
|
15
|
+
def error
|
16
|
+
ArgumentError.new("#{@value} is not a boolean")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,31 @@
|
|
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
|
data/lib/attribool/version.rb
CHANGED
@@ -9,7 +9,7 @@ module Attribool
|
|
9
9
|
# Major version.
|
10
10
|
#
|
11
11
|
# @return [Integer]
|
12
|
-
MAJOR =
|
12
|
+
MAJOR = 2
|
13
13
|
|
14
14
|
##
|
15
15
|
# Minor version.
|
@@ -21,13 +21,15 @@ module Attribool
|
|
21
21
|
# Patch version.
|
22
22
|
#
|
23
23
|
# @return [Integer]
|
24
|
-
PATCH =
|
24
|
+
PATCH = 0
|
25
|
+
|
26
|
+
module_function
|
25
27
|
|
26
28
|
##
|
27
29
|
# Version as +[MAJOR, MINOR, PATCH]+
|
28
30
|
#
|
29
31
|
# @return [Array]
|
30
|
-
def
|
32
|
+
def to_a
|
31
33
|
[MAJOR, MINOR, PATCH]
|
32
34
|
end
|
33
35
|
|
@@ -35,15 +37,15 @@ module Attribool
|
|
35
37
|
# Version as +MAJOR.MINOR.PATCH+
|
36
38
|
#
|
37
39
|
# @return [String]
|
38
|
-
def
|
39
|
-
to_a.join(
|
40
|
+
def to_s
|
41
|
+
to_a.join(".")
|
40
42
|
end
|
41
43
|
|
42
44
|
##
|
43
45
|
# Version as +{major: MAJOR, minor: MINOR, patch: PATCH}+
|
44
46
|
#
|
45
47
|
# @return [Hash]
|
46
|
-
def
|
48
|
+
def to_h
|
47
49
|
%i[major minor patch].zip(to_a).to_h
|
48
50
|
end
|
49
51
|
end
|
data/lib/attribool.rb
CHANGED
@@ -1,10 +1,32 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative
|
4
|
-
require_relative
|
3
|
+
require_relative "attribool/version"
|
4
|
+
require_relative "attribool/attribute"
|
5
|
+
require_relative "attribool/reader_name"
|
6
|
+
require_relative "attribool/attribute_list"
|
7
|
+
require_relative "attribool/validators/validator"
|
8
|
+
require_relative "attribool/validators/method_name_validator"
|
9
|
+
require_relative "attribool/validators/nil_attribute_validator"
|
10
|
+
require_relative "attribool/validators/strict_boolean_validator"
|
5
11
|
|
6
12
|
##
|
7
13
|
# Adds macros for dealing with boolean attributes.
|
14
|
+
#
|
15
|
+
# @example
|
16
|
+
# require "attribool"
|
17
|
+
# class Person
|
18
|
+
# extend Attribool
|
19
|
+
# attr_accessor :name
|
20
|
+
# bool_reader :name
|
21
|
+
# ends
|
22
|
+
# person = Person.new
|
23
|
+
# person.name?
|
24
|
+
# # false, because @name is nil.
|
25
|
+
# person.name = "John Smith"
|
26
|
+
# person.name
|
27
|
+
# # "John Smith"
|
28
|
+
# person.name?
|
29
|
+
# # true, because @name is truthy.
|
8
30
|
module Attribool
|
9
31
|
##
|
10
32
|
# Creates methods that return a boolean for attributes that may or may not be
|
@@ -17,20 +39,16 @@ module Attribool
|
|
17
39
|
# @kwarg [Proc] condition
|
18
40
|
#
|
19
41
|
# @kwarg [Symbol, String, Proc] method_name
|
20
|
-
def bool_reader(
|
21
|
-
|
22
|
-
allow_nil: true,
|
23
|
-
condition: nil,
|
24
|
-
method_name: nil
|
25
|
-
)
|
26
|
-
Attribute.validate_method_name(method_name, attributes.size)
|
42
|
+
def bool_reader(*attributes, allow_nil: true, method_name: nil, condition: nil)
|
43
|
+
Validators::MethodNameValidator.validate(method_name, attributes.size)
|
27
44
|
|
28
|
-
|
45
|
+
AttributeList.new(*attributes, method_name: method_name).each do |attribute|
|
29
46
|
define_method(attribute.reader) do
|
30
|
-
|
31
|
-
|
47
|
+
instance_variable_get(attribute.ivar).then do |value|
|
48
|
+
Validators::NilAttributeValidator.validate(attribute.ivar, value, allow_nil)
|
32
49
|
|
33
|
-
|
50
|
+
!!(condition ? condition.call(value) : value)
|
51
|
+
end
|
34
52
|
end
|
35
53
|
end
|
36
54
|
end
|
@@ -43,13 +61,11 @@ module Attribool
|
|
43
61
|
#
|
44
62
|
# @kwarg [Boolean] strict
|
45
63
|
def bool_writer(*attributes, strict: false)
|
46
|
-
|
47
|
-
define_method(attribute.writer) do |
|
48
|
-
|
49
|
-
raise ArgumentError, 'Argument must be a boolean'
|
50
|
-
end
|
64
|
+
AttributeList.new(*attributes).each do |attribute|
|
65
|
+
define_method(attribute.writer) do |value|
|
66
|
+
Validators::StrictBooleanValidator.validate(value, strict)
|
51
67
|
|
52
|
-
instance_variable_set(attribute.ivar, !!
|
68
|
+
instance_variable_set(attribute.ivar, !!value)
|
53
69
|
end
|
54
70
|
end
|
55
71
|
end
|
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:
|
4
|
+
version: 2.0.0
|
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-
|
11
|
+
date: 2022-12-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -44,6 +44,20 @@ dependencies:
|
|
44
44
|
- - ">="
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: '0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: standard
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
47
61
|
- !ruby/object:Gem::Dependency
|
48
62
|
name: test-unit
|
49
63
|
requirement: !ruby/object:Gem::Requirement
|
@@ -80,6 +94,12 @@ files:
|
|
80
94
|
- attribool.gemspec
|
81
95
|
- lib/attribool.rb
|
82
96
|
- lib/attribool/attribute.rb
|
97
|
+
- lib/attribool/attribute_list.rb
|
98
|
+
- lib/attribool/reader_name.rb
|
99
|
+
- lib/attribool/validators/method_name_validator.rb
|
100
|
+
- lib/attribool/validators/nil_attribute_validator.rb
|
101
|
+
- lib/attribool/validators/strict_boolean_validator.rb
|
102
|
+
- lib/attribool/validators/validator.rb
|
83
103
|
- lib/attribool/version.rb
|
84
104
|
homepage: https://github.com/evanthegrayt/attribool
|
85
105
|
licenses:
|
@@ -104,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
124
|
- !ruby/object:Gem::Version
|
105
125
|
version: '0'
|
106
126
|
requirements: []
|
107
|
-
rubygems_version: 3.
|
127
|
+
rubygems_version: 3.3.7
|
108
128
|
signing_key:
|
109
129
|
specification_version: 4
|
110
130
|
summary: Macros for creating boolean attribute methods
|