attr_bool 0.3.1 → 0.3.2
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 +7 -6
- data/README.md +17 -16
- data/attr_bool.gemspec +1 -1
- data/lib/attr_bool/version.rb +1 -1
- data/lib/attr_bool.rb +31 -25
- data/test/ref_test.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 25fcf6407c75df3659d7737f4f975b095ab691e1a5a470bd24866f34988f1f76
|
4
|
+
data.tar.gz: 54c921a73b3e8bf2ec1cbc87377b9573faab77ef119483bec262d084f58bc3af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f5679bb292206738700ca4e61e520398b3830beac8aba0584d4d50eacf800bad446f6e96ca89f167366df85b0e1d7a05daf4a3b36cff1af36fa95eeacdc47e55
|
7
|
+
data.tar.gz: 9e63e46286640a81f08eb25191862deb6965738920fa6ba19ba9b240000f997007f3503e4ca1ac4a0bd650b193e17246ec3e517b940f6ecd1ca9494c8de08842
|
data/Gemfile
CHANGED
@@ -6,13 +6,14 @@ source 'https://rubygems.org'
|
|
6
6
|
gemspec
|
7
7
|
|
8
8
|
group(:development,:test) do
|
9
|
-
gem '
|
10
|
-
gem '
|
11
|
-
|
12
|
-
gem '
|
13
|
-
gem '
|
9
|
+
gem 'bundler','~> 2.6'
|
10
|
+
gem 'rake','~> 13.3.0'
|
11
|
+
|
12
|
+
gem 'benchmark','~> 0.4.0' # For benchmarks in Rakefile.
|
13
|
+
gem 'rdoc','~> 6.14.0' # Doc.
|
14
14
|
end
|
15
15
|
|
16
16
|
group(:test) do
|
17
|
-
gem 'minitest','~>
|
17
|
+
gem 'minitest','~> 5.25.0' # Tests.
|
18
|
+
gem 'simplecov','~> 0.22.0' # Test coverage.
|
18
19
|
end
|
data/README.md
CHANGED
@@ -11,9 +11,10 @@ Easily create `attr` (attribute) methods that end with question marks (`?`) for
|
|
11
11
|
```ruby
|
12
12
|
require 'attr_bool'
|
13
13
|
|
14
|
+
#using AttrBool::Ref # Can use refinements instead of `extend AttrBool::Ext`.
|
15
|
+
|
14
16
|
class TheTodd
|
15
17
|
extend AttrBool::Ext
|
16
|
-
#using AttrBool::Ref # Can use refinements instead.
|
17
18
|
|
18
19
|
attr_accessor? :headband
|
19
20
|
attr_reader? :banana_hammock
|
@@ -39,17 +40,17 @@ puts todd.cat_fight?
|
|
39
40
|
Features:
|
40
41
|
- Can use multiple symbols and/or strings.
|
41
42
|
- Can force bool values.
|
42
|
-
- Can define custom logic with a block/proc.
|
43
|
+
- Can define custom logic with a block/proc/lambda.
|
43
44
|
- Can do DSL chaining, just like the core `attr` methods that return an array of the new method names.
|
44
45
|
- Can use refinements (`using AttrBool::Ref`) instead of `extend`.
|
45
|
-
- Fails fast if an instance variable name is invalid (if you don't use a block/proc).
|
46
|
+
- Fails fast if an instance variable name is invalid (if you don't use a block/proc/lambda).
|
46
47
|
|
47
48
|
Anti-features:
|
48
49
|
- No default values.
|
49
50
|
- Initialize your instance variables in `def initialize` like normal.
|
50
51
|
- Using default values has performance/memory issues and other drawbacks, so better to just match the core `attr` methods.
|
51
52
|
- Uses inner `AttrBool::Ext` & `AttrBool::Ref` instead of `AttrBool`.
|
52
|
-
- Some gems use the `extend AttrBool` (top module) pattern, but this includes `VERSION` in all of your classes/modules.
|
53
|
+
- Some gems use the `extend AttrBool` (top module) pattern, but this includes the `VERSION` constant in all of your classes/modules.
|
53
54
|
- Doesn't monkey-patch the core class/module by default.
|
54
55
|
- If desired for apps/scripts, you still can with `require 'attr_bool/core_ext'`, but not recommended for libraries.
|
55
56
|
|
@@ -105,14 +106,14 @@ gem install attr_bool
|
|
105
106
|
Or in your *Gemspec*:
|
106
107
|
|
107
108
|
```ruby
|
108
|
-
spec.add_dependency 'attr_bool', '~> X.X'
|
109
|
+
spec.add_dependency 'attr_bool', '~> X.X.X'
|
109
110
|
```
|
110
111
|
|
111
112
|
Or in your *Gemfile*:
|
112
113
|
|
113
114
|
```ruby
|
114
115
|
# Pick your poison...
|
115
|
-
gem 'attr_bool', '~> X.X'
|
116
|
+
gem 'attr_bool', '~> X.X.X'
|
116
117
|
gem 'attr_bool', git: 'https://github.com/esotericpig/attr_bool.git'
|
117
118
|
```
|
118
119
|
|
@@ -127,7 +128,7 @@ bundle exec rake install:local
|
|
127
128
|
|
128
129
|
## [//](#-contents) Usage
|
129
130
|
|
130
|
-
You can either add `
|
131
|
+
You can either add `using AttrBool::Ref` in your class/module/file, add `extend AttrBool::Ext` in your class/module, or include `require 'attr_bool/core_ext'`.
|
131
132
|
|
132
133
|
```ruby
|
133
134
|
require 'attr_bool'
|
@@ -142,18 +143,18 @@ class TheTodd
|
|
142
143
|
# Can do DSL chaining.
|
143
144
|
protected attr_accessor? :high_five, 'fist_bump'
|
144
145
|
|
145
|
-
# Can do custom logic.
|
146
|
-
attr_accessor? :headband, 'banana_hammock',
|
147
|
-
reader: -> { @wearing == :flaming },
|
148
|
-
writer: ->(value) { @wearing = value }
|
149
|
-
|
150
|
-
attr_reader?(:cat_fights) { @cat_fights % 69 }
|
151
|
-
attr_writer?(:hot_surgeries) { |count| @hot_surgeries += count }
|
152
|
-
|
153
146
|
# Can force bool values (i.e., only `true` or `false`).
|
154
147
|
attr_bool :carla_kiss # Accessor.
|
155
148
|
attr_bool? :elliot_kiss # Reader.
|
156
149
|
attr_bool! :thumbs_up # Writer.
|
150
|
+
|
151
|
+
# Can do custom logic with a block/proc/lambda.
|
152
|
+
attr_reader?(:cat_fights) { @cat_fights % 69 }
|
153
|
+
attr_writer?(:hot_surgeries) { |count| @hot_surgeries += count }
|
154
|
+
|
155
|
+
attr_accessor? :headband, 'banana_hammock',
|
156
|
+
reader: -> { @wearing == :flaming },
|
157
|
+
writer: ->(value) { @wearing = value }
|
157
158
|
end
|
158
159
|
```
|
159
160
|
|
@@ -199,7 +200,7 @@ class TheTodd
|
|
199
200
|
end
|
200
201
|
```
|
201
202
|
|
202
|
-
You can either
|
203
|
+
You can either adjust this Cop accordingly or disable it:
|
203
204
|
|
204
205
|
```yaml
|
205
206
|
Layout/EmptyLinesAroundAttributeAccessor:
|
data/attr_bool.gemspec
CHANGED
@@ -15,7 +15,7 @@ Gem::Specification.new do |spec|
|
|
15
15
|
#{spec.summary}
|
16
16
|
|
17
17
|
To get started, pick one:
|
18
|
-
(1) in your class/module, add `using AttrBool::Ref`,
|
18
|
+
(1) in your class/module/file, add `using AttrBool::Ref`,
|
19
19
|
or (2) in your class/module, add `extend AttrBool::Ext`,
|
20
20
|
or (3) in your app/script (not library), include `require 'attr_bool/core_ext'`.
|
21
21
|
|
data/lib/attr_bool/version.rb
CHANGED
data/lib/attr_bool.rb
CHANGED
@@ -15,9 +15,10 @@ require 'attr_bool/version'
|
|
15
15
|
# ```
|
16
16
|
# require 'attr_bool'
|
17
17
|
#
|
18
|
+
# #using AttrBool::Ref # Can use refinements instead of `extend AttrBool::Ext`.
|
19
|
+
#
|
18
20
|
# class TheTodd
|
19
21
|
# extend AttrBool::Ext
|
20
|
-
# #using AttrBool::Ref # Can use refinements instead.
|
21
22
|
#
|
22
23
|
# # Can use multiple symbols and/or strings.
|
23
24
|
# attr_accessor? :flexing, 'bounce_pecs'
|
@@ -25,18 +26,18 @@ require 'attr_bool/version'
|
|
25
26
|
# # Can do DSL chaining.
|
26
27
|
# protected attr_accessor? :high_five, 'fist_bump'
|
27
28
|
#
|
28
|
-
# # Can do custom logic.
|
29
|
-
# attr_accessor? :headband, 'banana_hammock',
|
30
|
-
# reader: -> { @wearing == :flaming },
|
31
|
-
# writer: ->(value) { @wearing = value }
|
32
|
-
#
|
33
|
-
# attr_reader?(:cat_fights) { @cat_fights % 69 }
|
34
|
-
# attr_writer?(:hot_surgeries) { |count| @hot_surgeries += count }
|
35
|
-
#
|
36
29
|
# # Can force bool values (i.e., only `true` or `false`).
|
37
30
|
# attr_bool :carla_kiss # Accessor.
|
38
31
|
# attr_bool? :elliot_kiss # Reader.
|
39
32
|
# attr_bool! :thumbs_up # Writer.
|
33
|
+
#
|
34
|
+
# # Can do custom logic with a block/proc/lambda.
|
35
|
+
# attr_reader?(:cat_fights) { @cat_fights % 69 }
|
36
|
+
# attr_writer?(:hot_surgeries) { |count| @hot_surgeries += count }
|
37
|
+
#
|
38
|
+
# attr_accessor? :headband, 'banana_hammock',
|
39
|
+
# reader: -> { @wearing == :flaming },
|
40
|
+
# writer: ->(value) { @wearing = value }
|
40
41
|
# end
|
41
42
|
# ```
|
42
43
|
module AttrBool
|
@@ -108,24 +109,21 @@ module AttrBool
|
|
108
109
|
|
109
110
|
def __attr_bool(names,reader: false,writer: false,force_bool: false)
|
110
111
|
# For DSL chaining, must return the method names created, like core `attr_accessor`/etc. does.
|
111
|
-
# Example:
|
112
|
+
# Example:
|
113
|
+
# protected attr_bool :banana_hammock,:bounce_pecs
|
112
114
|
method_names = []
|
113
115
|
|
114
116
|
# noinspection RubySimplifyBooleanInspection
|
115
|
-
names.
|
117
|
+
names.each do |name|
|
118
|
+
# Most of the time, users will use the `ivar` way, so just create it once here,
|
119
|
+
# instead of potentially twice inside the if-blocks.
|
116
120
|
ivar = :"@#{name}"
|
117
121
|
|
118
122
|
if reader != false # false, nil, or Proc.
|
119
123
|
name_q = :"#{name}?"
|
120
124
|
method_names << name_q
|
121
125
|
|
122
|
-
if reader # Proc?
|
123
|
-
if force_bool
|
124
|
-
define_method(name_q) { instance_exec(&reader) ? true : false }
|
125
|
-
else
|
126
|
-
define_method(name_q,&reader)
|
127
|
-
end
|
128
|
-
else # nil?
|
126
|
+
if reader.nil? # Not a Proc?
|
129
127
|
instance_variable_get(ivar) # Fail fast if `ivar` is invalid.
|
130
128
|
|
131
129
|
if force_bool
|
@@ -133,6 +131,12 @@ module AttrBool
|
|
133
131
|
else
|
134
132
|
define_method(name_q) { instance_variable_get(ivar) }
|
135
133
|
end
|
134
|
+
else # Proc?
|
135
|
+
if force_bool
|
136
|
+
define_method(name_q) { instance_exec(&reader) ? true : false }
|
137
|
+
else
|
138
|
+
define_method(name_q,&reader)
|
139
|
+
end
|
136
140
|
end
|
137
141
|
end
|
138
142
|
|
@@ -140,13 +144,7 @@ module AttrBool
|
|
140
144
|
name_eq = :"#{name}="
|
141
145
|
method_names << name_eq
|
142
146
|
|
143
|
-
if writer # Proc?
|
144
|
-
if force_bool
|
145
|
-
define_method(name_eq) { |value| instance_exec(value ? true : false,&writer) }
|
146
|
-
else
|
147
|
-
define_method(name_eq,&writer)
|
148
|
-
end
|
149
|
-
else # nil?
|
147
|
+
if writer.nil? # Not a Proc?
|
150
148
|
instance_variable_get(ivar) # Fail fast if `ivar` is invalid.
|
151
149
|
|
152
150
|
if force_bool
|
@@ -154,6 +152,12 @@ module AttrBool
|
|
154
152
|
else
|
155
153
|
define_method(name_eq) { |value| instance_variable_set(ivar,value) }
|
156
154
|
end
|
155
|
+
else # Proc?
|
156
|
+
if force_bool
|
157
|
+
define_method(name_eq) { |value| instance_exec(value ? true : false,&writer) }
|
158
|
+
else
|
159
|
+
define_method(name_eq,&writer)
|
160
|
+
end
|
157
161
|
end
|
158
162
|
end
|
159
163
|
end
|
@@ -165,6 +169,8 @@ module AttrBool
|
|
165
169
|
##
|
166
170
|
# Example usage:
|
167
171
|
# ```
|
172
|
+
# #using AttrBool::Ref # Can refine the entire file instead (doesn't affect other files).
|
173
|
+
#
|
168
174
|
# module TheToddMod
|
169
175
|
# using AttrBool::Ref
|
170
176
|
#
|
data/test/ref_test.rb
CHANGED
@@ -29,7 +29,7 @@ describe AttrBool::Ref do
|
|
29
29
|
end
|
30
30
|
|
31
31
|
it 'refines the core Class & Module inside of its scope only once' do
|
32
|
-
# JRuby doesn't implement used_modules() currently.
|
32
|
+
# NOTE: JRuby doesn't implement used_modules() currently.
|
33
33
|
if RUBY_PLATFORM != 'java'
|
34
34
|
_(@sut.class_used_modules.count(AttrBool::Ref)).must_equal(1)
|
35
35
|
_(@sut.module_used_modules.count(AttrBool::Ref)).must_equal(1)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attr_bool
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bradley Whited
|
@@ -13,7 +13,7 @@ description: |
|
|
13
13
|
Finally attr_accessor? & attr_reader? with question marks for booleans/predicates!?
|
14
14
|
|
15
15
|
To get started, pick one:
|
16
|
-
(1) in your class/module, add `using AttrBool::Ref`,
|
16
|
+
(1) in your class/module/file, add `using AttrBool::Ref`,
|
17
17
|
or (2) in your class/module, add `extend AttrBool::Ext`,
|
18
18
|
or (3) in your app/script (not library), include `require 'attr_bool/core_ext'`.
|
19
19
|
|
@@ -62,7 +62,7 @@ rdoc_options:
|
|
62
62
|
- "--markup"
|
63
63
|
- markdown
|
64
64
|
- "--title"
|
65
|
-
- AttrBool v0.3.
|
65
|
+
- AttrBool v0.3.2
|
66
66
|
- "--main"
|
67
67
|
- README.md
|
68
68
|
require_paths:
|
@@ -78,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
78
78
|
- !ruby/object:Gem::Version
|
79
79
|
version: '0'
|
80
80
|
requirements: []
|
81
|
-
rubygems_version: 3.
|
81
|
+
rubygems_version: 3.7.1
|
82
82
|
specification_version: 4
|
83
83
|
summary: Finally attr_accessor? & attr_reader? with question marks for booleans/predicates!?
|
84
84
|
test_files: []
|