null_object 0.0.2 → 0.0.3
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.
- data/README.md +36 -0
- data/lib/null_object.rb +9 -19
- data/lib/null_object/version.rb +1 -1
- data/null_object.gemspec +1 -0
- data/spec/null_object_spec.rb +10 -0
- data/spec/spec_helper.rb +1 -0
- metadata +20 -9
data/README.md
CHANGED
@@ -3,6 +3,35 @@
|
|
3
3
|
Dead simple library to create null objects (objects that respond to all
|
4
4
|
messages)
|
5
5
|
|
6
|
+
## Why?
|
7
|
+
|
8
|
+
Imagine that sometimes your code uses a [statsd](https://github.com/etsy/statsd)
|
9
|
+
client to instrument itself. But only sometimes.
|
10
|
+
|
11
|
+
If you used `nil` to represent the case where the statsd client isn't configured,
|
12
|
+
you end up writing code like this ... which sucks:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
statsd.increment("foo") if statsd
|
16
|
+
```
|
17
|
+
|
18
|
+
And how to you deal with timers? I have no idea
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
# statsd might be nil, ugh!
|
22
|
+
statsd.timer("foo") { ... }
|
23
|
+
```
|
24
|
+
|
25
|
+
But if your `statsd` were either a real `Statsd` client **or a `NullObject`**,
|
26
|
+
the problems go away:
|
27
|
+
|
28
|
+
```
|
29
|
+
obj = NullObject.new { |&block| block.call if block }
|
30
|
+
|
31
|
+
@statsd.increment("foo") # no need for a conditional; it's a no-op
|
32
|
+
@statsd.timer("foo") { ... } # yields to the block, but otherwise a no-op
|
33
|
+
```
|
34
|
+
|
6
35
|
## Usage
|
7
36
|
|
8
37
|
Respond to ALL the things:
|
@@ -14,6 +43,13 @@ obj.bar # => obj
|
|
14
43
|
obj.foo.bar # => obj
|
15
44
|
```
|
16
45
|
|
46
|
+
Yield ALL the things:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
obj = NullObject.new { |&block| block.call if block }
|
50
|
+
obj.foo { puts "bar" } # outputs "bar"
|
51
|
+
```
|
52
|
+
|
17
53
|
Respond to SOME of the things:
|
18
54
|
|
19
55
|
```ruby
|
data/lib/null_object.rb
CHANGED
@@ -11,23 +11,13 @@ class NullObject
|
|
11
11
|
@return_block = return_block
|
12
12
|
end
|
13
13
|
|
14
|
-
def method_missing(method, *args)
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
matched = true and __global_return_value__
|
22
|
-
end
|
23
|
-
|
24
|
-
if matched
|
25
|
-
# Next time, there'll be a real method and we'll avoid the method_missing
|
26
|
-
# chain
|
27
|
-
singleton_class = class << self; self; end;
|
28
|
-
singleton_class.__send__(:define_method, method) { return_value }
|
29
|
-
|
30
|
-
return_value
|
14
|
+
def method_missing(method, *args, &block)
|
15
|
+
if @methods.empty?
|
16
|
+
__global_return_value__(*args, &block)
|
17
|
+
elsif @methods.is_a?(Hash) && @methods.key?(method.to_sym)
|
18
|
+
@methods[method.to_sym]
|
19
|
+
elsif @methods.is_a?(Enumerable) && @methods.include?(method.to_sym)
|
20
|
+
__global_return_value__(*args, &block)
|
31
21
|
else
|
32
22
|
super
|
33
23
|
end
|
@@ -35,7 +25,7 @@ class NullObject
|
|
35
25
|
|
36
26
|
private
|
37
27
|
|
38
|
-
def __global_return_value__
|
39
|
-
@return_block ? @return_block.call : self
|
28
|
+
def __global_return_value__(*args, &block)
|
29
|
+
@return_block ? @return_block.call(*args, &block) : self
|
40
30
|
end
|
41
31
|
end
|
data/lib/null_object/version.rb
CHANGED
data/null_object.gemspec
CHANGED
data/spec/null_object_spec.rb
CHANGED
@@ -8,6 +8,16 @@ describe NullObject do
|
|
8
8
|
obj.foo.bar.should be obj
|
9
9
|
end
|
10
10
|
|
11
|
+
it "yields ALL the things" do
|
12
|
+
obj = NullObject.new { |&block| block.call if block }
|
13
|
+
obj.foo.should be_nil
|
14
|
+
|
15
|
+
called = false
|
16
|
+
obj.foo { called = true }
|
17
|
+
|
18
|
+
called.should be_true
|
19
|
+
end
|
20
|
+
|
11
21
|
it "responds to SOME of the things" do
|
12
22
|
obj = NullObject.new(:foo, :bar)
|
13
23
|
obj.foo.should be obj
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: null_object
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-05-10 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70206631894660 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 2.10.0
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70206631894660
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: bourne
|
27
|
-
requirement: &
|
27
|
+
requirement: &70206631893640 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,21 @@ dependencies:
|
|
32
32
|
version: 1.1.2
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70206631893640
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: pry
|
38
|
+
requirement: &70206631893100 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70206631893100
|
36
47
|
- !ruby/object:Gem::Dependency
|
37
48
|
name: rake
|
38
|
-
requirement: &
|
49
|
+
requirement: &70206631908780 !ruby/object:Gem::Requirement
|
39
50
|
none: false
|
40
51
|
requirements:
|
41
52
|
- - ! '>='
|
@@ -43,7 +54,7 @@ dependencies:
|
|
43
54
|
version: '0'
|
44
55
|
type: :development
|
45
56
|
prerelease: false
|
46
|
-
version_requirements: *
|
57
|
+
version_requirements: *70206631908780
|
47
58
|
description: Dead simple library to create null objects (objects that respond to all
|
48
59
|
messages)
|
49
60
|
email:
|
@@ -77,7 +88,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
77
88
|
version: '0'
|
78
89
|
segments:
|
79
90
|
- 0
|
80
|
-
hash:
|
91
|
+
hash: 2394258275185068831
|
81
92
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
93
|
none: false
|
83
94
|
requirements:
|
@@ -86,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
86
97
|
version: '0'
|
87
98
|
segments:
|
88
99
|
- 0
|
89
|
-
hash:
|
100
|
+
hash: 2394258275185068831
|
90
101
|
requirements: []
|
91
102
|
rubyforge_project:
|
92
103
|
rubygems_version: 1.8.15
|