null_object 0.0.1
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/.gitignore +17 -0
- data/.rspec +4 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +55 -0
- data/Rakefile +2 -0
- data/lib/null_object.rb +41 -0
- data/lib/null_object/version.rb +3 -0
- data/null_object.gemspec +21 -0
- data/spec/null_object_spec.rb +34 -0
- data/spec/spec_helper.rb +9 -0
- metadata +98 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Andy Lindeman
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# NullObject
|
2
|
+
|
3
|
+
Dead simple library to create null objects (objects that respond to all
|
4
|
+
messages)
|
5
|
+
|
6
|
+
## Usage
|
7
|
+
|
8
|
+
Respond to ALL the things:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
obj = NullObject.new
|
12
|
+
obj.foo # => nil
|
13
|
+
obj.bar # => nil
|
14
|
+
```
|
15
|
+
|
16
|
+
Respond to SOME of the things:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
obj = NullObject.new(:foo, :bar)
|
20
|
+
obj.foo # => nil
|
21
|
+
obj.bar # => nil
|
22
|
+
obj.baz # raises NoMethodError
|
23
|
+
```
|
24
|
+
|
25
|
+
Respond to SOME of the things with CERTAIN return values:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
obj = NullObject.new(:foo => :bar)
|
29
|
+
obj.foo # => :bar
|
30
|
+
obj.baz # raises NoMethodError
|
31
|
+
```
|
32
|
+
|
33
|
+
Respond to ALL of the things with a CERTAIN return value:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
obj = NullObject.new { "foo" }
|
37
|
+
obj.foo # => "foo"
|
38
|
+
obj.bar # => "foo"
|
39
|
+
```
|
40
|
+
|
41
|
+
Respond to ALL of the things with `self`:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
obj = NullObject.new { obj }
|
45
|
+
obj.foo # => obj
|
46
|
+
obj.foo.bar.baz # => obj
|
47
|
+
```
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
1. Fork it
|
52
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
53
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
54
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
55
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/null_object.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require "null_object/version"
|
2
|
+
|
3
|
+
class NullObject
|
4
|
+
def initialize(*methods, &return_block)
|
5
|
+
if methods.first.is_a?(Hash)
|
6
|
+
@methods = methods.first
|
7
|
+
else
|
8
|
+
@methods = methods
|
9
|
+
end
|
10
|
+
|
11
|
+
@return_block = return_block
|
12
|
+
end
|
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
|
31
|
+
else
|
32
|
+
super
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def __global_return_value__
|
39
|
+
@return_block && @return_block.call
|
40
|
+
end
|
41
|
+
end
|
data/null_object.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/null_object/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Andy Lindeman"]
|
6
|
+
gem.email = ["alindeman@gmail.com"]
|
7
|
+
gem.description = %q{Dead simple library to create null objects (objects that respond to all messages)}
|
8
|
+
gem.summary = %q{Respond to ALL the things}
|
9
|
+
gem.homepage = "https://github.com/alindeman/null_object"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "null_object"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = NullObject::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency "rspec", "~>2.10.0"
|
19
|
+
gem.add_development_dependency "bourne", "~>1.1.2"
|
20
|
+
gem.add_development_dependency "rake"
|
21
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe NullObject do
|
4
|
+
it "responds to ALL of the things" do
|
5
|
+
obj = NullObject.new
|
6
|
+
obj.foo.should be_nil
|
7
|
+
obj.bar.should be_nil
|
8
|
+
end
|
9
|
+
|
10
|
+
it "responds to SOME of the things" do
|
11
|
+
obj = NullObject.new(:foo, :bar)
|
12
|
+
obj.foo.should be_nil
|
13
|
+
obj.bar.should be_nil
|
14
|
+
expect { obj.baz }.to raise_error(NoMethodError)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "responds to SOME of the things with CERTAIN return values" do
|
18
|
+
obj = NullObject.new(:foo => :bar)
|
19
|
+
obj.foo.should == :bar
|
20
|
+
expect { obj.baz }.to raise_error(NoMethodError)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "responds to ALL of the things with a CERTAIN return value" do
|
24
|
+
obj = NullObject.new { "foo" }
|
25
|
+
obj.foo.should == "foo"
|
26
|
+
obj.bar.should == "foo"
|
27
|
+
end
|
28
|
+
|
29
|
+
it "responds to ALL of the things with `self`" do
|
30
|
+
obj = NullObject.new { obj }
|
31
|
+
obj.foo.should equal obj
|
32
|
+
obj.foo.bar.baz.should equal obj
|
33
|
+
end
|
34
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'mocha'
|
2
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'null_object')
|
3
|
+
|
4
|
+
RSpec.configure do |config|
|
5
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
6
|
+
config.run_all_when_everything_filtered = true
|
7
|
+
config.filter_run :focus
|
8
|
+
config.mock_with :mocha
|
9
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: null_object
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Andy Lindeman
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-10 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70359975280920 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.10.0
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70359975280920
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: bourne
|
27
|
+
requirement: &70359975280220 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.1.2
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70359975280220
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rake
|
38
|
+
requirement: &70359975279840 !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: *70359975279840
|
47
|
+
description: Dead simple library to create null objects (objects that respond to all
|
48
|
+
messages)
|
49
|
+
email:
|
50
|
+
- alindeman@gmail.com
|
51
|
+
executables: []
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- .gitignore
|
56
|
+
- .rspec
|
57
|
+
- Gemfile
|
58
|
+
- LICENSE
|
59
|
+
- README.md
|
60
|
+
- Rakefile
|
61
|
+
- lib/null_object.rb
|
62
|
+
- lib/null_object/version.rb
|
63
|
+
- null_object.gemspec
|
64
|
+
- spec/null_object_spec.rb
|
65
|
+
- spec/spec_helper.rb
|
66
|
+
homepage: https://github.com/alindeman/null_object
|
67
|
+
licenses: []
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
hash: -2749999320314441199
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
hash: -2749999320314441199
|
90
|
+
requirements: []
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 1.8.15
|
93
|
+
signing_key:
|
94
|
+
specification_version: 3
|
95
|
+
summary: Respond to ALL the things
|
96
|
+
test_files:
|
97
|
+
- spec/null_object_spec.rb
|
98
|
+
- spec/spec_helper.rb
|