Last active
August 29, 2015 14:24
-
-
Save npassaro/acd5ae47f72d3861246f to your computer and use it in GitHub Desktop.
Decorator that kind of implements Null Object pattern
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 Utils | |
class StringDecorator | |
def initialize(string) | |
@string = string | |
end | |
def =~(regex) | |
self.to_s =~ regex | |
end | |
def nil? | |
@string.nil? | |
end | |
def to_s | |
@string | |
end | |
def string | |
@string | |
end | |
def method_missing(name,*args,&block) | |
if self.nil? | |
self | |
elsif self.string.respond_to? name | |
@string = @string.send(name,*args) | |
self | |
else | |
super | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment