naught 1.1.0 → 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 +5 -5
- data/LICENSE.txt +1 -1
- data/lib/naught/basic_object.rb +4 -14
- data/lib/naught/call_location.rb +131 -0
- data/lib/naught/caller_info.rb +128 -0
- data/lib/naught/chain_proxy.rb +51 -0
- data/lib/naught/conversions.rb +108 -34
- data/lib/naught/null_class_builder/command.rb +42 -4
- data/lib/naught/null_class_builder/commands/callstack.rb +89 -0
- data/lib/naught/null_class_builder/commands/define_explicit_conversions.rb +21 -9
- data/lib/naught/null_class_builder/commands/define_implicit_conversions.rb +19 -21
- data/lib/naught/null_class_builder/commands/impersonate.rb +13 -1
- data/lib/naught/null_class_builder/commands/mimic.rb +75 -31
- data/lib/naught/null_class_builder/commands/null_safe_proxy.rb +92 -0
- data/lib/naught/null_class_builder/commands/pebble.rb +21 -17
- data/lib/naught/null_class_builder/commands/predicates_return.rb +42 -24
- data/lib/naught/null_class_builder/commands/singleton.rb +14 -17
- data/lib/naught/null_class_builder/commands/traceable.rb +16 -11
- data/lib/naught/null_class_builder/commands.rb +10 -8
- data/lib/naught/null_class_builder.rb +213 -119
- data/lib/naught/stub_strategy.rb +30 -0
- data/lib/naught/version.rb +3 -1
- data/lib/naught.rb +31 -7
- metadata +34 -66
- data/.gitignore +0 -23
- data/.rspec +0 -2
- data/.rubocop.yml +0 -65
- data/.travis.yml +0 -24
- data/Changelog.md +0 -18
- data/Gemfile +0 -25
- data/Guardfile +0 -15
- data/README.markdown +0 -474
- data/Rakefile +0 -15
- data/naught.gemspec +0 -22
- data/spec/base_object_spec.rb +0 -46
- data/spec/basic_null_object_spec.rb +0 -34
- data/spec/blackhole_spec.rb +0 -14
- data/spec/explicit_conversions_spec.rb +0 -21
- data/spec/functions/actual_spec.rb +0 -22
- data/spec/functions/just_spec.rb +0 -22
- data/spec/functions/maybe_spec.rb +0 -35
- data/spec/functions/null_spec.rb +0 -33
- data/spec/implicit_conversions_spec.rb +0 -28
- data/spec/mimic_spec.rb +0 -123
- data/spec/naught/null_object_builder/command_spec.rb +0 -10
- data/spec/naught/null_object_builder_spec.rb +0 -31
- data/spec/naught_spec.rb +0 -93
- data/spec/pebble_spec.rb +0 -77
- data/spec/predicate_spec.rb +0 -79
- data/spec/singleton_null_object_spec.rb +0 -33
- data/spec/spec_helper.rb +0 -15
- data/spec/support/convertable_null.rb +0 -4
- data/spec/support/jruby.rb +0 -3
- data/spec/support/rubinius.rb +0 -3
- data/spec/support/ruby_18.rb +0 -3
data/.rubocop.yml
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
Lint/NestedMethodDefinition:
|
|
2
|
-
Enabled: false
|
|
3
|
-
|
|
4
|
-
Metrics/BlockNesting:
|
|
5
|
-
Max: 2
|
|
6
|
-
|
|
7
|
-
Metrics/LineLength:
|
|
8
|
-
AllowURI: true
|
|
9
|
-
Max: 93 # TODO: Lower to 80
|
|
10
|
-
|
|
11
|
-
Metrics/MethodLength:
|
|
12
|
-
CountComments: false
|
|
13
|
-
Max: 21 # TODO: Lower to 15
|
|
14
|
-
|
|
15
|
-
Metrics/ParameterLists:
|
|
16
|
-
Max: 4
|
|
17
|
-
CountKeywordArgs: true
|
|
18
|
-
|
|
19
|
-
Style/AccessModifierIndentation:
|
|
20
|
-
EnforcedStyle: outdent
|
|
21
|
-
|
|
22
|
-
Style/ClassVars:
|
|
23
|
-
Enabled: false
|
|
24
|
-
|
|
25
|
-
Style/CollectionMethods:
|
|
26
|
-
Enabled: true
|
|
27
|
-
PreferredMethods:
|
|
28
|
-
map: 'collect'
|
|
29
|
-
map!: 'collect!'
|
|
30
|
-
reduce: 'inject'
|
|
31
|
-
find: 'detect'
|
|
32
|
-
find_all: 'select'
|
|
33
|
-
|
|
34
|
-
Style/Documentation:
|
|
35
|
-
Enabled: false
|
|
36
|
-
|
|
37
|
-
Style/DotPosition:
|
|
38
|
-
EnforcedStyle: trailing
|
|
39
|
-
|
|
40
|
-
Style/DoubleNegation:
|
|
41
|
-
Enabled: false
|
|
42
|
-
|
|
43
|
-
Style/EachWithObject:
|
|
44
|
-
Enabled: false
|
|
45
|
-
|
|
46
|
-
Style/Encoding:
|
|
47
|
-
Enabled: false
|
|
48
|
-
|
|
49
|
-
Style/HashSyntax:
|
|
50
|
-
EnforcedStyle: hash_rockets
|
|
51
|
-
|
|
52
|
-
Style/Lambda:
|
|
53
|
-
Enabled: false
|
|
54
|
-
|
|
55
|
-
Style/MethodName:
|
|
56
|
-
Enabled: false
|
|
57
|
-
|
|
58
|
-
Style/RaiseArgs:
|
|
59
|
-
EnforcedStyle: compact
|
|
60
|
-
|
|
61
|
-
Style/SpaceInsideHashLiteralBraces:
|
|
62
|
-
EnforcedStyle: no_space
|
|
63
|
-
|
|
64
|
-
Style/TrailingComma:
|
|
65
|
-
EnforcedStyleForMultiline: 'comma'
|
data/.travis.yml
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
before_install: gem update bundler
|
|
2
|
-
bundler_args: --without development --retry=3 --jobs=3
|
|
3
|
-
cache: bundler
|
|
4
|
-
env:
|
|
5
|
-
global:
|
|
6
|
-
- JRUBY_OPTS="$JRUBY_OPTS --debug"
|
|
7
|
-
language: ruby
|
|
8
|
-
rvm:
|
|
9
|
-
- 1.8.7
|
|
10
|
-
- 1.9.3
|
|
11
|
-
- 2.0.0
|
|
12
|
-
- 2.1
|
|
13
|
-
- 2.2
|
|
14
|
-
- jruby-9000
|
|
15
|
-
- jruby-head
|
|
16
|
-
- rbx-2
|
|
17
|
-
- ruby-head
|
|
18
|
-
matrix:
|
|
19
|
-
allow_failures:
|
|
20
|
-
- rvm: jruby-head
|
|
21
|
-
- rvm: rbx-2
|
|
22
|
-
- rvm: ruby-head
|
|
23
|
-
fast_finish: true
|
|
24
|
-
sudo: false
|
data/Changelog.md
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
## 1.1.0
|
|
2
|
-
|
|
3
|
-
- [Make it possible to supply an example object to mimic, with no class.](https://github.com/avdi/naught/commit/df2b62c027812760ce200177ce056929b5aea339)
|
|
4
|
-
- [Define implicit conversion for to_hash](https://github.com/avdi/naught/commit/e20dc472d3bc71ba927d6ddb0fb0032e1646df77)
|
|
5
|
-
- [Define implicit conversion for to_int](https://github.com/avdi/naught/commit/d32d4ea32a9a847bffd6cf18f480bdfaaf7a3641)
|
|
6
|
-
|
|
7
|
-
## 1.0.0
|
|
8
|
-
|
|
9
|
-
- [Replace `::BasicObject` with `Naught::BasicObject`](https://github.com/avdi/naught/commit/8defad0bf9eb65e33054bf0a6e9c625c87c3e6df)
|
|
10
|
-
- [Delegate explicit conversions to nil instead of defining them explicitly](https://github.com/avdi/naught/commit/85c195de80ed56993b88f47e09112c903a92a167)
|
|
11
|
-
- Add support for (and run tests on) Ruby 1.8, 1.9, 2.0, 2.1, JRuby, and Rubinius
|
|
12
|
-
|
|
13
|
-
## 0.0.3
|
|
14
|
-
|
|
15
|
-
Features:
|
|
16
|
-
|
|
17
|
-
- New "pebble" mode (Guilherme Carvalho)
|
|
18
|
-
|
data/Gemfile
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
source 'https://rubygems.org'
|
|
2
|
-
|
|
3
|
-
# Specify your gem's dependencies in naught.gemspec
|
|
4
|
-
gemspec
|
|
5
|
-
|
|
6
|
-
gem 'rake'
|
|
7
|
-
|
|
8
|
-
group :development do
|
|
9
|
-
platforms :ruby_19, :ruby_20, :ruby_21, :ruby_22 do
|
|
10
|
-
gem 'guard'
|
|
11
|
-
gem 'guard-bundler'
|
|
12
|
-
gem 'guard-rspec'
|
|
13
|
-
end
|
|
14
|
-
gem 'pry'
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
group :test do
|
|
18
|
-
gem 'coveralls', :require => false
|
|
19
|
-
gem 'json', :platforms => [:jruby, :rbx, :ruby_18, :ruby_19]
|
|
20
|
-
gem 'libnotify'
|
|
21
|
-
gem 'mime-types', '~> 1.25', :platforms => [:jruby, :ruby_18]
|
|
22
|
-
gem 'rest-client', '~> 1.6.0', :platforms => [:jruby, :ruby_18]
|
|
23
|
-
gem 'rspec', '>= 2.14'
|
|
24
|
-
gem 'rubocop', '~> 0.34.0', :platforms => [:ruby_19, :ruby_20, :ruby_21, :ruby_22]
|
|
25
|
-
end
|
data/Guardfile
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
guard 'bundler' do
|
|
2
|
-
watch('Gemfile')
|
|
3
|
-
watch(/^.+\.gemspec/)
|
|
4
|
-
end
|
|
5
|
-
|
|
6
|
-
guard :rspec, :cli => '-fs --color --order rand' do
|
|
7
|
-
watch(%r{^spec/.+_spec\.rb$})
|
|
8
|
-
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
|
9
|
-
watch('spec/spec_helper.rb') { 'spec' }
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
guard 'ctags-bundler', :emacs => true, :src_path => ['lib', 'spec/support'] do
|
|
13
|
-
watch(%r{^(lib|spec/support)/.*\.rb$})
|
|
14
|
-
watch('Gemfile.lock')
|
|
15
|
-
end
|
data/README.markdown
DELETED
|
@@ -1,474 +0,0 @@
|
|
|
1
|
-
[][gem]
|
|
2
|
-
[][travis]
|
|
3
|
-
[][gemnasium]
|
|
4
|
-
[][codeclimate]
|
|
5
|
-
[][coveralls]
|
|
6
|
-
[][docs]
|
|
7
|
-
|
|
8
|
-
[gem]: https://rubygems.org/gems/naught
|
|
9
|
-
[travis]: https://travis-ci.org/avdi/naught
|
|
10
|
-
[gemnasium]: https://gemnasium.com/avdi/naught
|
|
11
|
-
[codeclimate]: https://codeclimate.com/github/avdi/naught
|
|
12
|
-
[coveralls]: https://coveralls.io/github/avdi/naught?branch=master
|
|
13
|
-
[docs]: http://inch-ci.org/github/avdi/naught
|
|
14
|
-
|
|
15
|
-
A quick intro to Naught
|
|
16
|
-
-------------------------
|
|
17
|
-
|
|
18
|
-
#### What's all this now then?
|
|
19
|
-
|
|
20
|
-
Naught is a toolkit for building [Null
|
|
21
|
-
Objects](http://en.wikipedia.org/wiki/Null_Object_pattern) in Ruby.
|
|
22
|
-
|
|
23
|
-
#### What's that supposed to mean?
|
|
24
|
-
|
|
25
|
-
Null Objects can make your code more
|
|
26
|
-
[confident](http://confidentruby.com).
|
|
27
|
-
|
|
28
|
-
Here's a method that's not very sure of itself.
|
|
29
|
-
|
|
30
|
-
```ruby
|
|
31
|
-
class Geordi
|
|
32
|
-
def make_it_so(logger=nil)
|
|
33
|
-
logger && logger.info("Reversing the flux phase capacitance!")
|
|
34
|
-
logger && logger.info("Bounding a tachyon particle beam off of Data's cat!")
|
|
35
|
-
logger && logger.warn("Warning, bogon levels are rising!")
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
Now, observe as we give it a dash of confidence with the Null Object
|
|
41
|
-
pattern!
|
|
42
|
-
|
|
43
|
-
```ruby
|
|
44
|
-
class NullLogger
|
|
45
|
-
def debug(*); end
|
|
46
|
-
def info(*); end
|
|
47
|
-
def warn(*); end
|
|
48
|
-
def error(*); end
|
|
49
|
-
def fatal(*); end
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
class Geordi
|
|
53
|
-
def make_it_so(logger=NullLogger.new)
|
|
54
|
-
logger.info "Reversing the flux phase capacitance!"
|
|
55
|
-
logger.info "Bounding a tachyon particle beam off of Data's cat!"
|
|
56
|
-
logger.warn "Warning, bogon levels are rising!"
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
By providing a `NullLogger` which implements [some of] the `Logger`
|
|
62
|
-
interface as no-op methods, we've gotten rid of those unsightly `&&`
|
|
63
|
-
operators.
|
|
64
|
-
|
|
65
|
-
#### That was simple enough. Why do I need a library for it?
|
|
66
|
-
|
|
67
|
-
You don't! The Null Object pattern is a very simple one at its core.
|
|
68
|
-
|
|
69
|
-
#### And yet here we are…
|
|
70
|
-
|
|
71
|
-
Yes. While you don't *need* a Null Object library, this one offers some
|
|
72
|
-
conveniences you probably won't find elsewhere.
|
|
73
|
-
|
|
74
|
-
But there's an even more important reason I wrote this library. In the
|
|
75
|
-
immortal last words of James T. Kirk: "It was… *fun!*"
|
|
76
|
-
|
|
77
|
-
#### OK, so how do I use this thing?
|
|
78
|
-
|
|
79
|
-
Well, what would you like to do?
|
|
80
|
-
|
|
81
|
-
#### I dunno, gimme an object that responds to any message with nil
|
|
82
|
-
|
|
83
|
-
Sure thing!
|
|
84
|
-
|
|
85
|
-
```ruby
|
|
86
|
-
require 'naught'
|
|
87
|
-
|
|
88
|
-
NullObject = Naught.build
|
|
89
|
-
|
|
90
|
-
null = NullObject.new
|
|
91
|
-
null.foo # => nil
|
|
92
|
-
null.bar # => nil
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
#### That was… weird. What's with this "build" business?
|
|
96
|
-
|
|
97
|
-
Naught is a *toolkit* for building null object classes. It is not a
|
|
98
|
-
one-size-fits-all solution.
|
|
99
|
-
|
|
100
|
-
What else can I make for you?
|
|
101
|
-
|
|
102
|
-
#### How about a "black hole" null object that supports infinite chaining of methods?
|
|
103
|
-
|
|
104
|
-
OK.
|
|
105
|
-
|
|
106
|
-
```ruby
|
|
107
|
-
require 'naught'
|
|
108
|
-
|
|
109
|
-
BlackHole = Naught.build do |config|
|
|
110
|
-
config.black_hole
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
null = BlackHole.new
|
|
114
|
-
null.foo # => <null>
|
|
115
|
-
null.foo.bar.baz # => <null>
|
|
116
|
-
null << "hello" << "world" # => <null>
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
#### What's that "config" thing?
|
|
120
|
-
|
|
121
|
-
That's what you use to customize the generated class to your
|
|
122
|
-
liking. Internally, Naught uses the [Builder
|
|
123
|
-
Pattern](http://en.wikipedia.org/wiki/Builder_pattern) to make this work..
|
|
124
|
-
|
|
125
|
-
#### Whatever. What if I want a null object that has conversions to Integer, String, etc. using sensible conversions to "zero values"?
|
|
126
|
-
|
|
127
|
-
We can do that.
|
|
128
|
-
|
|
129
|
-
```ruby
|
|
130
|
-
require 'naught'
|
|
131
|
-
|
|
132
|
-
NullObject = Naught.build do |config|
|
|
133
|
-
config.define_explicit_conversions
|
|
134
|
-
end
|
|
135
|
-
|
|
136
|
-
null = NullObject.new
|
|
137
|
-
|
|
138
|
-
null.to_s # => ""
|
|
139
|
-
null.to_i # => 0
|
|
140
|
-
null.to_f # => 0.0
|
|
141
|
-
null.to_a # => []
|
|
142
|
-
null.to_h # => {}
|
|
143
|
-
null.to_c # => (0+0i)
|
|
144
|
-
null.to_r # => (0/1)
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
#### Ah, but what about implicit conversions such as `#to_str`? Like what if I want a null object that implicitly splats the same way as an empty array?
|
|
148
|
-
|
|
149
|
-
Gotcha covered.
|
|
150
|
-
|
|
151
|
-
```ruby
|
|
152
|
-
require 'naught'
|
|
153
|
-
|
|
154
|
-
NullObject = Naught.build do |config|
|
|
155
|
-
config.define_implicit_conversions
|
|
156
|
-
end
|
|
157
|
-
|
|
158
|
-
null = NullObject.new
|
|
159
|
-
|
|
160
|
-
null.to_str # => ""
|
|
161
|
-
null.to_ary # => []
|
|
162
|
-
|
|
163
|
-
a, b, c = []
|
|
164
|
-
a # => nil
|
|
165
|
-
b # => nil
|
|
166
|
-
c # => nil
|
|
167
|
-
x, y, z = null
|
|
168
|
-
x # => nil
|
|
169
|
-
y # => nil
|
|
170
|
-
z # => nil
|
|
171
|
-
```
|
|
172
|
-
|
|
173
|
-
#### How about a null object that only stubs out the methods from a specific class?
|
|
174
|
-
|
|
175
|
-
That's what `mimic` is for.
|
|
176
|
-
|
|
177
|
-
```ruby
|
|
178
|
-
require 'naught'
|
|
179
|
-
|
|
180
|
-
NullIO = Naught.build do |config|
|
|
181
|
-
config.mimic IO
|
|
182
|
-
end
|
|
183
|
-
|
|
184
|
-
null_io = NullIO.new
|
|
185
|
-
|
|
186
|
-
null_io << "foo" # => nil
|
|
187
|
-
null_io.readline # => nil
|
|
188
|
-
null_io.foobar # =>
|
|
189
|
-
# ~> -:11:in `<main>': undefined method `foobar' for
|
|
190
|
-
# <null:IO>:NullIO (NoMethodError)
|
|
191
|
-
```
|
|
192
|
-
|
|
193
|
-
There is also `impersonate` which takes `mimic` one step further. The
|
|
194
|
-
generated null class will be derived from the impersonated class. This
|
|
195
|
-
is handy when refitting legacy code that contains type checks.
|
|
196
|
-
|
|
197
|
-
```ruby
|
|
198
|
-
require 'naught'
|
|
199
|
-
|
|
200
|
-
NullIO = Naught.build do |config|
|
|
201
|
-
config.impersonate IO
|
|
202
|
-
end
|
|
203
|
-
|
|
204
|
-
null_io = NullIO.new
|
|
205
|
-
IO === null_io # => true
|
|
206
|
-
|
|
207
|
-
case null_io
|
|
208
|
-
when IO
|
|
209
|
-
puts "Yep, checks out!"
|
|
210
|
-
null_io << "some output"
|
|
211
|
-
else
|
|
212
|
-
raise "Hey, I expected an IO!"
|
|
213
|
-
end
|
|
214
|
-
# >> Yep, checks out!
|
|
215
|
-
```
|
|
216
|
-
|
|
217
|
-
#### My objects are unique and special snowflakes, with new methods added to them at runtime. How are you gonna mimic *that*, hotshot?
|
|
218
|
-
|
|
219
|
-
So long as you can create an object to serve as an example, Naught can copy the interface of that object (both the methods defined by its class, and its singleton methods).
|
|
220
|
-
|
|
221
|
-
```ruby
|
|
222
|
-
require "naught"
|
|
223
|
-
require "logging"
|
|
224
|
-
|
|
225
|
-
log = Logging.logger["test"]
|
|
226
|
-
log.info
|
|
227
|
-
|
|
228
|
-
NullLog = Naught.build do |config|
|
|
229
|
-
config.mimic example: log
|
|
230
|
-
end
|
|
231
|
-
|
|
232
|
-
null_log = NullLog.new
|
|
233
|
-
null_log.info # => nil
|
|
234
|
-
```
|
|
235
|
-
|
|
236
|
-
#### What about predicate methods? You know, the ones that end with question marks? Shouldn't they return `false` instead of `nil`?
|
|
237
|
-
|
|
238
|
-
Sure, if you'd like.
|
|
239
|
-
|
|
240
|
-
```ruby
|
|
241
|
-
require 'naught'
|
|
242
|
-
|
|
243
|
-
NullObject = Naught.build do |config|
|
|
244
|
-
config.predicates_return false
|
|
245
|
-
end
|
|
246
|
-
|
|
247
|
-
null = NullObject.new
|
|
248
|
-
null.foo # => nil
|
|
249
|
-
null.bar? # => false
|
|
250
|
-
null.nil? # => false
|
|
251
|
-
```
|
|
252
|
-
|
|
253
|
-
#### Alright smartypants. What if I want to add my own methods?
|
|
254
|
-
|
|
255
|
-
Not a problem, just define them in the `.build` block.
|
|
256
|
-
|
|
257
|
-
```ruby
|
|
258
|
-
require 'naught'
|
|
259
|
-
|
|
260
|
-
NullObject = Naught.build do |config|
|
|
261
|
-
config.define_explicit_conversions
|
|
262
|
-
config.predicates_return false
|
|
263
|
-
def to_path
|
|
264
|
-
"/dev/null"
|
|
265
|
-
end
|
|
266
|
-
|
|
267
|
-
# You can override methods generated by Naught
|
|
268
|
-
def to_s
|
|
269
|
-
"NOTHING TO SEE HERE MOVE ALONG"
|
|
270
|
-
end
|
|
271
|
-
|
|
272
|
-
def nil?
|
|
273
|
-
true
|
|
274
|
-
end
|
|
275
|
-
end
|
|
276
|
-
|
|
277
|
-
null = NullObject.new
|
|
278
|
-
null.to_path # => "/dev/null"
|
|
279
|
-
null.to_s # => "NOTHING TO SEE HERE MOVE ALONG"
|
|
280
|
-
null.nil? # => true
|
|
281
|
-
```
|
|
282
|
-
|
|
283
|
-
#### Got anything else up your sleeve?
|
|
284
|
-
|
|
285
|
-
Well, we can make the null class a singleton, since null objects
|
|
286
|
-
generally have no state.
|
|
287
|
-
|
|
288
|
-
```ruby
|
|
289
|
-
require 'naught'
|
|
290
|
-
|
|
291
|
-
NullObject = Naught.build do |config|
|
|
292
|
-
config.singleton
|
|
293
|
-
end
|
|
294
|
-
|
|
295
|
-
null = NullObject.instance
|
|
296
|
-
|
|
297
|
-
null.__id__ # => 17844080
|
|
298
|
-
NullObject.instance.__id__ # => 17844080
|
|
299
|
-
NullObject.new # =>
|
|
300
|
-
# ~> -:11:in `<main>': private method `new' called for
|
|
301
|
-
# NullObject:Class (NoMethodError)
|
|
302
|
-
```
|
|
303
|
-
|
|
304
|
-
Speaking of null objects with state, we can also enable tracing. This is
|
|
305
|
-
handy for playing "where'd that null come from?!" Try doing *that* with
|
|
306
|
-
`nil`!
|
|
307
|
-
|
|
308
|
-
```ruby
|
|
309
|
-
require 'naught'
|
|
310
|
-
|
|
311
|
-
NullObject = Naught.build do |config|
|
|
312
|
-
config.traceable
|
|
313
|
-
end
|
|
314
|
-
|
|
315
|
-
null = NullObject.new # line 7
|
|
316
|
-
|
|
317
|
-
null.__file__ # => "example.rb"
|
|
318
|
-
null.__line__ # => 7
|
|
319
|
-
```
|
|
320
|
-
|
|
321
|
-
We can even conditionally enable either singleton mode (for production)
|
|
322
|
-
or tracing (for development). Here's an example of using the `$DEBUG`
|
|
323
|
-
global variable (set with the `-d` option to ruby) to choose which one.
|
|
324
|
-
|
|
325
|
-
```ruby
|
|
326
|
-
require 'naught'
|
|
327
|
-
|
|
328
|
-
NullObject = Naught.build do |config|
|
|
329
|
-
if $DEBUG
|
|
330
|
-
config.traceable
|
|
331
|
-
else
|
|
332
|
-
config.singleton
|
|
333
|
-
end
|
|
334
|
-
end
|
|
335
|
-
```
|
|
336
|
-
|
|
337
|
-
The only caveat is that when swapping between singleton and
|
|
338
|
-
non-singleton implementations, you should be careful to always
|
|
339
|
-
instantiate your null objects with `NullObject.get`, not `.new` or
|
|
340
|
-
`.instance`. `.get` will work whether the class is implemented as a
|
|
341
|
-
singleton or not.
|
|
342
|
-
|
|
343
|
-
```ruby
|
|
344
|
-
NullObject.get # => <null>
|
|
345
|
-
```
|
|
346
|
-
|
|
347
|
-
#### And if I want to know legacy code better?
|
|
348
|
-
|
|
349
|
-
Naught can make a null object behave as a pebble object.
|
|
350
|
-
|
|
351
|
-
```ruby
|
|
352
|
-
require 'naught'
|
|
353
|
-
|
|
354
|
-
NullObject = Naught.build do |config|
|
|
355
|
-
if $DEBUG
|
|
356
|
-
config.pebble
|
|
357
|
-
else
|
|
358
|
-
config.black_hole
|
|
359
|
-
end
|
|
360
|
-
end
|
|
361
|
-
```
|
|
362
|
-
|
|
363
|
-
Now you can pass the pebble object to your code and see which messages are sent to the pebble.
|
|
364
|
-
|
|
365
|
-
```ruby
|
|
366
|
-
null = NullObject.new
|
|
367
|
-
|
|
368
|
-
class MyConsumer < Struct.new(:producer)
|
|
369
|
-
def consume
|
|
370
|
-
producer.produce
|
|
371
|
-
end
|
|
372
|
-
end
|
|
373
|
-
|
|
374
|
-
MyConsumer.new(null).consume
|
|
375
|
-
# >> produce() from consume
|
|
376
|
-
# => <null>
|
|
377
|
-
```
|
|
378
|
-
|
|
379
|
-
#### Are you done yet?
|
|
380
|
-
|
|
381
|
-
Just one more thing. For maximum convenience, Naught-generated null
|
|
382
|
-
classes also come with a full suite of conversion functions which can be
|
|
383
|
-
included into your classes.
|
|
384
|
-
|
|
385
|
-
```ruby
|
|
386
|
-
require 'naught'
|
|
387
|
-
|
|
388
|
-
NullObject = Naught.build
|
|
389
|
-
|
|
390
|
-
include NullObject::Conversions
|
|
391
|
-
|
|
392
|
-
# Convert nil to null objects. Everything else passes through.
|
|
393
|
-
Maybe(42) # => 42
|
|
394
|
-
Maybe(nil) # => <null>
|
|
395
|
-
Maybe(NullObject.get) # => <null>
|
|
396
|
-
Maybe{ 42 } # => 42
|
|
397
|
-
|
|
398
|
-
# Insist on a non-null (or nil) value
|
|
399
|
-
Just(42) # => 42
|
|
400
|
-
Just(nil) rescue $! # => #<ArgumentError: Null value: nil>
|
|
401
|
-
Just(NullObject.get) rescue $! # => #<ArgumentError: Null value: <null>>
|
|
402
|
-
|
|
403
|
-
# nils and nulls become nulls. Everything else is rejected.
|
|
404
|
-
Null() # => <null>
|
|
405
|
-
Null(42) rescue $! # => #<ArgumentError: 42 is not null!>
|
|
406
|
-
Null(nil) # => <null>
|
|
407
|
-
Null(NullObject.get) # => <null>
|
|
408
|
-
|
|
409
|
-
# Convert nulls back to nils. Everything else passes through. Useful
|
|
410
|
-
# for preventing null objects from "leaking" into public API return
|
|
411
|
-
# values.
|
|
412
|
-
Actual(42) # => 42
|
|
413
|
-
Actual(nil) # => nil
|
|
414
|
-
Actual(NullObject.get) # => nil
|
|
415
|
-
Actual { 42 } # => 42
|
|
416
|
-
```
|
|
417
|
-
|
|
418
|
-
Installation
|
|
419
|
-
--------------
|
|
420
|
-
|
|
421
|
-
``` {.example}
|
|
422
|
-
gem install naught
|
|
423
|
-
```
|
|
424
|
-
|
|
425
|
-
Requirements
|
|
426
|
-
--------------
|
|
427
|
-
|
|
428
|
-
- Ruby
|
|
429
|
-
|
|
430
|
-
Contributing
|
|
431
|
-
--------------
|
|
432
|
-
|
|
433
|
-
- Fork, branch, submit PR, blah blah blah. Don't forget tests.
|
|
434
|
-
|
|
435
|
-
Who's responsible
|
|
436
|
-
-------------------
|
|
437
|
-
|
|
438
|
-
Naught is by [Avdi Grimm](http://devblog.avdi.org/).
|
|
439
|
-
|
|
440
|
-
Prior Art
|
|
441
|
-
---------
|
|
442
|
-
|
|
443
|
-
This isn't the first Ruby Null Object library. Others to check out include:
|
|
444
|
-
|
|
445
|
-
- [NullAndVoid](https://github.com/jfelchner/null_and_void)
|
|
446
|
-
- [BlankSlate](https://github.com/saturnflyer/blank_slate)
|
|
447
|
-
|
|
448
|
-
The Book
|
|
449
|
-
--------
|
|
450
|
-
|
|
451
|
-
If you've read this far, you might be interested in the short ebook, [*Much Ado About Naught*](https://shiprise.dpdcart.com/cart/add?product_id=64334&method_id=66165), I (Avdi) wrote as I developed this library. It's a fun exploration of Ruby metaprogramming techniques as applied to writing a Ruby gem. You can [read the introduction here](http://devblog.avdi.org/introduction-to-much-ado-about-naught/).
|
|
452
|
-
|
|
453
|
-
Further reading
|
|
454
|
-
-----------------
|
|
455
|
-
|
|
456
|
-
- [Null Object: Something for
|
|
457
|
-
Nothing](http://www.two-sdg.demon.co.uk/curbralan/papers/europlop/NullObject.pdf)
|
|
458
|
-
(PDF) by Kevlin Henney
|
|
459
|
-
- [The Null Object
|
|
460
|
-
Pattern](http://www.cs.oberlin.edu/~jwalker/refs/woolf.ps) (PS) by
|
|
461
|
-
Bobby Woolf
|
|
462
|
-
- [NullObject](http://www.c2.com/cgi/wiki?NullObject) on WikiWiki
|
|
463
|
-
- [Null Object
|
|
464
|
-
pattern](http://en.wikipedia.org/wiki/Null_Object_pattern) on
|
|
465
|
-
Wikipedia
|
|
466
|
-
- [Null Objects and
|
|
467
|
-
Falsiness](http://devblog.avdi.org/2011/05/30/null-objects-and-falsiness/),
|
|
468
|
-
by Avdi Grimm
|
|
469
|
-
|
|
470
|
-
Libraries Using Naught
|
|
471
|
-
-----------------------
|
|
472
|
-
|
|
473
|
-
- [ActiveNull](https://github.com/Originate/active_null) Null Model support for ActiveRecord.
|
|
474
|
-
- [Twitter](https://github.com/sferik/twitter) A Ruby interface to the Twitter API.
|
data/Rakefile
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
require 'bundler/gem_tasks'
|
|
2
|
-
require 'rspec/core/rake_task'
|
|
3
|
-
|
|
4
|
-
RSpec::Core::RakeTask.new(:spec)
|
|
5
|
-
|
|
6
|
-
begin
|
|
7
|
-
require 'rubocop/rake_task'
|
|
8
|
-
RuboCop::RakeTask.new
|
|
9
|
-
rescue LoadError
|
|
10
|
-
task :rubocop do
|
|
11
|
-
$stderr.puts 'Rubocop is disabled'
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
task :default => [:spec, :rubocop]
|
data/naught.gemspec
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
# coding: utf-8
|
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
-
require 'naught/version'
|
|
5
|
-
|
|
6
|
-
Gem::Specification.new do |spec|
|
|
7
|
-
spec.name = 'naught'
|
|
8
|
-
spec.version = Naught::VERSION
|
|
9
|
-
spec.authors = ['Avdi Grimm']
|
|
10
|
-
spec.email = ['avdi@avdi.org']
|
|
11
|
-
spec.description = 'Naught is a toolkit for building Null Objects'
|
|
12
|
-
spec.summary = spec.description
|
|
13
|
-
spec.homepage = 'https://github.com/avdi/naught'
|
|
14
|
-
spec.license = 'MIT'
|
|
15
|
-
|
|
16
|
-
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
|
17
|
-
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
18
|
-
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
19
|
-
spec.require_paths = ['lib']
|
|
20
|
-
|
|
21
|
-
spec.add_development_dependency 'bundler', '~> 1.3'
|
|
22
|
-
end
|
data/spec/base_object_spec.rb
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe 'null object with a custom base class' do
|
|
4
|
-
subject(:null) { custom_base_null_class.new }
|
|
5
|
-
|
|
6
|
-
let(:custom_base_null_class) do
|
|
7
|
-
Naught.build do |b|
|
|
8
|
-
b.base_class = Object
|
|
9
|
-
end
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
it 'responds to base class methods' do
|
|
13
|
-
expect(null.methods).to be_an Array
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
it 'responds to unknown methods' do
|
|
17
|
-
expect(null.foo).to be_nil
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
it 'exposes the default base class choice, for the curious' do
|
|
21
|
-
default_base_class = :not_set
|
|
22
|
-
Naught.build do |b|
|
|
23
|
-
default_base_class = b.base_class
|
|
24
|
-
end
|
|
25
|
-
expect(default_base_class).to eq(Naught::BasicObject)
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
describe 'singleton null object' do
|
|
29
|
-
subject(:null_instance) { custom_base_singleton_null_class.instance }
|
|
30
|
-
|
|
31
|
-
let(:custom_base_singleton_null_class) do
|
|
32
|
-
Naught.build do |b|
|
|
33
|
-
b.singleton
|
|
34
|
-
b.base_class = Object
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
it 'can be cloned' do
|
|
39
|
-
expect(null_instance.clone).to be(null_instance)
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
it 'can be duplicated' do
|
|
43
|
-
expect(null_instance.dup).to be(null_instance)
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
end
|