attr_bool 0.3.0 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 427185e002ab3a1eea72403d4b91428dca1d981049fc9b79abd2b445add682b7
4
- data.tar.gz: 71328c73417dcdff6c82600d46bc15347c9aa5ca65ea73f4310d4d6b55518a9b
3
+ metadata.gz: 25fcf6407c75df3659d7737f4f975b095ab691e1a5a470bd24866f34988f1f76
4
+ data.tar.gz: 54c921a73b3e8bf2ec1cbc87377b9573faab77ef119483bec262d084f58bc3af
5
5
  SHA512:
6
- metadata.gz: 4bfaabe00faf88460c364ff76a978eb8adbfc2cd40d481bdb05cab9d8eb565c2634a796042af6bc08d5e3b4b16744140c4d9f8dfce3da14780cf8e0ed43eb857
7
- data.tar.gz: eb3fadb07b363bba497d12b05d112c762d1b44c66c99c6bd5b0decb0374044f2cd92055116c18f2508111c6f0ffa243a526bf6c64695992c36563e26aa3bf462
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 'benchmark','~> 0.4 ' # For benchmarks in Rakefile.
10
- gem 'bundler' ,'~> 2.6 '
11
- gem 'rake' ,'~> 13.3 '
12
- gem 'rdoc' ,'~> 6.14' # Doc.
13
- gem 'simplecov','~> 0.22' # Test coverage.
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','~> 5.25'
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,18 +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
- - Returns an array of the new method names, just like the core `attr` methods, to allow DSL chaining.
43
+ - Can define custom logic with a block/proc/lambda.
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
- - This allows you to refine the top module only of your project, instead of having to extend every class.
46
- - 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).
47
47
 
48
48
  Anti-features:
49
49
  - No default values.
50
50
  - Initialize your instance variables in `def initialize` like normal.
51
- - Using default values has performance issues and other drawbacks, so better to just match the core `attr` methods.
51
+ - Using default values has performance/memory issues and other drawbacks, so better to just match the core `attr` methods.
52
52
  - Uses inner `AttrBool::Ext` & `AttrBool::Ref` instead of `AttrBool`.
53
- - 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.
54
54
  - Doesn't monkey-patch the core class/module by default.
55
55
  - If desired for apps/scripts, you still can with `require 'attr_bool/core_ext'`, but not recommended for libraries.
56
56
 
@@ -106,14 +106,14 @@ gem install attr_bool
106
106
  Or in your *Gemspec*:
107
107
 
108
108
  ```ruby
109
- spec.add_dependency 'attr_bool', '~> X.X'
109
+ spec.add_dependency 'attr_bool', '~> X.X.X'
110
110
  ```
111
111
 
112
112
  Or in your *Gemfile*:
113
113
 
114
114
  ```ruby
115
115
  # Pick your poison...
116
- gem 'attr_bool', '~> X.X'
116
+ gem 'attr_bool', '~> X.X.X'
117
117
  gem 'attr_bool', git: 'https://github.com/esotericpig/attr_bool.git'
118
118
  ```
119
119
 
@@ -128,7 +128,7 @@ bundle exec rake install:local
128
128
 
129
129
  ## [//](#-contents) Usage
130
130
 
131
- You can either add `extend AttrBool::Ext` in your class/module, add `using AttrBool::Ref` in your class/module, or include `require 'attr_bool/core_ext'`.
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'`.
132
132
 
133
133
  ```ruby
134
134
  require 'attr_bool'
@@ -143,35 +143,38 @@ class TheTodd
143
143
  # Can do DSL chaining.
144
144
  protected attr_accessor? :high_five, 'fist_bump'
145
145
 
146
- # Can do custom logic.
147
- attr_accessor? :headband, 'banana_hammock',
148
- reader: -> { @wearing == :flaming },
149
- writer: ->(value) { @wearing = value }
150
-
151
- attr_reader?(:cat_fights) { @cat_fights % 69 }
152
- attr_writer?(:hot_surgeries) { |count| @hot_surgeries += count }
153
-
154
146
  # Can force bool values (i.e., only `true` or `false`).
155
147
  attr_bool :carla_kiss # Accessor.
156
148
  attr_bool? :elliot_kiss # Reader.
157
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 }
158
158
  end
159
159
  ```
160
160
 
161
- If you don't want to have to add `extend AttrBool::Ext` to every class/module, you can simply refine the top module in your gem/library/app:
161
+ If you don't want to have to add `extend AttrBool::Ext` to every inner class/module (within the same file), then you can simply refine the outer module or the file:
162
162
 
163
163
  ```ruby
164
164
  require 'attr_bool'
165
165
 
166
+ #using AttrBool::Ref # Can refine the entire file instead (doesn't affect other files).
167
+
166
168
  module TheToddMod
167
169
  using AttrBool::Ref
168
- end
169
170
 
170
- # --- Some other file.
171
- module TheToddMod
172
171
  class TheTodd
173
172
  attr_bool :banana_hammock
174
173
  end
174
+
175
+ class TheToddBod
176
+ attr_bool :bounce_pecs
177
+ end
175
178
  end
176
179
  ```
177
180
 
@@ -197,7 +200,7 @@ class TheTodd
197
200
  end
198
201
  ```
199
202
 
200
- You can either disable this Cop or adjust it accordingly:
203
+ You can either adjust this Cop accordingly or disable it:
201
204
 
202
205
  ```yaml
203
206
  Layout/EmptyLinesAroundAttributeAccessor:
data/attr_bool.gemspec CHANGED
@@ -14,8 +14,8 @@ Gem::Specification.new do |spec|
14
14
  spec.description = <<~DESC
15
15
  #{spec.summary}
16
16
 
17
- Pick one:
18
- (1) in your top module, add `using AttrBool::Ref`,
17
+ To get started, pick one:
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
 
@@ -9,5 +9,5 @@
9
9
  #++
10
10
 
11
11
  module AttrBool
12
- VERSION = '0.3.0'
12
+ VERSION = '0.3.2'
13
13
  end
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: protected attr_bool :banana_hammock,:bounce_pecs
112
+ # Example:
113
+ # protected attr_bool :banana_hammock,:bounce_pecs
112
114
  method_names = []
113
115
 
114
116
  # noinspection RubySimplifyBooleanInspection
115
- names.map do |name|
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.0
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bradley Whited
@@ -12,8 +12,8 @@ dependencies: []
12
12
  description: |
13
13
  Finally attr_accessor? & attr_reader? with question marks for booleans/predicates!?
14
14
 
15
- Pick one:
16
- (1) in your top module, add `using AttrBool::Ref`,
15
+ To get started, pick one:
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.0
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.6.9
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: []