no 0.0.1 → 0.0.2
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.
- checksums.yaml +4 -4
- data/.travis.yml +13 -0
- data/README.md +11 -10
- data/Rakefile +5 -0
- data/lib/no.rb +4 -10
- data/lib/no/version.rb +1 -1
- data/no.gemspec +2 -2
- data/spec/no_spec.rb +18 -2
- metadata +6 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 21389a70261e82117063c2fef57de15b543fc06b
|
4
|
+
data.tar.gz: 594f608fedb3221758891b40b2cd757b2e85bdc4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6214b5f1a4075d7626177ab85ce75b3959fc7c3b8dd7d8589fcdf82b442fd49eb7828c44072aa4eb8cd21d833673ff42484fb8b2883314e20bf3290236ebd6c8
|
7
|
+
data.tar.gz: ebafa2d7aac1e74d84498d01bc7ac43ec3f1caf53df7769ead7424ebc6e2d08bf9498fade5171420c5c89886dc726c31e2bc1550d39c7454b2c5e3ab02dfd64d
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# NO
|
2
2
|
|
3
|
-
|
3
|
+
[](https://travis-ci.org/ToQoz/no)
|
4
|
+
|
5
|
+
Supporter for creating null object in Ruby. NO::NullObject basically bahaves as nil.
|
4
6
|
|
5
7
|
1. NO::NullObject basically delegates to nil.
|
6
8
|
2. if nil doesn't respond to a method, NO::NullObject returns nil.
|
@@ -74,24 +76,23 @@ UserRepository.find(3).name #=> 'annonymous'
|
|
74
76
|
## Spec
|
75
77
|
|
76
78
|
```
|
77
|
-
|
78
79
|
NO::NullObject
|
79
80
|
can expand by inheritance
|
80
|
-
to_a
|
81
|
+
#to_a
|
81
82
|
should eq []
|
82
|
-
to_c
|
83
|
+
#to_c
|
83
84
|
should eq (0+0i)
|
84
|
-
to_f
|
85
|
+
#to_f
|
85
86
|
should eq 0.0
|
86
|
-
to_h
|
87
|
+
#to_h
|
87
88
|
should eq {}
|
88
|
-
to_i
|
89
|
+
#to_i
|
89
90
|
should eq 0
|
90
|
-
to_r
|
91
|
+
#to_r
|
91
92
|
should eq (0/1)
|
92
|
-
to_s
|
93
|
+
#to_s
|
93
94
|
should eq ""
|
94
|
-
rationalize
|
95
|
+
#rationalize
|
95
96
|
should eq (0/1)
|
96
97
|
#&
|
97
98
|
NO::NullObject & object
|
data/Rakefile
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
2
|
require "bundler/gem_helper"
|
3
3
|
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
RSpec::Core::RakeTask.new(:spec)
|
6
|
+
|
4
7
|
t = Bundler::GemHelper.new
|
5
8
|
|
6
9
|
desc "Create tag #{t.send(:version_tag)}"
|
@@ -13,3 +16,5 @@ task :tag do
|
|
13
16
|
end
|
14
17
|
end
|
15
18
|
end
|
19
|
+
|
20
|
+
task :default => :spec
|
data/lib/no.rb
CHANGED
@@ -3,6 +3,8 @@ require "no/version"
|
|
3
3
|
module NO
|
4
4
|
class NullObject < Object
|
5
5
|
%w(to_a to_c to_f to_h to_i to_r to_s rationalize & ! nil?).each do |method|
|
6
|
+
next unless nil.respond_to?(method)
|
7
|
+
|
6
8
|
define_method(method) do |*args|
|
7
9
|
nil.send(method, *args)
|
8
10
|
end
|
@@ -17,7 +19,7 @@ module NO
|
|
17
19
|
end
|
18
20
|
|
19
21
|
def ^ a
|
20
|
-
if a.
|
22
|
+
if a.is_a?(::NO::NullObject) # for "NO::NullObject ^ NO::NullObject returns false"
|
21
23
|
false
|
22
24
|
else
|
23
25
|
nil.^ a
|
@@ -25,21 +27,13 @@ module NO
|
|
25
27
|
end
|
26
28
|
|
27
29
|
def | a
|
28
|
-
if a.
|
30
|
+
if a.is_a?(::NO::NullObject) # for "NO::NullObject | NO::NullObject returns false"
|
29
31
|
false
|
30
32
|
else
|
31
33
|
nil.| a
|
32
34
|
end
|
33
35
|
end
|
34
36
|
|
35
|
-
def inspect
|
36
|
-
"NO::NullObject"
|
37
|
-
end
|
38
|
-
|
39
|
-
def pretty_inspect
|
40
|
-
"NO::NullObject\n"
|
41
|
-
end
|
42
|
-
|
43
37
|
def method_missing name, *args
|
44
38
|
if nil.respond_to?(name)
|
45
39
|
nil.send(name, *args)
|
data/lib/no/version.rb
CHANGED
data/no.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = NO::VERSION
|
9
9
|
spec.authors = ["Takatoshi Matsumoto"]
|
10
10
|
spec.email = ["toqoz403@gmail.com"]
|
11
|
-
spec.summary = %q{
|
12
|
-
spec.description = %q{
|
11
|
+
spec.summary = %q{Supporter for creating null object in Ruby.}
|
12
|
+
spec.description = %q{Supporter for creating null object in Ruby. This basically behaves as nil and when a method is not found, returns nil.}
|
13
13
|
spec.homepage = "https://github.com/ToQoz/no"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
data/spec/no_spec.rb
CHANGED
@@ -2,13 +2,29 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
# http://www.ruby-doc.org/core-2.1.1/NilClass.html
|
4
4
|
describe NO::NullObject do
|
5
|
-
%w(to_a to_c to_f
|
6
|
-
describe method do
|
5
|
+
%w(to_a to_c to_f to_i to_r to_s rationalize).each do |method|
|
6
|
+
describe "##{method}" do
|
7
7
|
subject { described_class.new.send(method) }
|
8
8
|
it { should eq(nil.send(method)) }
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
|
+
describe '#to_h' do
|
13
|
+
context 'nil has to_h' do
|
14
|
+
if nil.respond_to?(:to_h)
|
15
|
+
subject { described_class.new.to_h }
|
16
|
+
it { should eq(nil.to_h) }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'nil doesn\'t have to_h' do
|
21
|
+
unless nil.respond_to?(:to_h)
|
22
|
+
subject { described_class.new.to_h }
|
23
|
+
it { should eq(nil) }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
12
28
|
describe '#&' do
|
13
29
|
context 'NO::NullObject & object' do
|
14
30
|
subject { NO::NullObject.new & Object.new }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: 'no'
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Takatoshi Matsumoto
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,8 +66,8 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
-
description:
|
70
|
-
when a method is not found, returns nil.
|
69
|
+
description: Supporter for creating null object in Ruby. This basically behaves as
|
70
|
+
nil and when a method is not found, returns nil.
|
71
71
|
email:
|
72
72
|
- toqoz403@gmail.com
|
73
73
|
executables: []
|
@@ -76,6 +76,7 @@ extra_rdoc_files: []
|
|
76
76
|
files:
|
77
77
|
- ".gitignore"
|
78
78
|
- ".rspec"
|
79
|
+
- ".travis.yml"
|
79
80
|
- Gemfile
|
80
81
|
- LICENSE.txt
|
81
82
|
- README.md
|
@@ -108,7 +109,7 @@ rubyforge_project:
|
|
108
109
|
rubygems_version: 2.2.2
|
109
110
|
signing_key:
|
110
111
|
specification_version: 4
|
111
|
-
summary:
|
112
|
+
summary: Supporter for creating null object in Ruby.
|
112
113
|
test_files:
|
113
114
|
- spec/no_spec.rb
|
114
115
|
- spec/spec_helper.rb
|