null_object 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -11,23 +11,13 @@ class NullObject
11
11
  @return_block = return_block
12
12
  end
13
13
 
14
- def method_missing(method, *args)
15
- matched = false
16
- return_value = if @methods.empty?
17
- matched = true and __global_return_value__
18
- elsif @methods.is_a?(Hash) && @methods.key?(method.to_sym)
19
- matched = true and @methods[method.to_sym]
20
- elsif @methods.is_a?(Enumerable) && @methods.include?(method.to_sym)
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
@@ -1,3 +1,3 @@
1
1
  class NullObject
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -17,5 +17,6 @@ Gem::Specification.new do |gem|
17
17
 
18
18
  gem.add_development_dependency "rspec", "~>2.10.0"
19
19
  gem.add_development_dependency "bourne", "~>1.1.2"
20
+ gem.add_development_dependency "pry"
20
21
  gem.add_development_dependency "rake"
21
22
  end
@@ -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
@@ -1,4 +1,5 @@
1
1
  require 'mocha'
2
+ require 'pry'
2
3
  require File.join(File.dirname(__FILE__), '..', 'lib', 'null_object')
3
4
 
4
5
  RSpec.configure do |config|
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.2
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: &70364672404620 !ruby/object:Gem::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: *70364672404620
24
+ version_requirements: *70206631894660
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bourne
27
- requirement: &70364672403920 !ruby/object:Gem::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: *70364672403920
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: &70364672403540 !ruby/object:Gem::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: *70364672403540
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: -2648222180714243262
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: -2648222180714243262
100
+ hash: 2394258275185068831
90
101
  requirements: []
91
102
  rubyforge_project:
92
103
  rubygems_version: 1.8.15