attr_bool 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a62c877260d0a0c1011606c3ac052b8a3ca9f2024bebd22a616e9ec4e6c8b15f
4
+ data.tar.gz: 22ed6454ecab891813d979c76f9799196c903b6b5b423ecf92c343d64e761528
5
+ SHA512:
6
+ metadata.gz: b0853e2ac22aa5bae184ebfe4358dfe9ab2286930de0e95dda926c0bb89b15c41cac7863dd8f1667e444d557f2b80da5c4e1355de24eab44db19bcd606eb3f16
7
+ data.tar.gz: 52b6a2c764429a3453251c3d6e84563c43f405a128e4d345cd56610943a70c3a9b173eee93620667a16c5931309972feb9fbf326ade849e0d6cc40ebc998dfe7
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ # encoding: UTF-8
2
+ # frozen_string_literal: true
3
+
4
+
5
+ source 'https://rubygems.org'
6
+
7
+ gemspec
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Jonathan Bradley Whited
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,152 @@
1
+ # encoding: UTF-8
2
+ # frozen_string_literal: true
3
+
4
+ #--
5
+ # This file is part of attr_bool.
6
+ # Copyright (c) 2020 Jonathan Bradley Whited (@esotericpig)
7
+ #
8
+ # attr_bool is free software: you can redistribute it and/or modify it under
9
+ # the terms of the MIT License.
10
+ #
11
+ # You should have received a copy of the MIT License along with attr_bool.
12
+ # If not, see <https://choosealicense.com/licenses/mit/>.
13
+ #++
14
+
15
+
16
+ require 'bundler/gem_tasks'
17
+
18
+ require 'benchmark'
19
+ require 'rake/clean'
20
+ require 'rake/testtask'
21
+ require 'yard'
22
+
23
+ require 'attr_bool/version'
24
+
25
+
26
+ CLEAN.exclude('.git/','stock/')
27
+ CLOBBER.include('doc/')
28
+
29
+
30
+ task default: [:test]
31
+
32
+ desc 'Generate doc (YARDoc)'
33
+ task :doc => [:yard] do |task|
34
+ end
35
+
36
+ desc 'Generate doc for tests too (for checking macros)'
37
+ task :doc_test do |task|
38
+ ENV['doctest'] = 'y'
39
+
40
+ doc_task = Rake::Task[:doc]
41
+
42
+ doc_task.reenable()
43
+ doc_task.invoke()
44
+ end
45
+
46
+ Rake::TestTask.new() do |task|
47
+ task.libs = ['lib','test']
48
+ task.pattern = File.join('test','**','*_test.rb')
49
+ task.description += ": '#{task.pattern}'"
50
+ task.verbose = false
51
+ task.warning = true
52
+ end
53
+
54
+ YARD::Rake::YardocTask.new() do |task|
55
+ task.files = [File.join('lib','**','*.{rb}')]
56
+
57
+ task.options += ['--files','CHANGELOG.md,LICENSE.txt']
58
+ task.options += ['--readme','README.md']
59
+
60
+ task.options << '--protected' # Show protected methods
61
+ #task.options += ['--template-path',File.join('yard','templates')]
62
+ task.options += ['--title',"attr_bool v#{AttrBool::VERSION} doc"]
63
+
64
+ task.before = Proc.new() do
65
+ task.files << File.join('test','**','*.{rb}') if ENV['doctest'].to_s().casecmp?('y')
66
+ end
67
+ end
68
+
69
+
70
+ desc 'Benchmark define_method vs module_eval & ?: vs bangbang'
71
+ task :benchmark do |task|
72
+ N0 = 100_000
73
+ N1 = 20_000_000
74
+
75
+ module ModuleExt
76
+ def do_class_eval(name)
77
+ 0.upto(N0) do |i|
78
+ n = "#{name}#{i}"
79
+
80
+ class_eval("def #{n}?(); @#{n}; end")
81
+ end
82
+ end
83
+
84
+ def do_define_method(name)
85
+ 0.upto(N0) do |i|
86
+ n = "#{name}#{i}"
87
+
88
+ define_method(:"#{n}?") do
89
+ instance_variable_get(:"@#{n}")
90
+ end
91
+ end
92
+ end
93
+
94
+ def do_module_eval(name)
95
+ 0.upto(N0) do |i|
96
+ n = "#{name}#{i}"
97
+
98
+ module_eval("def #{n}?(); @#{n}; end")
99
+ end
100
+ end
101
+ end
102
+
103
+ Module.prepend ModuleExt
104
+
105
+ puts
106
+ Benchmark.bmbm() do |bm|
107
+ bm.report('class_eval ') do
108
+ class ClassEvalTest
109
+ do_class_eval :ce
110
+ end
111
+ end
112
+
113
+ bm.report('define_method') do
114
+ class DefineMethodTest
115
+ do_define_method :dm
116
+ end
117
+ end
118
+
119
+ bm.report('module_eval ') do
120
+ class ModuleEvalTest
121
+ do_module_eval :me
122
+ end
123
+ end
124
+ end
125
+
126
+ str = 'str' # Warning workaround
127
+
128
+ puts
129
+ Benchmark.bmbm() do |bm|
130
+ bm.report('?:') do
131
+ 0.upto(N1) do |i|
132
+ x = str ? true : false
133
+ x = nil ? true : false
134
+ x = true ? true : false
135
+ x = false ? true : false
136
+ x = x ? true : false
137
+ end
138
+ end
139
+
140
+ bm.report('!!') do
141
+ 0.upto(N1) do |i|
142
+ y = !!str
143
+ y = !!nil
144
+ y = !!true
145
+ y = !!false
146
+ y = !!y
147
+ end
148
+ end
149
+ end
150
+
151
+ puts
152
+ end
@@ -0,0 +1,53 @@
1
+ # encoding: UTF-8
2
+ # frozen_string_literal: true
3
+
4
+ #--
5
+ # This file is part of attr_bool.
6
+ # Copyright (c) 2020 Jonathan Bradley Whited (@esotericpig)
7
+ #
8
+ # attr_bool is free software: you can redistribute it and/or modify it under
9
+ # the terms of the MIT License.
10
+ #
11
+ # You should have received a copy of the MIT License along with attr_bool.
12
+ # If not, see <https://choosealicense.com/licenses/mit/>.
13
+ #++
14
+
15
+
16
+ lib = File.expand_path(File.join('..','lib'),__FILE__)
17
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
18
+
19
+ require 'attr_bool/version'
20
+
21
+
22
+ Gem::Specification.new() do |spec|
23
+ spec.name = 'attr_bool'
24
+ spec.version = AttrBool::VERSION
25
+ spec.authors = ['Jonathan Bradley Whited (@esotericpig)']
26
+ spec.email = ['bradley@esotericpig.com']
27
+ spec.licenses = ['MIT']
28
+ spec.homepage = 'https://github.com/esotericpig/attr_bool'
29
+ spec.summary = 'Finally attr_accessor & attr_reader with question marks for booleans!?'
30
+ spec.description = spec.summary +
31
+ ' Simply use one the following: attr_accessor?, attr_reader?, attr_bool, attr_bool?.' \
32
+ ' Default values can also be passed in.'
33
+
34
+ spec.metadata = {
35
+ 'bug_tracker_uri' => 'https://github.com/esotericpig/attr_bool/issues',
36
+ 'changelog_uri' => 'https://github.com/esotericpig/attr_bool/blob/master/CHANGELOG.md',
37
+ 'homepage_uri' => 'https://github.com/esotericpig/attr_bool',
38
+ 'source_code_uri' => 'https://github.com/esotericpig/attr_bool',
39
+ }
40
+
41
+ spec.require_paths = ['lib']
42
+
43
+ spec.files = Dir.glob(File.join("{#{spec.require_paths.join(',')}}",'**','*.{rb}')) +
44
+ %W( Gemfile #{spec.name}.gemspec Rakefile ) +
45
+ %w( LICENSE.txt )
46
+
47
+ spec.required_ruby_version = '>= 2.4'
48
+
49
+ spec.add_development_dependency 'bundler' ,'~> 2.1'
50
+ spec.add_development_dependency 'minitest','~> 5.14'
51
+ spec.add_development_dependency 'rake' ,'~> 13.0'
52
+ spec.add_development_dependency 'yard' ,'~> 0.9' # For doc
53
+ end
@@ -0,0 +1,227 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+ # frozen_string_literal: true
4
+
5
+ #--
6
+ # This file is part of attr_bool.
7
+ # Copyright (c) 2020 Jonathan Bradley Whited (@esotericpig)
8
+ #
9
+ # attr_bool is free software: you can redistribute it and/or modify it under
10
+ # the terms of the MIT License.
11
+ #
12
+ # You should have received a copy of the MIT License along with attr_bool.
13
+ # If not, see <https://choosealicense.com/licenses/mit/>.
14
+ #++
15
+
16
+
17
+ require 'attr_bool/version'
18
+
19
+
20
+ ###
21
+ # Benchmarks are kind of meaningless, but after playing around with some,
22
+ # I found the following to be the case on my system:
23
+ # - +define_method+ is faster than +module_eval+ & +class_eval+
24
+ # - +? true : false+ (ternary operator) is faster than +!!+ (surprisingly)
25
+ #
26
+ # To run benchmark code:
27
+ # $ bundle exec rake benchmark
28
+ #
29
+ # @author Jonathan Bradley Whited (@esotericpig)
30
+ # @since 0.1.0
31
+ ###
32
+ module AttrBool
33
+ ###
34
+ # This works for both +class+ & +module+ because +class+ extends +module+.
35
+ #
36
+ # @author Jonathan Bradley Whited (@esotericpig)
37
+ # @since 0.1.0
38
+ ###
39
+ module ModuleExt
40
+ def attr_accessor?(*var_ids,default: nil,reader: nil,writer: nil,&block)
41
+ if block
42
+ reader = block if reader.nil?()
43
+ writer = block if writer.nil?()
44
+ end
45
+
46
+ if default.nil?() && reader.nil?()
47
+ last = var_ids[-1]
48
+
49
+ if !last.is_a?(String) && !last.is_a?(Symbol)
50
+ default = var_ids.pop()
51
+ end
52
+ end
53
+
54
+ attr_reader?(*var_ids,default: default,&reader)
55
+ attr_writer?(*var_ids,&writer)
56
+ end
57
+
58
+ def attr_reader?(*var_ids,default: nil,&block)
59
+ no_default = (default.nil?() && !block)
60
+
61
+ if no_default
62
+ last = var_ids[-1]
63
+
64
+ if !last.is_a?(String) && !last.is_a?(Symbol)
65
+ default = var_ids.pop()
66
+ no_default = false
67
+ end
68
+ end
69
+
70
+ var_ids.each() do |var_id|
71
+ var_id_q = :"#{var_id}?"
72
+
73
+ if no_default
74
+ define_method(var_id_q) do
75
+ instance_variable_get(:"@#{var_id}")
76
+ end
77
+ else
78
+ if block
79
+ define_method(var_id_q,&block)
80
+ else
81
+ at_var_id = :"@#{var_id}"
82
+
83
+ define_method(var_id_q) do
84
+ instance_variable_defined?(at_var_id) ? instance_variable_get(at_var_id) : default
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+
91
+ # This should only be used when you want to pass in a block/proc.
92
+ def attr_writer?(*var_ids,&block)
93
+ if block
94
+ var_ids.each() do |var_id|
95
+ define_method(:"#{var_id}=",&block)
96
+ end
97
+ else
98
+ last = var_ids[-1]
99
+
100
+ if !last.is_a?(String) && !last.is_a?(Symbol)
101
+ raise ArgumentError,'default value not allowed for writer'
102
+ end
103
+
104
+ attr_writer(*var_ids)
105
+ end
106
+ end
107
+
108
+ def attr_bool(*var_ids,default: nil,reader: nil,writer: nil,&block)
109
+ if block
110
+ reader = block if reader.nil?()
111
+ writer = block if writer.nil?()
112
+ end
113
+
114
+ if default.nil?() && reader.nil?()
115
+ last = var_ids[-1]
116
+
117
+ if !last.is_a?(String) && !last.is_a?(Symbol)
118
+ default = var_ids.pop()
119
+ end
120
+ end
121
+
122
+ attr_bool?(*var_ids,default: default,&reader)
123
+ attr_booler(*var_ids,&writer)
124
+ end
125
+ alias_method :attr_boolor,:attr_bool
126
+
127
+ def attr_bool?(*var_ids,default: nil,&block)
128
+ no_default = default.nil?()
129
+
130
+ if no_default
131
+ no_default = !block
132
+
133
+ if no_default
134
+ last = var_ids[-1]
135
+
136
+ if !last.is_a?(String) && !last.is_a?(Symbol)
137
+ default = var_ids.pop() ? true : false
138
+ no_default = false
139
+ end
140
+ end
141
+ else
142
+ default = default ? true : false
143
+ end
144
+
145
+ var_ids.each() do |var_id|
146
+ var_id_q = :"#{var_id}?"
147
+
148
+ if no_default
149
+ define_method(var_id_q) do
150
+ instance_variable_get(:"@#{var_id}") ? true : false
151
+ end
152
+ else
153
+ if block
154
+ define_method(var_id_q,&block)
155
+ else
156
+ at_var_id = :"@#{var_id}"
157
+
158
+ define_method(var_id_q) do
159
+ if instance_variable_defined?(at_var_id)
160
+ instance_variable_get(at_var_id) ? true : false
161
+ else
162
+ default
163
+ end
164
+ end
165
+ end
166
+ end
167
+ end
168
+ end
169
+
170
+ def attr_booler(*var_ids,&block)
171
+ if !block
172
+ last = var_ids[-1]
173
+
174
+ if !last.is_a?(String) && !last.is_a?(Symbol)
175
+ raise ArgumentError,'default value not allowed for writer'
176
+ end
177
+ end
178
+
179
+ var_ids.each() do |var_id|
180
+ var_id_eq = :"#{var_id}="
181
+
182
+ if block
183
+ define_method(var_id_eq,&block)
184
+ else
185
+ define_method(var_id_eq) do |value|
186
+ instance_variable_set(:"@#{var_id}",value ? true : false)
187
+ end
188
+ end
189
+ end
190
+ end
191
+
192
+ # @!macro attach attr_reader?
193
+ # @!method $1?
194
+ # @return [Boolean] whether +$1+ or not
195
+
196
+ # @!macro attach attr_writer?
197
+ # @!method $1=(value)
198
+ # Sets +$1+ to +true+ or +false+.
199
+ # @param value [Boolean] the new value of +$1+
200
+
201
+ # @!macro attach attr_accessor?
202
+ # @!method $1?
203
+ # @!macro attach attr_reader?
204
+ # @!method $1=(value)
205
+ # @!macro attach attr_writer?
206
+
207
+ # @!macro attach attr_bool?
208
+ # @!method $1?
209
+ # @return [true,false] whether +$1+ or not
210
+
211
+ # @!macro attach attr_booler
212
+ # @!method $1=(value)
213
+ # Sets +$1+ to +true+ or +false+.
214
+ # @param value [true,false] the new value of +$1+
215
+
216
+ # @!macro attach attr_bool
217
+ # @!method $1?
218
+ # @!macro attach attr_bool?
219
+ # @!method $1=(value)
220
+ # @!macro attach attr_booler
221
+
222
+ # @!macro attach attr_boolor
223
+ # @!macro attach attr_bool
224
+ end
225
+ end
226
+
227
+ Module.prepend AttrBool::ModuleExt
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+ # frozen_string_literal: true
4
+
5
+ #--
6
+ # This file is part of attr_bool.
7
+ # Copyright (c) 2020 Jonathan Bradley Whited (@esotericpig)
8
+ #
9
+ # attr_bool is free software: you can redistribute it and/or modify it under
10
+ # the terms of the MIT License.
11
+ #
12
+ # You should have received a copy of the MIT License along with attr_bool.
13
+ # If not, see <https://choosealicense.com/licenses/mit/>.
14
+ #++
15
+
16
+
17
+ module AttrBool
18
+ VERSION = '0.1.0'
19
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attr_bool
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jonathan Bradley Whited (@esotericpig)
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-04-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.14'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '13.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '13.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: yard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.9'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.9'
69
+ description: 'Finally attr_accessor & attr_reader with question marks for booleans!?
70
+ Simply use one the following: attr_accessor?, attr_reader?, attr_bool, attr_bool?.
71
+ Default values can also be passed in.'
72
+ email:
73
+ - bradley@esotericpig.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - Rakefile
81
+ - attr_bool.gemspec
82
+ - lib/attr_bool.rb
83
+ - lib/attr_bool/version.rb
84
+ homepage: https://github.com/esotericpig/attr_bool
85
+ licenses:
86
+ - MIT
87
+ metadata:
88
+ bug_tracker_uri: https://github.com/esotericpig/attr_bool/issues
89
+ changelog_uri: https://github.com/esotericpig/attr_bool/blob/master/CHANGELOG.md
90
+ homepage_uri: https://github.com/esotericpig/attr_bool
91
+ source_code_uri: https://github.com/esotericpig/attr_bool
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '2.4'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubygems_version: 3.1.2
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Finally attr_accessor & attr_reader with question marks for booleans!?
111
+ test_files: []