blank_slate 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.simplecov ADDED
File without changes
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem 'rake'
7
+ gem 'simplecov'
8
+ end
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
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "simplecov"
3
+
4
+ desc "Test and run coverage"
5
+ task :test do
6
+ SimpleCov.start
7
+ load 'test/lib/blank_slate_test.rb'
8
+ end
9
+
10
+ task :default => :test
@@ -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
@@ -0,0 +1,3 @@
1
+ module BlankSlate
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,11 @@
1
+ module BlankSlate
2
+ def BlankSlate(klass, &block)
3
+ blank_slate = Class.new(&block)
4
+ blank_slate.class_eval do
5
+ klass.instance_methods(false).each do |meth|
6
+ define_method(meth){ nil } unless method_defined?(meth)
7
+ end
8
+ end
9
+ blank_slate
10
+ end
11
+ end
@@ -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