bool_attr_accessor 0.1.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 +7 -0
- data/.editorconfig +12 -0
- data/.github/workflows/ci.yml +31 -0
- data/.gitignore +11 -0
- data/.rspec +4 -0
- data/.rubocop.yml +39 -0
- data/CHANGELOG.md +10 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +82 -0
- data/LICENSE.txt +22 -0
- data/README.md +121 -0
- data/Rakefile +15 -0
- data/benchmark.rb +75 -0
- data/bool_attr_accessor.gemspec +35 -0
- data/lib/bool_attr_accessor/implementation.rb +81 -0
- data/lib/bool_attr_accessor/version.rb +7 -0
- data/lib/bool_attr_accessor.rb +9 -0
- data/lib/module_extensions.rb +54 -0
- metadata +213 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 325763577b43f1057a205883c0805c8e906d38ad41ba7a4a3b37bacd05a2e659
|
|
4
|
+
data.tar.gz: e36df5d098cb69bb6f418e597966b73878a4a6d55605388793a43c112abfe69f
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: b851d57ff5ef148d3f63224b73ae478658945cd0429b7641748974e15977f79a8e6e4b3e5110cf23dfe1ed9fa6a165a7d8b1b18f1b6181f82f3e39d47f5aa61d
|
|
7
|
+
data.tar.gz: 320a631fa52ae0b5300817e3354c2752100619c8b184a5887cc37c13a3079231972ea986da11a022ba2b76a6d022c793ea8ab9e32e27bc740513e10277d47cc5
|
data/.editorconfig
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on: [push, pull_request]
|
|
4
|
+
|
|
5
|
+
jobs:
|
|
6
|
+
test:
|
|
7
|
+
runs-on: ubuntu-latest
|
|
8
|
+
|
|
9
|
+
# We want to run on external PRs, but not on our own internal PRs as they'll be run on push event
|
|
10
|
+
if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != 'marcrohloff/bool_attr_accessor'
|
|
11
|
+
|
|
12
|
+
strategy:
|
|
13
|
+
fail-fast: false
|
|
14
|
+
matrix:
|
|
15
|
+
ruby: ["2.5", "2.6", "2.7", "3.0"]
|
|
16
|
+
|
|
17
|
+
name: ${{ matrix.ruby }}
|
|
18
|
+
|
|
19
|
+
steps:
|
|
20
|
+
- uses: actions/checkout@v2
|
|
21
|
+
|
|
22
|
+
- uses: ruby/setup-ruby@v1
|
|
23
|
+
with:
|
|
24
|
+
ruby-version: ${{ matrix.ruby }}
|
|
25
|
+
bundler-cache: true
|
|
26
|
+
|
|
27
|
+
- run: bundle exec rake
|
|
28
|
+
|
|
29
|
+
- uses: coverallsapp/github-action@master
|
|
30
|
+
with:
|
|
31
|
+
github-token: ${{ secrets.GITHUB_TOKEN }}
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
|
|
2
|
+
require:
|
|
3
|
+
- rubocop-rake
|
|
4
|
+
- rubocop-rspec
|
|
5
|
+
- rubocop-performance
|
|
6
|
+
|
|
7
|
+
inherit_mode:
|
|
8
|
+
merge:
|
|
9
|
+
- Exclude
|
|
10
|
+
|
|
11
|
+
AllCops:
|
|
12
|
+
NewCops: enable
|
|
13
|
+
DisplayCopNames: true
|
|
14
|
+
TargetRubyVersion: '2.5'
|
|
15
|
+
|
|
16
|
+
Layout/EmptyLinesAroundBlockBody:
|
|
17
|
+
Enabled: false
|
|
18
|
+
|
|
19
|
+
Layout/EmptyLinesAroundClassBody:
|
|
20
|
+
EnforcedStyle: empty_lines_except_namespace
|
|
21
|
+
|
|
22
|
+
Layout/EmptyLinesAroundModuleBody:
|
|
23
|
+
EnforcedStyle: empty_lines_except_namespace
|
|
24
|
+
|
|
25
|
+
Layout/ExtraSpacing:
|
|
26
|
+
AllowBeforeTrailingComments: true
|
|
27
|
+
|
|
28
|
+
Metrics/BlockLength:
|
|
29
|
+
Exclude:
|
|
30
|
+
- spec/**/*_spec.rb
|
|
31
|
+
|
|
32
|
+
Naming/MethodParameterName:
|
|
33
|
+
AllowedNames: ["x", "y", "z"]
|
|
34
|
+
|
|
35
|
+
RSpec/ExampleLength:
|
|
36
|
+
Max: 20
|
|
37
|
+
|
|
38
|
+
RSpec/MultipleExpectations:
|
|
39
|
+
Enabled: false
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
## [Unreleased]
|
|
2
|
+
|
|
3
|
+
## [v0.1.0] - 2021-10-26
|
|
4
|
+
- Initial version. ([@marcrohloff])
|
|
5
|
+
|
|
6
|
+
[v0.1.0]: https://github.com/marcrohloff/bool_attr_accessor/tree/v0.1.0
|
|
7
|
+
[Unreleased]: https://github.com/marcrohloff/bool_attr_accessor/compare/v0.1.0...HEAD
|
|
8
|
+
|
|
9
|
+
[@marcrohloff]: https://github.com/marcrohloff
|
|
10
|
+
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
bool_attr_accessor (0.1.0)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
ast (2.4.2)
|
|
10
|
+
benchmark-ips (2.9.2)
|
|
11
|
+
benchmark-memory (0.1.2)
|
|
12
|
+
memory_profiler (~> 0.9)
|
|
13
|
+
diff-lcs (1.4.4)
|
|
14
|
+
docile (1.4.0)
|
|
15
|
+
memory_profiler (0.9.14)
|
|
16
|
+
parallel (1.21.0)
|
|
17
|
+
parser (3.0.2.0)
|
|
18
|
+
ast (~> 2.4.1)
|
|
19
|
+
rainbow (3.0.0)
|
|
20
|
+
rake (13.0.6)
|
|
21
|
+
regexp_parser (2.1.1)
|
|
22
|
+
rexml (3.2.5)
|
|
23
|
+
rspec (3.10.0)
|
|
24
|
+
rspec-core (~> 3.10.0)
|
|
25
|
+
rspec-expectations (~> 3.10.0)
|
|
26
|
+
rspec-mocks (~> 3.10.0)
|
|
27
|
+
rspec-core (3.10.1)
|
|
28
|
+
rspec-support (~> 3.10.0)
|
|
29
|
+
rspec-expectations (3.10.1)
|
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
31
|
+
rspec-support (~> 3.10.0)
|
|
32
|
+
rspec-mocks (3.10.2)
|
|
33
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
34
|
+
rspec-support (~> 3.10.0)
|
|
35
|
+
rspec-support (3.10.2)
|
|
36
|
+
rubocop (1.22.2)
|
|
37
|
+
parallel (~> 1.10)
|
|
38
|
+
parser (>= 3.0.0.0)
|
|
39
|
+
rainbow (>= 2.2.2, < 4.0)
|
|
40
|
+
regexp_parser (>= 1.8, < 3.0)
|
|
41
|
+
rexml
|
|
42
|
+
rubocop-ast (>= 1.12.0, < 2.0)
|
|
43
|
+
ruby-progressbar (~> 1.7)
|
|
44
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
|
45
|
+
rubocop-ast (1.12.0)
|
|
46
|
+
parser (>= 3.0.1.1)
|
|
47
|
+
rubocop-performance (1.11.5)
|
|
48
|
+
rubocop (>= 1.7.0, < 2.0)
|
|
49
|
+
rubocop-ast (>= 0.4.0)
|
|
50
|
+
rubocop-rake (0.6.0)
|
|
51
|
+
rubocop (~> 1.0)
|
|
52
|
+
rubocop-rspec (2.5.0)
|
|
53
|
+
rubocop (~> 1.19)
|
|
54
|
+
ruby-progressbar (1.11.0)
|
|
55
|
+
simplecov (0.21.2)
|
|
56
|
+
docile (~> 1.1)
|
|
57
|
+
simplecov-html (~> 0.11)
|
|
58
|
+
simplecov_json_formatter (~> 0.1)
|
|
59
|
+
simplecov-html (0.12.3)
|
|
60
|
+
simplecov-lcov (0.8.0)
|
|
61
|
+
simplecov_json_formatter (0.1.3)
|
|
62
|
+
unicode-display_width (2.1.0)
|
|
63
|
+
|
|
64
|
+
PLATFORMS
|
|
65
|
+
ruby
|
|
66
|
+
|
|
67
|
+
DEPENDENCIES
|
|
68
|
+
benchmark-ips
|
|
69
|
+
benchmark-memory
|
|
70
|
+
bool_attr_accessor!
|
|
71
|
+
bundler
|
|
72
|
+
rake
|
|
73
|
+
rspec
|
|
74
|
+
rubocop
|
|
75
|
+
rubocop-performance
|
|
76
|
+
rubocop-rake
|
|
77
|
+
rubocop-rspec
|
|
78
|
+
simplecov
|
|
79
|
+
simplecov-lcov
|
|
80
|
+
|
|
81
|
+
BUNDLED WITH
|
|
82
|
+
2.1.4
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Marc Rohloff
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
|
22
|
+
|
data/README.md
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# attr_bool_accessor [](https://badge.fury.io/rb/bool_attr_accessor) [](https://github.com/marcrohloff/bool_attr_accessor) [](https://coveralls.io/github/marcrohloff/bool_attr_accessor?branch=main)
|
|
2
|
+
|
|
3
|
+
**attr_bool_accessor** is a gem for adding boolean style attributes to classes.
|
|
4
|
+
|
|
5
|
+
For example:
|
|
6
|
+
|
|
7
|
+
```ruby
|
|
8
|
+
class User
|
|
9
|
+
battr_accessor :enabled
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
u = User.new
|
|
13
|
+
u.enabled?
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
Just like any other gem. Just add
|
|
19
|
+
```
|
|
20
|
+
gem "attr_bool_accessor"
|
|
21
|
+
```
|
|
22
|
+
to your Gemfile or run
|
|
23
|
+
```
|
|
24
|
+
gem install attr_bool_accessor
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
For basic usage it can be used similarly to an `attr_accessor`
|
|
31
|
+
|
|
32
|
+
```ruby
|
|
33
|
+
class User
|
|
34
|
+
battr_accessor :enabled
|
|
35
|
+
end
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Which provides some basic accessor methods:
|
|
39
|
+
```ruby
|
|
40
|
+
u = User.new
|
|
41
|
+
u.enabled? # => false
|
|
42
|
+
u.enabled! # set it to true
|
|
43
|
+
u.enabled? # => true
|
|
44
|
+
u.enabled = false # set it to a value
|
|
45
|
+
u.enabled? # => false
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
The *query* method `attribute?` always returns a `true` or `false` value.
|
|
49
|
+
The *bang* method (`attribute!`) sets the attribute to `true`.
|
|
50
|
+
The *writer* method (`attribute=`) allows you to set the attribute. Values will be converted to boolean.
|
|
51
|
+
|
|
52
|
+
You can provide multiple attribute names, use strings and use names that include a trailing question mark
|
|
53
|
+
|
|
54
|
+
```ruby
|
|
55
|
+
class User
|
|
56
|
+
battr_accessor :first, :second, 'third', :fourth?
|
|
57
|
+
end
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
You can also use `attr_boolean` as an alias for `battr_accessor`
|
|
61
|
+
|
|
62
|
+
```ruby
|
|
63
|
+
class User
|
|
64
|
+
attr_boolean :enabled
|
|
65
|
+
end
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
##### Options
|
|
69
|
+
|
|
70
|
+
The following options are accepted:
|
|
71
|
+
|
|
72
|
+
**`default`**:
|
|
73
|
+
sets the default (initial) value for the attribute (`false` by default)
|
|
74
|
+
|
|
75
|
+
**`bang`**:
|
|
76
|
+
Set to `false` to disable the bang (`attribute!`) method (`true` by default)
|
|
77
|
+
|
|
78
|
+
**`writer`**:
|
|
79
|
+
Set to `false` to disable the writer (`attribute=`) method (by default the writer is enabled if the bang method is enabled)
|
|
80
|
+
|
|
81
|
+
**`reader`**:
|
|
82
|
+
Set to `true` to disable the reader (`attribute`) method (this is enabled if the `raw` option is set)
|
|
83
|
+
|
|
84
|
+
**`raw`**:
|
|
85
|
+
Enables raw mode.
|
|
86
|
+
This makes a few changes.
|
|
87
|
+
Default and assigned values are stored internally in the value provided and not converted to boolean values
|
|
88
|
+
A `reader` method is added that allows you to read the raw value.
|
|
89
|
+
The query method will always return a boolean.
|
|
90
|
+
|
|
91
|
+
##### Examples
|
|
92
|
+
|
|
93
|
+
```ruby
|
|
94
|
+
class User
|
|
95
|
+
battr_accessor :enabled, default: true, writer: false
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
class Account
|
|
99
|
+
battr_accessor :state, default: 3, raw: true
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
a = Account.new
|
|
103
|
+
a.state # => 3
|
|
104
|
+
a.state? # => true
|
|
105
|
+
a.state = nil
|
|
106
|
+
a.state # => nil
|
|
107
|
+
a.state? # => false
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
## Contributing
|
|
112
|
+
|
|
113
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/marcrohloff/attr_bool_accessor.
|
|
114
|
+
|
|
115
|
+
## License
|
|
116
|
+
|
|
117
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
118
|
+
|
|
119
|
+
## Author
|
|
120
|
+
|
|
121
|
+
Created by Yuri Smirnov.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'bundler/gem_tasks'
|
|
4
|
+
require 'rspec/core/rake_task'
|
|
5
|
+
require 'rubocop/rake_task'
|
|
6
|
+
|
|
7
|
+
RSpec::Core::RakeTask.new(:spec)
|
|
8
|
+
RuboCop::RakeTask.new(:lint)
|
|
9
|
+
|
|
10
|
+
task default: %i[lint spec]
|
|
11
|
+
|
|
12
|
+
desc 'Run benchmarks'
|
|
13
|
+
task :benchmark do
|
|
14
|
+
require_relative './benchmark'
|
|
15
|
+
end
|
data/benchmark.rb
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'bundler/setup'
|
|
4
|
+
Bundler.setup
|
|
5
|
+
|
|
6
|
+
require 'benchmark'
|
|
7
|
+
require 'benchmark/ips'
|
|
8
|
+
require 'benchmark/memory'
|
|
9
|
+
|
|
10
|
+
puts '```ruby'
|
|
11
|
+
puts File.read(__FILE__)
|
|
12
|
+
puts '```'
|
|
13
|
+
puts
|
|
14
|
+
puts '### Output'
|
|
15
|
+
puts
|
|
16
|
+
puts '```'
|
|
17
|
+
|
|
18
|
+
require_relative 'lib/bool_attr_accessor'
|
|
19
|
+
|
|
20
|
+
class Foo
|
|
21
|
+
|
|
22
|
+
class << self
|
|
23
|
+
|
|
24
|
+
attr_boolean :bool
|
|
25
|
+
attr_accessor :direct
|
|
26
|
+
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def test_read_direct
|
|
32
|
+
Foo.direct
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def test_read_bool
|
|
36
|
+
Foo.bool?
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def test_write_direct
|
|
40
|
+
Foo.direct = true
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def test_write_bool
|
|
44
|
+
Foo.bool = true
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
Benchmark.ips do |x|
|
|
48
|
+
x.report('test_read_direct') { test_read_direct }
|
|
49
|
+
end
|
|
50
|
+
Benchmark.memory do |x|
|
|
51
|
+
x.report('test_read_direct') { 100.times { test_read_direct } }
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
Benchmark.ips do |x|
|
|
55
|
+
x.report('test_read_bool') { test_read_bool }
|
|
56
|
+
end
|
|
57
|
+
Benchmark.memory do |x|
|
|
58
|
+
x.report('test_read_bool') { 100.times { test_read_bool } }
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
Benchmark.ips do |x|
|
|
62
|
+
x.report('test_write_direct') { test_write_direct }
|
|
63
|
+
end
|
|
64
|
+
Benchmark.memory do |x|
|
|
65
|
+
x.report('test_write_direct') { 100.times { test_write_direct } }
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
Benchmark.ips do |x|
|
|
69
|
+
x.report('test_write_bool') { test_write_bool }
|
|
70
|
+
end
|
|
71
|
+
Benchmark.memory do |x|
|
|
72
|
+
x.report('test_write_bool') { 100.times { test_write_bool } }
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
puts '```'
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
5
|
+
require 'bool_attr_accessor/version'
|
|
6
|
+
|
|
7
|
+
Gem::Specification.new do |spec|
|
|
8
|
+
spec.required_ruby_version = '>= 2.5.0'
|
|
9
|
+
|
|
10
|
+
spec.name = 'bool_attr_accessor'
|
|
11
|
+
spec.version = BoolAttrAccessor::VERSION
|
|
12
|
+
spec.authors = ['Marc Rohloff']
|
|
13
|
+
|
|
14
|
+
spec.summary = 'A gem for creating boolean attributes.'
|
|
15
|
+
spec.description = 'bool_attr_accessor is a gem for creating boolean style attributes.'
|
|
16
|
+
spec.homepage = 'https://github.com/marcrohloff/bool_attr_accessor'
|
|
17
|
+
spec.license = 'MIT'
|
|
18
|
+
|
|
19
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
|
20
|
+
f.match(%r{^(test|spec|features)/})
|
|
21
|
+
end
|
|
22
|
+
spec.require_paths = ['lib']
|
|
23
|
+
|
|
24
|
+
spec.add_development_dependency 'benchmark-ips'
|
|
25
|
+
spec.add_development_dependency 'benchmark-memory'
|
|
26
|
+
spec.add_development_dependency 'bundler'
|
|
27
|
+
spec.add_development_dependency 'rake'
|
|
28
|
+
spec.add_development_dependency 'rspec'
|
|
29
|
+
spec.add_development_dependency 'rubocop'
|
|
30
|
+
spec.add_development_dependency 'rubocop-performance'
|
|
31
|
+
spec.add_development_dependency 'rubocop-rake'
|
|
32
|
+
spec.add_development_dependency 'rubocop-rspec'
|
|
33
|
+
spec.add_development_dependency 'simplecov'
|
|
34
|
+
spec.add_development_dependency 'simplecov-lcov'
|
|
35
|
+
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module BoolAttrAccessor
|
|
4
|
+
class Implementation # rubocop:disable Style/Documentation
|
|
5
|
+
|
|
6
|
+
attr_reader :raw, :reader, :writer, :bang
|
|
7
|
+
|
|
8
|
+
def initialize(mod, raw, reader, writer, bang)
|
|
9
|
+
@module = mod
|
|
10
|
+
@raw = raw
|
|
11
|
+
@reader = reader
|
|
12
|
+
@writer = writer
|
|
13
|
+
@bang = bang
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def create(attribute, default)
|
|
17
|
+
attribute_base = check_name!(attribute)
|
|
18
|
+
ivar_name = "@#{attribute_base}"
|
|
19
|
+
|
|
20
|
+
make_query_method(attribute_base, ivar_name, default)
|
|
21
|
+
make_reader_method(attribute_base, ivar_name, default) if reader
|
|
22
|
+
make_raw_writer_method(attribute_base, ivar_name) if writer && raw
|
|
23
|
+
make_checked_writer_method(attribute_base, ivar_name) if writer && !raw
|
|
24
|
+
make_bang_method(attribute_base, ivar_name) if bang
|
|
25
|
+
|
|
26
|
+
:"#{attribute_base}?"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
def check_name!(name)
|
|
32
|
+
root_name = name.to_s.sub(/\?$/, '')
|
|
33
|
+
raise(NameError, "Invalid attribute name #{name.inspect}") if root_name.match?(/[?!=]/)
|
|
34
|
+
|
|
35
|
+
root_name
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def make_query_method(attribute_base, ivar_name, default)
|
|
39
|
+
# rubocop:disable Style/DoubleNegation
|
|
40
|
+
@module.class_eval(<<-QUERY_METHOD, __FILE__, __LINE__ + 1)
|
|
41
|
+
def #{attribute_base}? # def attribute?
|
|
42
|
+
defined?(#{ivar_name}) ? !!#{ivar_name} : #{!!default} # defined?(@attribute) ? !!@attribute : !!default
|
|
43
|
+
end # end
|
|
44
|
+
QUERY_METHOD
|
|
45
|
+
# rubocop:enable Style/DoubleNegation
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def make_reader_method(attribute_base, ivar_name, default)
|
|
49
|
+
@module.class_eval(<<-READER_METHOD, __FILE__, __LINE__ + 1)
|
|
50
|
+
def #{attribute_base} # def attribute?
|
|
51
|
+
defined?(#{ivar_name}) ? #{ivar_name} : #{default.inspect} # defined?(@attribute) ? @attribute : default
|
|
52
|
+
end # end
|
|
53
|
+
READER_METHOD
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def make_raw_writer_method(attribute_base, ivar_name)
|
|
57
|
+
@module.class_eval(<<-WRITER_METHOD, __FILE__, __LINE__ + 1)
|
|
58
|
+
def #{attribute_base}=(value) # def attribute=(value)
|
|
59
|
+
#{ivar_name} = value # @attribute = value
|
|
60
|
+
end # end
|
|
61
|
+
WRITER_METHOD
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def make_checked_writer_method(attribute_base, ivar_name)
|
|
65
|
+
@module.class_eval(<<-WRITER_METHOD, __FILE__, __LINE__ + 1)
|
|
66
|
+
def #{attribute_base}=(value) # def attribute=(value)
|
|
67
|
+
#{ivar_name} = !!value # @attribute = !!value
|
|
68
|
+
end # end
|
|
69
|
+
WRITER_METHOD
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def make_bang_method(attribute_base, ivar_name)
|
|
73
|
+
@module.class_eval(<<-BANG_METHOD, __FILE__, __LINE__ + 1)
|
|
74
|
+
def #{attribute_base}! # def attribute!
|
|
75
|
+
#{ivar_name} = true # @attribute = true
|
|
76
|
+
end # end
|
|
77
|
+
BANG_METHOD
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
end
|
|
81
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Extend module with attr_boolean
|
|
4
|
+
|
|
5
|
+
class Module # rubocop:disable Style/Documentation
|
|
6
|
+
|
|
7
|
+
# battr_accessor(names, ..., options)
|
|
8
|
+
# attr_boolean(names, ..., options)
|
|
9
|
+
#
|
|
10
|
+
# Defines a boolean style attribute for the module or class
|
|
11
|
+
# with a query method (attribute?) and optional writers and defaults
|
|
12
|
+
# If the name does not end with a question mark (?) then it is added
|
|
13
|
+
#
|
|
14
|
+
# ==== Options
|
|
15
|
+
#
|
|
16
|
+
# * <tt>:default</tt> - Sets the default value for the attributes
|
|
17
|
+
# (defaults to false).
|
|
18
|
+
# * <tt>:raw</tt> - Raw mode stores actual values instead of coercing values to booleans.
|
|
19
|
+
# (defaults to false).
|
|
20
|
+
# * <tt>:reader</tt> - Creates a reader method for the attribute (attribute)
|
|
21
|
+
# (By default this is enabled when raw mode is enabled).
|
|
22
|
+
# * <tt>:bang</tt> - Creates a bang method to set the attribute to true (attribute!)
|
|
23
|
+
# (defaults to true).
|
|
24
|
+
# * <tt>:writer</tt> - Creates a write method for the attribute (attribute = vale)
|
|
25
|
+
# (defaults to the same value as bang).
|
|
26
|
+
#
|
|
27
|
+
# ==== Examples
|
|
28
|
+
#
|
|
29
|
+
# class Base
|
|
30
|
+
# attr_boolean :test? # read-write value
|
|
31
|
+
# attr_boolean :active?, writer: false # read only boolean
|
|
32
|
+
# end
|
|
33
|
+
#
|
|
34
|
+
# Base.new.test?
|
|
35
|
+
# Base.new.test=(false)
|
|
36
|
+
# Base.new.test!
|
|
37
|
+
# Base.new.active?
|
|
38
|
+
# Base.new.active=(false) # NoMethodFound
|
|
39
|
+
# Base.new.active! # NoMethodFound
|
|
40
|
+
def battr_accessor(*attributes, # rubocop:disable Metrics/ParameterLists
|
|
41
|
+
default: false, # default attribute value
|
|
42
|
+
raw: false, # treat this as a raw attribute allowing non-boolean values
|
|
43
|
+
reader: raw, # create a reader method (attribute)
|
|
44
|
+
bang: true, # create a bang method to set the attribute (attribute!)
|
|
45
|
+
writer: bang) # create a writer method (attribute=)
|
|
46
|
+
impl = BoolAttrAccessor::Implementation.new(self, raw, reader, writer, bang)
|
|
47
|
+
attributes.map do |attribute|
|
|
48
|
+
impl.create(attribute, default)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
alias attr_boolean battr_accessor
|
|
53
|
+
|
|
54
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: bool_attr_accessor
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Marc Rohloff
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2021-12-23 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: benchmark-ips
|
|
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'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: benchmark-memory
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: bundler
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ">="
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rake
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - ">="
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - ">="
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: rspec
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - ">="
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '0'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - ">="
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '0'
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: rubocop
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - ">="
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '0'
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - ">="
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: '0'
|
|
97
|
+
- !ruby/object:Gem::Dependency
|
|
98
|
+
name: rubocop-performance
|
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
|
100
|
+
requirements:
|
|
101
|
+
- - ">="
|
|
102
|
+
- !ruby/object:Gem::Version
|
|
103
|
+
version: '0'
|
|
104
|
+
type: :development
|
|
105
|
+
prerelease: false
|
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
107
|
+
requirements:
|
|
108
|
+
- - ">="
|
|
109
|
+
- !ruby/object:Gem::Version
|
|
110
|
+
version: '0'
|
|
111
|
+
- !ruby/object:Gem::Dependency
|
|
112
|
+
name: rubocop-rake
|
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
|
114
|
+
requirements:
|
|
115
|
+
- - ">="
|
|
116
|
+
- !ruby/object:Gem::Version
|
|
117
|
+
version: '0'
|
|
118
|
+
type: :development
|
|
119
|
+
prerelease: false
|
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
121
|
+
requirements:
|
|
122
|
+
- - ">="
|
|
123
|
+
- !ruby/object:Gem::Version
|
|
124
|
+
version: '0'
|
|
125
|
+
- !ruby/object:Gem::Dependency
|
|
126
|
+
name: rubocop-rspec
|
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
|
128
|
+
requirements:
|
|
129
|
+
- - ">="
|
|
130
|
+
- !ruby/object:Gem::Version
|
|
131
|
+
version: '0'
|
|
132
|
+
type: :development
|
|
133
|
+
prerelease: false
|
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
135
|
+
requirements:
|
|
136
|
+
- - ">="
|
|
137
|
+
- !ruby/object:Gem::Version
|
|
138
|
+
version: '0'
|
|
139
|
+
- !ruby/object:Gem::Dependency
|
|
140
|
+
name: simplecov
|
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
|
142
|
+
requirements:
|
|
143
|
+
- - ">="
|
|
144
|
+
- !ruby/object:Gem::Version
|
|
145
|
+
version: '0'
|
|
146
|
+
type: :development
|
|
147
|
+
prerelease: false
|
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
149
|
+
requirements:
|
|
150
|
+
- - ">="
|
|
151
|
+
- !ruby/object:Gem::Version
|
|
152
|
+
version: '0'
|
|
153
|
+
- !ruby/object:Gem::Dependency
|
|
154
|
+
name: simplecov-lcov
|
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
|
156
|
+
requirements:
|
|
157
|
+
- - ">="
|
|
158
|
+
- !ruby/object:Gem::Version
|
|
159
|
+
version: '0'
|
|
160
|
+
type: :development
|
|
161
|
+
prerelease: false
|
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
163
|
+
requirements:
|
|
164
|
+
- - ">="
|
|
165
|
+
- !ruby/object:Gem::Version
|
|
166
|
+
version: '0'
|
|
167
|
+
description: bool_attr_accessor is a gem for creating boolean style attributes.
|
|
168
|
+
email:
|
|
169
|
+
executables: []
|
|
170
|
+
extensions: []
|
|
171
|
+
extra_rdoc_files: []
|
|
172
|
+
files:
|
|
173
|
+
- ".editorconfig"
|
|
174
|
+
- ".github/workflows/ci.yml"
|
|
175
|
+
- ".gitignore"
|
|
176
|
+
- ".rspec"
|
|
177
|
+
- ".rubocop.yml"
|
|
178
|
+
- CHANGELOG.md
|
|
179
|
+
- Gemfile
|
|
180
|
+
- Gemfile.lock
|
|
181
|
+
- LICENSE.txt
|
|
182
|
+
- README.md
|
|
183
|
+
- Rakefile
|
|
184
|
+
- benchmark.rb
|
|
185
|
+
- bool_attr_accessor.gemspec
|
|
186
|
+
- lib/bool_attr_accessor.rb
|
|
187
|
+
- lib/bool_attr_accessor/implementation.rb
|
|
188
|
+
- lib/bool_attr_accessor/version.rb
|
|
189
|
+
- lib/module_extensions.rb
|
|
190
|
+
homepage: https://github.com/marcrohloff/bool_attr_accessor
|
|
191
|
+
licenses:
|
|
192
|
+
- MIT
|
|
193
|
+
metadata: {}
|
|
194
|
+
post_install_message:
|
|
195
|
+
rdoc_options: []
|
|
196
|
+
require_paths:
|
|
197
|
+
- lib
|
|
198
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
199
|
+
requirements:
|
|
200
|
+
- - ">="
|
|
201
|
+
- !ruby/object:Gem::Version
|
|
202
|
+
version: 2.5.0
|
|
203
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
204
|
+
requirements:
|
|
205
|
+
- - ">="
|
|
206
|
+
- !ruby/object:Gem::Version
|
|
207
|
+
version: '0'
|
|
208
|
+
requirements: []
|
|
209
|
+
rubygems_version: 3.1.2
|
|
210
|
+
signing_key:
|
|
211
|
+
specification_version: 4
|
|
212
|
+
summary: A gem for creating boolean attributes.
|
|
213
|
+
test_files: []
|