nullobject 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/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README.md +47 -0
- data/Rakefile +12 -0
- data/lib/nullobject.rb +39 -0
- data/lib/nullobject/version.rb +3 -0
- data/nullobject.gemspec +23 -0
- data/spec/lib/nullobject_spec.rb +85 -0
- data/spec/spec_helper.rb +14 -0
- metadata +97 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Marcin Ciunelis
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# NullObject[](http://travis-ci.org/martinciu/nullobject)
|
2
|
+
### Intent
|
3
|
+
|
4
|
+
The intent of a Null Object is to encapsulate the absence of an object by providing a substitutable alternative that offers suitable default do nothing behavior. In short, a design where “nothing will come of nothing”
|
5
|
+
|
6
|
+
## Setup
|
7
|
+
|
8
|
+
If you are using bundler add nullobject to your Gemfile:
|
9
|
+
|
10
|
+
gem 'nullobject'
|
11
|
+
|
12
|
+
Then run:
|
13
|
+
|
14
|
+
bundle install
|
15
|
+
|
16
|
+
Otherwise install the gem:
|
17
|
+
|
18
|
+
gem install nullobject
|
19
|
+
|
20
|
+
and require it in your project:
|
21
|
+
|
22
|
+
require 'nullobject'
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
[TODO]
|
27
|
+
|
28
|
+
## Development
|
29
|
+
|
30
|
+
Source hosted at [GitHub](http://github.com/martinciu/nullobject).
|
31
|
+
Report Issues/Feature requests on [GitHub Issues](http://github.com/martinciu/nullobject/issues).
|
32
|
+
|
33
|
+
Tests can be ran with `rake test`
|
34
|
+
|
35
|
+
### Note on Patches/Pull Requests
|
36
|
+
|
37
|
+
* Fork the project.
|
38
|
+
* Make your feature addition or bug fix.
|
39
|
+
* Add tests for it. This is important so I don't break it in a
|
40
|
+
future version unintentionally.
|
41
|
+
* Commit, do not mess with rakefile, version, or history.
|
42
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
43
|
+
* Send me a pull request. Bonus points for topic branches.
|
44
|
+
|
45
|
+
## Copyright
|
46
|
+
|
47
|
+
Copyright (c) 2011 Marcin Ciunelis. See [LICENSE](https://github.com/martinciu/nullobject/blob/master/LICENSE) for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
Bundler.setup(:default, :test)
|
5
|
+
|
6
|
+
Rake::TestTask.new do |t|
|
7
|
+
t.libs << "spec"
|
8
|
+
t.test_files = FileList['spec/**/*_spec.rb']
|
9
|
+
t.verbose = true
|
10
|
+
end
|
11
|
+
|
12
|
+
task :default => :test
|
data/lib/nullobject.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require "singleton"
|
2
|
+
|
3
|
+
class NullObject
|
4
|
+
include Singleton
|
5
|
+
|
6
|
+
def self.maybe(value)
|
7
|
+
value.nil? ? instance : value
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_a; []; end
|
11
|
+
alias :to_ary :to_a
|
12
|
+
def to_s; ""; end
|
13
|
+
alias :to_str :to_s
|
14
|
+
def to_f; 0.0; end
|
15
|
+
def to_i; 0; end
|
16
|
+
def nil?; true; end
|
17
|
+
def +(other); other; end
|
18
|
+
def -(other); coerced(other) - other; end
|
19
|
+
|
20
|
+
def coerce(other)
|
21
|
+
[other, coerced(other)]
|
22
|
+
end
|
23
|
+
|
24
|
+
def method_missing(*args, &block)
|
25
|
+
self
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
def coerced(other)
|
30
|
+
send({
|
31
|
+
Integer => :to_i,
|
32
|
+
Fixnum => :to_i,
|
33
|
+
String => :to_s,
|
34
|
+
Float => :to_f,
|
35
|
+
Array => :to_a
|
36
|
+
}[other.class])
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
data/nullobject.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/nullobject/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Marcin Ciunelis"]
|
6
|
+
gem.email = ["marcin.ciunelis@gmail.com"]
|
7
|
+
gem.description = %q{Null Object Design Pattern Ruby implementation}
|
8
|
+
gem.summary = %q{}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "nullobject"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Nullobject::VERSION
|
17
|
+
|
18
|
+
|
19
|
+
gem.add_development_dependency "rake", "~> 0.9.2"
|
20
|
+
gem.add_development_dependency "minitest", "~> 2.7.0"
|
21
|
+
gem.add_development_dependency "turn", "~> 0.8.3"
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "NullObject" do
|
4
|
+
|
5
|
+
describe "instance methods" do
|
6
|
+
before do
|
7
|
+
@it = NullObject.instance
|
8
|
+
end
|
9
|
+
|
10
|
+
it "#to_s return empty string" do
|
11
|
+
@it.to_s.must_equal ""
|
12
|
+
end
|
13
|
+
|
14
|
+
it "#to_str return empty string" do
|
15
|
+
@it.to_str.must_equal ""
|
16
|
+
end
|
17
|
+
|
18
|
+
it "#to_a return empty array" do
|
19
|
+
@it.to_a.must_equal []
|
20
|
+
end
|
21
|
+
|
22
|
+
it "#to_f return 0.0" do
|
23
|
+
@it.to_f.must_equal 0.0
|
24
|
+
end
|
25
|
+
|
26
|
+
it "#to_i return 0" do
|
27
|
+
@it.to_i.must_equal 0
|
28
|
+
end
|
29
|
+
|
30
|
+
it "#nil? return true" do
|
31
|
+
@it.nil?.must_equal true
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "#+" do
|
35
|
+
it "does not modify integer" do
|
36
|
+
(@it + 5).must_equal 5
|
37
|
+
(5 + @it).must_equal 5
|
38
|
+
end
|
39
|
+
|
40
|
+
it "does not modify float" do
|
41
|
+
(@it + 5.0).must_equal 5.0
|
42
|
+
(5.0 + @it).must_equal 5.0
|
43
|
+
end
|
44
|
+
|
45
|
+
it "does not modify string" do
|
46
|
+
(@it + "foo").must_equal "foo"
|
47
|
+
("foo" + @it).must_equal "foo"
|
48
|
+
end
|
49
|
+
|
50
|
+
it "does not modify array" do
|
51
|
+
(@it + [1, 3, "foo"]).must_equal [1, 3, "foo"]
|
52
|
+
([1, 3, "foo"] + @it).must_equal [1, 3, "foo"]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "#-" do
|
57
|
+
it "does not modify integer" do
|
58
|
+
(5 - @it).must_equal 5
|
59
|
+
end
|
60
|
+
|
61
|
+
it "acts as empty if other substracts from" do
|
62
|
+
(@it - 5).must_equal -5
|
63
|
+
(@it - [5]).must_equal []
|
64
|
+
end
|
65
|
+
|
66
|
+
it "does not modify array" do
|
67
|
+
([1, 3, "foo"] - @it).must_equal [1, 3, "foo"]
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
describe "#maybe" do
|
74
|
+
it "returns NullObject if value is nil" do
|
75
|
+
NullObject.maybe(nil).must_be_kind_of NullObject
|
76
|
+
end
|
77
|
+
|
78
|
+
it "returns value if it is not nil" do
|
79
|
+
val = "foo"
|
80
|
+
NullObject.maybe(val).must_equal val
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
Bundler.setup(:default, :test)
|
4
|
+
Bundler.require(:default, :test)
|
5
|
+
|
6
|
+
dir = File.dirname(File.expand_path(__FILE__))
|
7
|
+
$LOAD_PATH.unshift dir + '/../lib'
|
8
|
+
$TESTING = true
|
9
|
+
require 'minitest/spec'
|
10
|
+
require 'minitest/autorun'
|
11
|
+
require 'turn'
|
12
|
+
|
13
|
+
require 'nullobject'
|
14
|
+
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nullobject
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Marcin Ciunelis
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-18 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: &70204139217140 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.9.2
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70204139217140
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: minitest
|
27
|
+
requirement: &70204139215840 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 2.7.0
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70204139215840
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: turn
|
38
|
+
requirement: &70204139214100 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 0.8.3
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70204139214100
|
47
|
+
description: Null Object Design Pattern Ruby implementation
|
48
|
+
email:
|
49
|
+
- marcin.ciunelis@gmail.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- .travis.yml
|
56
|
+
- Gemfile
|
57
|
+
- LICENSE
|
58
|
+
- README.md
|
59
|
+
- Rakefile
|
60
|
+
- lib/nullobject.rb
|
61
|
+
- lib/nullobject/version.rb
|
62
|
+
- nullobject.gemspec
|
63
|
+
- spec/lib/nullobject_spec.rb
|
64
|
+
- spec/spec_helper.rb
|
65
|
+
homepage: ''
|
66
|
+
licenses: []
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
hash: 1565268376526755474
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
hash: 1565268376526755474
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 1.8.10
|
92
|
+
signing_key:
|
93
|
+
specification_version: 3
|
94
|
+
summary: ''
|
95
|
+
test_files:
|
96
|
+
- spec/lib/nullobject_spec.rb
|
97
|
+
- spec/spec_helper.rb
|