no 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +138 -0
- data/Rakefile +15 -0
- data/lib/no/version.rb +3 -0
- data/lib/no.rb +51 -0
- data/no.gemspec +25 -0
- data/spec/no_spec.rb +132 -0
- data/spec/spec_helper.rb +4 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2b1c1ef28920e21755295b837fb86b47a7a32e91
|
4
|
+
data.tar.gz: 0408d4fd3dc0c9783b6b4363e8f58afff44d2523
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1e2dd2ef13c18a7e3cc76ac24f739ccfb77869e94af8ed40b4c7e066bc03d3419ac5e469a6650e026a805c0d429ff1938346d7ae2230ca4ed12edc6b52d75b57
|
7
|
+
data.tar.gz: 36661411593f73a34ee343eb21f1382bf31433221b5383b14ba8a46ba49c4c8067b332f8bf37c012e52125816065556ac0a4c3c99ebf009a8f129d8ee5f4d576
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Takatoshi Matsumoto
|
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,138 @@
|
|
1
|
+
# NO
|
2
|
+
|
3
|
+
Support creating null object in Ruby. NO::NullObject basically bahaves as nil.
|
4
|
+
|
5
|
+
1. NO::NullObject basically delegates to nil.
|
6
|
+
2. if nil doesn't respond to a method, NO::NullObject returns nil.
|
7
|
+
|
8
|
+
If you want to customize, inherit NO::NullObject
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```
|
15
|
+
gem 'no'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
```
|
21
|
+
$ bundle
|
22
|
+
```
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
```
|
27
|
+
$ gem install no
|
28
|
+
```
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
### Simple
|
33
|
+
|
34
|
+
```
|
35
|
+
def write(out = NO::NullObject.new, body)
|
36
|
+
out.write body
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
### Custom
|
41
|
+
|
42
|
+
```
|
43
|
+
class User
|
44
|
+
class Annonymous < NO::NullObject
|
45
|
+
def name
|
46
|
+
'annonymous'
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
ANNONYMOUS = Annonymous.new
|
51
|
+
|
52
|
+
attr_reader :name
|
53
|
+
|
54
|
+
def initialize name
|
55
|
+
@name = name
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class UserRepository
|
60
|
+
class << self
|
61
|
+
def users
|
62
|
+
[User.new('Foo'), User.new('Bar'), User.new('Baz')]
|
63
|
+
end
|
64
|
+
|
65
|
+
def find id
|
66
|
+
users[id] || User::ANNONYMOUS
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
UserRepository.find(3).name #=> 'annonymous'
|
72
|
+
```
|
73
|
+
|
74
|
+
## Spec
|
75
|
+
|
76
|
+
```
|
77
|
+
|
78
|
+
NO::NullObject
|
79
|
+
can expand by inheritance
|
80
|
+
to_a
|
81
|
+
should eq []
|
82
|
+
to_c
|
83
|
+
should eq (0+0i)
|
84
|
+
to_f
|
85
|
+
should eq 0.0
|
86
|
+
to_h
|
87
|
+
should eq {}
|
88
|
+
to_i
|
89
|
+
should eq 0
|
90
|
+
to_r
|
91
|
+
should eq (0/1)
|
92
|
+
to_s
|
93
|
+
should eq ""
|
94
|
+
rationalize
|
95
|
+
should eq (0/1)
|
96
|
+
#&
|
97
|
+
NO::NullObject & object
|
98
|
+
should be false
|
99
|
+
#^
|
100
|
+
NO::NullObject ^ nil
|
101
|
+
should eq false
|
102
|
+
NO::NullObject ^ false
|
103
|
+
should eq false
|
104
|
+
NO::NullObject ^ NO::NullObject
|
105
|
+
should eq false
|
106
|
+
NO::NullObject ^ object(that is not nil, false or NO::NullObject)
|
107
|
+
should eq true
|
108
|
+
#|
|
109
|
+
NO::NullObject | nil
|
110
|
+
should eq false
|
111
|
+
NO::NullObject | false
|
112
|
+
should eq false
|
113
|
+
NO::NullObject | NO::NullObject
|
114
|
+
should eq false
|
115
|
+
NO::NullObject | object(that is not nil, false or NO::NullObject)
|
116
|
+
should eq true
|
117
|
+
#==
|
118
|
+
NO::NullObject == nil
|
119
|
+
should be true
|
120
|
+
NO::NullObject == NO::NullObject (Same instance)
|
121
|
+
should be true
|
122
|
+
NO::NullObject == NO::NullObject (Different instance)
|
123
|
+
should be false
|
124
|
+
#!
|
125
|
+
should be true
|
126
|
+
#nil?
|
127
|
+
should be true
|
128
|
+
methods that nil don't have
|
129
|
+
should return nil
|
130
|
+
```
|
131
|
+
|
132
|
+
## Contributing
|
133
|
+
|
134
|
+
1. Fork it ( http://github.com/<my-github-username>/no/fork )
|
135
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
136
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
137
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
138
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "bundler/gem_helper"
|
3
|
+
|
4
|
+
t = Bundler::GemHelper.new
|
5
|
+
|
6
|
+
desc "Create tag #{t.send(:version_tag)}"
|
7
|
+
task :tag do
|
8
|
+
unless t.send(:already_tagged?)
|
9
|
+
t.send(:tag_version) do
|
10
|
+
t.send(:perform_git_push, 'origin master')
|
11
|
+
t.send(:perform_git_push, ' --tags origin master')
|
12
|
+
Bundler.ui.confirm "Pushed git commits and tags."
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/no/version.rb
ADDED
data/lib/no.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require "no/version"
|
2
|
+
|
3
|
+
module NO
|
4
|
+
class NullObject < Object
|
5
|
+
%w(to_a to_c to_f to_h to_i to_r to_s rationalize & ! nil?).each do |method|
|
6
|
+
define_method(method) do |*args|
|
7
|
+
nil.send(method, *args)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def == a
|
12
|
+
if a.is_a?(::NO::NullObject)
|
13
|
+
super
|
14
|
+
else
|
15
|
+
nil.== a
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def ^ a
|
20
|
+
if a.nil? # for "NO::NullObject ^ NO::NullObject returns false"
|
21
|
+
false
|
22
|
+
else
|
23
|
+
nil.^ a
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def | a
|
28
|
+
if a.nil? # for "NO::NullObject | NO::NullObject returns false"
|
29
|
+
false
|
30
|
+
else
|
31
|
+
nil.| a
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def inspect
|
36
|
+
"NO::NullObject"
|
37
|
+
end
|
38
|
+
|
39
|
+
def pretty_inspect
|
40
|
+
"NO::NullObject\n"
|
41
|
+
end
|
42
|
+
|
43
|
+
def method_missing name, *args
|
44
|
+
if nil.respond_to?(name)
|
45
|
+
nil.send(name, *args)
|
46
|
+
else
|
47
|
+
nil
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/no.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'no/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "no"
|
8
|
+
spec.version = NO::VERSION
|
9
|
+
spec.authors = ["Takatoshi Matsumoto"]
|
10
|
+
spec.email = ["toqoz403@gmail.com"]
|
11
|
+
spec.summary = %q{Support creating null object in Ruby.}
|
12
|
+
spec.description = %q{Support creating null object in Ruby. This basically behaves as nil and when a method is not found, returns nil.}
|
13
|
+
spec.homepage = "https://github.com/ToQoz/no"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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.5"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "pry"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
end
|
data/spec/no_spec.rb
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
# http://www.ruby-doc.org/core-2.1.1/NilClass.html
|
4
|
+
describe NO::NullObject do
|
5
|
+
%w(to_a to_c to_f to_h to_i to_r to_s rationalize).each do |method|
|
6
|
+
describe method do
|
7
|
+
subject { described_class.new.send(method) }
|
8
|
+
it { should eq(nil.send(method)) }
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#&' do
|
13
|
+
context 'NO::NullObject & object' do
|
14
|
+
subject { NO::NullObject.new & Object.new }
|
15
|
+
it { should be_false }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe '#^' do
|
20
|
+
context 'NO::NullObject ^ nil' do
|
21
|
+
subject { NO::NullObject.new ^ nil }
|
22
|
+
it { should eq(nil ^ nil) }
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'NO::NullObject ^ false' do
|
26
|
+
subject { NO::NullObject.new ^ false }
|
27
|
+
it { should eq(nil ^ false) }
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'NO::NullObject ^ NO::NullObject' do
|
31
|
+
subject { NO::NullObject.new ^ NO::NullObject.new }
|
32
|
+
it { should eq(nil ^ nil) }
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'NO::NullObject ^ object(that is not nil, false or NO::NullObject)' do
|
36
|
+
subject { NO::NullObject.new ^ Object.new }
|
37
|
+
it { should eq(nil ^ Object.new) }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '#|' do
|
42
|
+
context 'NO::NullObject | nil' do
|
43
|
+
subject { NO::NullObject.new | nil }
|
44
|
+
it { should eq(nil ^ nil) }
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'NO::NullObject | false' do
|
48
|
+
subject { NO::NullObject.new | false }
|
49
|
+
it { should eq(nil ^ false) }
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'NO::NullObject | NO::NullObject' do
|
53
|
+
subject { NO::NullObject.new | NO::NullObject.new }
|
54
|
+
it { should eq(nil ^ nil) }
|
55
|
+
end
|
56
|
+
|
57
|
+
context 'NO::NullObject | object(that is not nil, false or NO::NullObject)' do
|
58
|
+
subject { NO::NullObject.new | Object.new }
|
59
|
+
it { should eq(nil ^ Object.new) }
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe '#==' do
|
64
|
+
context 'NO::NullObject == nil' do
|
65
|
+
subject { described_class.new == nil }
|
66
|
+
it { should be_true }
|
67
|
+
end
|
68
|
+
|
69
|
+
context 'NO::NullObject == NO::NullObject (Same instance)' do
|
70
|
+
subject { a = described_class.new; a == a }
|
71
|
+
it { should be_true }
|
72
|
+
end
|
73
|
+
|
74
|
+
context 'NO::NullObject == NO::NullObject (Different instance)' do
|
75
|
+
# `a == nil is true` and `b == nil` is true, but `a == b` is false
|
76
|
+
# !!!This is strange but useful!!!
|
77
|
+
# Example:
|
78
|
+
# class Annonymous < NO::NullObject
|
79
|
+
# end
|
80
|
+
#
|
81
|
+
# class FakeLogger < NO::NullObject
|
82
|
+
# end
|
83
|
+
#
|
84
|
+
# FakeLogger.new == Annonymous.new # => false
|
85
|
+
#
|
86
|
+
|
87
|
+
subject { described_class.new == described_class.new }
|
88
|
+
it {
|
89
|
+
should be_false
|
90
|
+
|
91
|
+
# Check example's validity {{{
|
92
|
+
class Annonymous < NO::NullObject; end
|
93
|
+
class FakeLogger < NO::NullObject; end
|
94
|
+
expect(FakeLogger.new == Annonymous.new).to be_false
|
95
|
+
# }}}
|
96
|
+
}
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
describe '#!' do
|
101
|
+
subject { !described_class.new }
|
102
|
+
it { should be_true }
|
103
|
+
end
|
104
|
+
|
105
|
+
describe '#nil?' do
|
106
|
+
subject { described_class.new.nil? }
|
107
|
+
it { should be_true }
|
108
|
+
end
|
109
|
+
|
110
|
+
describe 'methods that nil don\'t have' do
|
111
|
+
it 'should return nil' do
|
112
|
+
n = described_class.new
|
113
|
+
|
114
|
+
expect(n.foo).to eq(nil)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
class Annonymous < NO::NullObject
|
119
|
+
def name
|
120
|
+
'annonymous'
|
121
|
+
end
|
122
|
+
|
123
|
+
def age
|
124
|
+
'unknown'
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
it 'can expand by inheritance' do
|
129
|
+
expect(Annonymous.new.name).to eq('annonymous')
|
130
|
+
expect(Annonymous.new.age).to eq('unknown')
|
131
|
+
end
|
132
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: 'no'
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Takatoshi Matsumoto
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Support creating null object in Ruby. This basically behaves as nil and
|
70
|
+
when a method is not found, returns nil.
|
71
|
+
email:
|
72
|
+
- toqoz403@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- lib/no.rb
|
84
|
+
- lib/no/version.rb
|
85
|
+
- no.gemspec
|
86
|
+
- spec/no_spec.rb
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
homepage: https://github.com/ToQoz/no
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.2.2
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Support creating null object in Ruby.
|
112
|
+
test_files:
|
113
|
+
- spec/no_spec.rb
|
114
|
+
- spec/spec_helper.rb
|