null_object_loader 0.0.1
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/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +59 -0
- data/Rakefile +1 -0
- data/lib/null_object_loader/version.rb +3 -0
- data/lib/null_object_loader.rb +29 -0
- data/null_object_loader.gemspec +24 -0
- data/spec/load_null_object_spec.rb +39 -0
- data/spec/spec_helper.rb +22 -0
- data/spec/support/null_principal.rb +19 -0
- data/spec/support/principal.rb +11 -0
- data/spec/support/school.rb +10 -0
- metadata +114 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Ivor Paul
|
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,59 @@
|
|
1
|
+
# NullObjectLoader
|
2
|
+
|
3
|
+
This little gem lets you declare what null objects to instantiate if a certain method call returns nil.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'null_object_loader'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install null_object_loader
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
In your class
|
22
|
+
|
23
|
+
include NullObjectLoader
|
24
|
+
|
25
|
+
Then, if you would like to load a null object if a method returns nil:
|
26
|
+
|
27
|
+
load_null_object :method_name_here, :null_class => NullObjectClassHere
|
28
|
+
|
29
|
+
Example:
|
30
|
+
|
31
|
+
class OldSchool
|
32
|
+
attr_accessor :principal
|
33
|
+
def initialize(principal = nil)
|
34
|
+
@principal = principal
|
35
|
+
end
|
36
|
+
end
|
37
|
+
OldSchool.new.principal.name # => NameError: Undefined method 'name' for nil:NilClass
|
38
|
+
|
39
|
+
Now, with NullObjectLoader
|
40
|
+
|
41
|
+
class NullPrincipal
|
42
|
+
def name; "No Principal"; end
|
43
|
+
end
|
44
|
+
|
45
|
+
class NewSchool < OldSchool
|
46
|
+
include NullObjectLoader
|
47
|
+
|
48
|
+
load_null_object :principal, :null_class => NullPrincipal
|
49
|
+
end
|
50
|
+
NewSchool.new.principal.name # => "No Principal"
|
51
|
+
|
52
|
+
|
53
|
+
## Contributing
|
54
|
+
|
55
|
+
1. Fork it
|
56
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
57
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
58
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
59
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require "null_object_loader/version"
|
2
|
+
|
3
|
+
module NullObjectLoader
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.extend(ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
|
11
|
+
def load_null_object(name, opts)
|
12
|
+
null_class = opts[:null_class]
|
13
|
+
load_if = opts[:load_if] || :nil?
|
14
|
+
|
15
|
+
alias_method NullObjectLoader.temp_name_for_method(name), name
|
16
|
+
define_method(name) do
|
17
|
+
original_value = send(NullObjectLoader.temp_name_for_method(name))
|
18
|
+
return original_value if !original_value.send(load_if)
|
19
|
+
null_class.new
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.temp_name_for_method(name)
|
26
|
+
"null_object_loader_original_#{name}".to_sym
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'null_object_loader/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "null_object_loader"
|
8
|
+
spec.version = NullObjectLoader::VERSION
|
9
|
+
spec.authors = ["Ivor Paul"]
|
10
|
+
spec.email = ["ivorpaul@gmail.com"]
|
11
|
+
spec.description = "Lets you specify what NullObject to instantiate instead of returning nil for instance methods."
|
12
|
+
spec.summary = "Loading null objects for instance methods."
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
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.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Null Object loaded if 'load_null_object' is declared" do
|
4
|
+
context "a class without a load_null_object call" do
|
5
|
+
|
6
|
+
it "should raise an exception when calling name on nil" do
|
7
|
+
s = School.new("High School")
|
8
|
+
s.principal.nil?.should be_true
|
9
|
+
expect { s.principal.name }.to raise_error(NameError)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should not raise a name error if the principal is not nil" do
|
13
|
+
p = Principal.new("Jim Kirk", "jim@enterprise.uss","1234567890")
|
14
|
+
s = School.new("High School", p)
|
15
|
+
s.principal.nil?.should be_false
|
16
|
+
expect { s.principal.name }.to_not raise_error(NameError)
|
17
|
+
s.principal.name.should == "Jim Kirk"
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
context "a class that has a load_null_object declaration" do
|
23
|
+
# Create a school class that using this gem.
|
24
|
+
class SchoolWithNullObjectLoader < School
|
25
|
+
include NullObjectLoader
|
26
|
+
|
27
|
+
load_null_object :principal, :null_class => NullPrincipal
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should not raise an exception when calling name on nil" do
|
31
|
+
s = SchoolWithNullObjectLoader.new("High School")
|
32
|
+
s.principal.nil?.should be_true
|
33
|
+
expect { s.principal.name }.to_not raise_error(NameError)
|
34
|
+
s.principal.name.should == NullPrincipal.new.name
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
GEM_ROOT = File.expand_path("../../", __FILE__)
|
8
|
+
|
9
|
+
require 'null_object_loader'
|
10
|
+
Dir[File.join(GEM_ROOT, "spec", "support", "**/*.rb")].each { |f| require f }
|
11
|
+
|
12
|
+
RSpec.configure do |config|
|
13
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
14
|
+
config.run_all_when_everything_filtered = true
|
15
|
+
config.filter_run :focus
|
16
|
+
|
17
|
+
# Run specs in random order to surface order dependencies. If you find an
|
18
|
+
# order dependency and want to debug it, you can fix the order by providing
|
19
|
+
# the seed, which is printed after each run.
|
20
|
+
# --seed 1234
|
21
|
+
config.order = 'random'
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: null_object_loader
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ivor Paul
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-06-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Lets you specify what NullObject to instantiate instead of returning
|
63
|
+
nil for instance methods.
|
64
|
+
email:
|
65
|
+
- ivorpaul@gmail.com
|
66
|
+
executables: []
|
67
|
+
extensions: []
|
68
|
+
extra_rdoc_files: []
|
69
|
+
files:
|
70
|
+
- .gitignore
|
71
|
+
- .rspec
|
72
|
+
- Gemfile
|
73
|
+
- LICENSE.txt
|
74
|
+
- README.md
|
75
|
+
- Rakefile
|
76
|
+
- lib/null_object_loader.rb
|
77
|
+
- lib/null_object_loader/version.rb
|
78
|
+
- null_object_loader.gemspec
|
79
|
+
- spec/load_null_object_spec.rb
|
80
|
+
- spec/spec_helper.rb
|
81
|
+
- spec/support/null_principal.rb
|
82
|
+
- spec/support/principal.rb
|
83
|
+
- spec/support/school.rb
|
84
|
+
homepage: ''
|
85
|
+
licenses:
|
86
|
+
- MIT
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
none: false
|
93
|
+
requirements:
|
94
|
+
- - ! '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
none: false
|
99
|
+
requirements:
|
100
|
+
- - ! '>='
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubyforge_project:
|
105
|
+
rubygems_version: 1.8.23
|
106
|
+
signing_key:
|
107
|
+
specification_version: 3
|
108
|
+
summary: Loading null objects for instance methods.
|
109
|
+
test_files:
|
110
|
+
- spec/load_null_object_spec.rb
|
111
|
+
- spec/spec_helper.rb
|
112
|
+
- spec/support/null_principal.rb
|
113
|
+
- spec/support/principal.rb
|
114
|
+
- spec/support/school.rb
|