blank_slate 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.simplecov +0 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +67 -0
- data/Rakefile +10 -0
- data/blank_slate.gemspec +23 -0
- data/lib/blank_slate/version.rb +3 -0
- data/lib/blank_slate.rb +11 -0
- data/test/lib/blank_slate_test.rb +49 -0
- metadata +64 -0
data/.gitignore
ADDED
data/.simplecov
ADDED
File without changes
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 'Jim Gay'
|
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,67 @@
|
|
1
|
+
# blank_slate
|
2
|
+
|
3
|
+
BlankSlate is useful for implementing the Null Object pattern.
|
4
|
+
|
5
|
+
This gem allows you to create null objects without resorting to
|
6
|
+
method_missing to catch every message passed. Using method_missing
|
7
|
+
means that your objects will respond to any method regardless of
|
8
|
+
the behavior you designed in your other related classes.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
gem 'blank_slate'
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
Here's an example of how you might use it.
|
19
|
+
|
20
|
+
A common example of a null object is to use `method_missing` to return the object itself. That allows you to continually call missing methods on the null object. The blank slate approach defines methods based upon another class. This keeps the interface to both objects (valid object and null object) the same.
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
|
24
|
+
# Setup some sort of class... this can be anything
|
25
|
+
|
26
|
+
require 'delegate'
|
27
|
+
class UserPresenter < SimpleDelegator
|
28
|
+
def recent_searches
|
29
|
+
last_queries.collect{|query| %{<a href="http://site.co.uk/q?=#{query}">#{query}</a>}}.join(',')
|
30
|
+
end
|
31
|
+
|
32
|
+
def welcome
|
33
|
+
"Welcome! We're so glad to have you!"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
require 'blank_slate'
|
38
|
+
include BlankSlate
|
39
|
+
|
40
|
+
# Create a blank slate but provide a specific value for a certain method
|
41
|
+
|
42
|
+
GuestPresenter = BlankSlate(UserPresenter) do
|
43
|
+
def welcome
|
44
|
+
"Sign-in or sign-up, cowboy!"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def Present(user)
|
49
|
+
user.authenticated? ? UserPresenter.new(user) : GuestPresenter.new(user)
|
50
|
+
end
|
51
|
+
|
52
|
+
presenter = Present(current_user)
|
53
|
+
|
54
|
+
presenter.welcome
|
55
|
+
presenter.recent_searches # unauthenticated outputs nothing
|
56
|
+
presenter.bad_method # raises an error in either case
|
57
|
+
```
|
58
|
+
|
59
|
+
In the example above the call to `bad_method` would have continued with a null object (using method_missing) but failed for the good object. With BlankSlate, we get the behavior we want from both our regular class and the null stand-in.
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
1. Fork it
|
64
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
65
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
66
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
67
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/blank_slate.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'blank_slate/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "blank_slate"
|
8
|
+
gem.version = BlankSlate::VERSION
|
9
|
+
gem.authors = ["Jim Gay"]
|
10
|
+
gem.email = ["jim@saturnflyer.com"]
|
11
|
+
gem.description = %q{
|
12
|
+
Create null classes based upon other classes.
|
13
|
+
This allows you to create null objects without resorting to
|
14
|
+
method_missing to catch every message passed regardless of
|
15
|
+
the behavior you designed in your other classes.}
|
16
|
+
gem.summary = %q{Impliment a null object without resorting to method_missing.}
|
17
|
+
gem.homepage = "http://github.com/saturnflyer/blank_slate"
|
18
|
+
|
19
|
+
gem.files = `git ls-files`.split($/)
|
20
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
21
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
22
|
+
gem.require_paths = ["lib"]
|
23
|
+
end
|
data/lib/blank_slate.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'minitest/spec'
|
3
|
+
require 'blank_slate'
|
4
|
+
|
5
|
+
include BlankSlate
|
6
|
+
|
7
|
+
module BlankSlate
|
8
|
+
class TesterClass
|
9
|
+
def one
|
10
|
+
"one"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "BlankSlate" do
|
16
|
+
def blank_slate(&block)
|
17
|
+
BlankSlate(BlankSlate::TesterClass, &block)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "returns a class with all instance_methods of the given class" do
|
21
|
+
assert_equal BlankSlate::TesterClass.instance_methods.sort, blank_slate.instance_methods.sort
|
22
|
+
end
|
23
|
+
|
24
|
+
it "sets the values of instance methods to nil" do
|
25
|
+
null_object = blank_slate.new
|
26
|
+
|
27
|
+
null_object.one.must_be_nil
|
28
|
+
end
|
29
|
+
|
30
|
+
it "accepts a block to define methods on the null class" do
|
31
|
+
null_object = blank_slate do
|
32
|
+
def passed_block_method
|
33
|
+
'this is from the method defined in the block!'
|
34
|
+
end
|
35
|
+
end.new
|
36
|
+
|
37
|
+
null_object.passed_block_method.must_equal 'this is from the method defined in the block!'
|
38
|
+
end
|
39
|
+
|
40
|
+
it "allows the block to override method definitions from the given class" do
|
41
|
+
null_object = blank_slate do
|
42
|
+
def one
|
43
|
+
'not the same one'
|
44
|
+
end
|
45
|
+
end.new
|
46
|
+
|
47
|
+
null_object.one.must_equal 'not the same one'
|
48
|
+
end
|
49
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: blank_slate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jim Gay
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-24 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: ! "\n Create null classes based upon other classes.\n This allows
|
15
|
+
you to create null objects without resorting to\n method_missing to catch every
|
16
|
+
message passed regardless of\n the behavior you designed in your other classes."
|
17
|
+
email:
|
18
|
+
- jim@saturnflyer.com
|
19
|
+
executables: []
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- .gitignore
|
24
|
+
- .simplecov
|
25
|
+
- Gemfile
|
26
|
+
- LICENSE.txt
|
27
|
+
- README.md
|
28
|
+
- Rakefile
|
29
|
+
- blank_slate.gemspec
|
30
|
+
- lib/blank_slate.rb
|
31
|
+
- lib/blank_slate/version.rb
|
32
|
+
- test/lib/blank_slate_test.rb
|
33
|
+
homepage: http://github.com/saturnflyer/blank_slate
|
34
|
+
licenses: []
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ! '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
hash: 3761206396574154736
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
hash: 3761206396574154736
|
57
|
+
requirements: []
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 1.8.25
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: Impliment a null object without resorting to method_missing.
|
63
|
+
test_files:
|
64
|
+
- test/lib/blank_slate_test.rb
|