Last active
August 29, 2015 14:05
-
-
Save grekko/879c10aee3f4fa5f46bd to your computer and use it in GitHub Desktop.
Null Object for Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Gem::Specification.new do |s| | |
s.name = 'null' | |
s.version = '0.0.1' | |
s.platform = Gem::Platform::RUBY | |
s.author = 'Gregory Igelmund' | |
s.email = '[email protected]' | |
s.summary = 'Dynamic Null Object' | |
s.description = 'Responds on all methods with itself. Credits to @flori. NullObject is taken from Tins: https://github.com/flori/tins' | |
s.files = ['null.rb'] | |
# TBD => s.test_file = '' | |
s.require_path = '.' | |
s.add_development_dependency('rspec', ["~> 2.0"]) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Microgem | |
# Implementation of the null object pattern in Ruby. | |
class NullObject | |
def method_missing(*) | |
self | |
end | |
def const_missing(*) | |
self | |
end | |
def to_s | |
'' | |
end | |
def to_str | |
nil | |
end | |
def to_f | |
0.0 | |
end | |
def to_i | |
0 | |
end | |
def to_int | |
nil | |
end | |
def to_a | |
[] | |
end | |
def to_ary | |
nil | |
end | |
def inspect | |
'NULL' | |
end | |
def nil? | |
true | |
end | |
def blank? | |
true | |
end | |
def as_json(*) | |
end | |
def to_json(*) | |
'null' | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment