nullobject 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.
- data/.travis.yml +2 -0
- data/README.md +52 -6
- data/lib/nullobject/version.rb +1 -1
- data/lib/nullobject.rb +7 -23
- data/spec/lib/nullobject_spec.rb +29 -43
- metadata +10 -10
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,58 @@
|
|
1
|
-
# NullObject
|
2
|
-
|
1
|
+
# NullObject
|
2
|
+
#### Null Object Design Pattern Ruby implementation
|
3
|
+
[](http://travis-ci.org/martinciu/nullobject)
|
4
|
+
## Intent
|
3
5
|
|
4
6
|
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
7
|
|
8
|
+
More about Null Object Pattern you can find here:
|
9
|
+
[http://en.wikipedia.org/wiki/Null_Object_pattern](http://en.wikipedia.org/wiki/Null_Object_pattern)
|
10
|
+
[http://sourcemaking.com/design_patterns/null_object](http://sourcemaking.com/design_patterns/null_object)
|
11
|
+
[http://avdi.org/devblog/2011/05/30/null-objects-and-falsiness](http://avdi.org/devblog/2011/05/30/null-objects-and-falsiness/)
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
# examples from http://avdi.org/devblog/2011/05/30/null-objects-and-falsiness/
|
16
|
+
|
17
|
+
Null::Object.instance.foobar.baz.buz
|
18
|
+
|
19
|
+
def Maybe(value)
|
20
|
+
case value
|
21
|
+
when nil then Null::Object.instance
|
22
|
+
else value
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def slug(title)
|
27
|
+
Maybe(title).strip.downcase.tr('^[0-9a-z]', '-')
|
28
|
+
end
|
29
|
+
|
30
|
+
h = {}
|
31
|
+
|
32
|
+
slug(h[:missing_key]) # => ""
|
33
|
+
|
34
|
+
foo = Null::Object.instance
|
35
|
+
|
36
|
+
foo.to_s # => ""
|
37
|
+
foo.to_a # => []
|
38
|
+
foo.to_i # => 0
|
39
|
+
foo.to_f # => 0.0
|
40
|
+
foo.nil? # => true
|
41
|
+
|
42
|
+
# Custom Null Object
|
43
|
+
|
44
|
+
class FooNullObject
|
45
|
+
include Null
|
46
|
+
|
47
|
+
def to_bar
|
48
|
+
"nothing here"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
foo = FooNullObject.instance
|
53
|
+
foo.to_bar # => "nothing here"
|
54
|
+
foo.to_s # => ""
|
55
|
+
|
6
56
|
## Setup
|
7
57
|
|
8
58
|
If you are using bundler add nullobject to your Gemfile:
|
@@ -21,10 +71,6 @@ and require it in your project:
|
|
21
71
|
|
22
72
|
require 'nullobject'
|
23
73
|
|
24
|
-
## Usage
|
25
|
-
|
26
|
-
[TODO]
|
27
|
-
|
28
74
|
## Development
|
29
75
|
|
30
76
|
Source hosted at [GitHub](http://github.com/martinciu/nullobject).
|
data/lib/nullobject/version.rb
CHANGED
data/lib/nullobject.rb
CHANGED
@@ -1,39 +1,23 @@
|
|
1
1
|
require "singleton"
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
value.nil? ? instance : value
|
3
|
+
module Null
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.send(:include, Singleton)
|
8
7
|
end
|
9
8
|
|
10
9
|
def to_a; []; end
|
11
|
-
alias :to_ary :to_a
|
12
10
|
def to_s; ""; end
|
13
|
-
alias :to_str :to_s
|
14
11
|
def to_f; 0.0; end
|
15
12
|
def to_i; 0; end
|
16
13
|
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
14
|
|
24
15
|
def method_missing(*args, &block)
|
25
16
|
self
|
26
17
|
end
|
27
18
|
|
28
|
-
|
29
|
-
|
30
|
-
|
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
|
19
|
+
class Object
|
20
|
+
include Null
|
21
|
+
end
|
38
22
|
|
39
23
|
end
|
data/spec/lib/nullobject_spec.rb
CHANGED
@@ -1,20 +1,16 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe "
|
3
|
+
describe "Null::Object" do
|
4
4
|
|
5
5
|
describe "instance methods" do
|
6
6
|
before do
|
7
|
-
@it =
|
7
|
+
@it = Null::Object.instance
|
8
8
|
end
|
9
9
|
|
10
10
|
it "#to_s return empty string" do
|
11
11
|
@it.to_s.must_equal ""
|
12
12
|
end
|
13
13
|
|
14
|
-
it "#to_str return empty string" do
|
15
|
-
@it.to_str.must_equal ""
|
16
|
-
end
|
17
|
-
|
18
14
|
it "#to_a return empty array" do
|
19
15
|
@it.to_a.must_equal []
|
20
16
|
end
|
@@ -30,55 +26,45 @@ describe "NullObject" do
|
|
30
26
|
it "#nil? return true" do
|
31
27
|
@it.nil?.must_equal true
|
32
28
|
end
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
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
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "custom null" do
|
32
|
+
class FooNullObject
|
33
|
+
include Null
|
49
34
|
|
50
|
-
|
51
|
-
|
52
|
-
([1, 3, "foo"] + @it).must_equal [1, 3, "foo"]
|
35
|
+
def to_bar
|
36
|
+
"nothing here"
|
53
37
|
end
|
54
38
|
end
|
55
39
|
|
56
|
-
|
57
|
-
it
|
58
|
-
|
59
|
-
end
|
40
|
+
before do
|
41
|
+
@it = FooNullObject.instance
|
42
|
+
end
|
60
43
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
end
|
44
|
+
it "#to_s return empty string" do
|
45
|
+
@it.to_s.must_equal ""
|
46
|
+
end
|
65
47
|
|
66
|
-
|
67
|
-
|
68
|
-
end
|
48
|
+
it "#to_a return empty array" do
|
49
|
+
@it.to_a.must_equal []
|
69
50
|
end
|
70
51
|
|
71
|
-
|
52
|
+
it "#to_f return 0.0" do
|
53
|
+
@it.to_f.must_equal 0.0
|
54
|
+
end
|
72
55
|
|
73
|
-
|
74
|
-
|
75
|
-
NullObject.maybe(nil).must_be_kind_of NullObject
|
56
|
+
it "#to_i return 0" do
|
57
|
+
@it.to_i.must_equal 0
|
76
58
|
end
|
77
59
|
|
78
|
-
it "
|
79
|
-
|
80
|
-
NullObject.maybe(val).must_equal val
|
60
|
+
it "#nil? return true" do
|
61
|
+
@it.nil?.must_equal true
|
81
62
|
end
|
63
|
+
|
64
|
+
it "#to_bar returns 'nothing_here'" do
|
65
|
+
@it.to_bar.must_equal "nothing here"
|
66
|
+
end
|
67
|
+
|
82
68
|
end
|
83
69
|
|
84
70
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nullobject
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-11-
|
12
|
+
date: 2011-11-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement: &
|
16
|
+
requirement: &70328215374680 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 0.9.2
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70328215374680
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: minitest
|
27
|
-
requirement: &
|
27
|
+
requirement: &70328215372580 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 2.7.0
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70328215372580
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: turn
|
38
|
-
requirement: &
|
38
|
+
requirement: &70328215370540 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: 0.8.3
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70328215370540
|
47
47
|
description: Null Object Design Pattern Ruby implementation
|
48
48
|
email:
|
49
49
|
- marcin.ciunelis@gmail.com
|
@@ -76,7 +76,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
76
76
|
version: '0'
|
77
77
|
segments:
|
78
78
|
- 0
|
79
|
-
hash:
|
79
|
+
hash: -3175962028283363154
|
80
80
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
81
|
none: false
|
82
82
|
requirements:
|
@@ -85,7 +85,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
85
85
|
version: '0'
|
86
86
|
segments:
|
87
87
|
- 0
|
88
|
-
hash:
|
88
|
+
hash: -3175962028283363154
|
89
89
|
requirements: []
|
90
90
|
rubyforge_project:
|
91
91
|
rubygems_version: 1.8.10
|