Created
October 21, 2010 23:23
-
-
Save hashrocketeer/639597 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class FuzzyHash | |
attr_reader :attrs | |
def initialize(attrs) | |
@attrs = attrs | |
end | |
def blank? | |
attrs.nil? | |
end | |
def nil? | |
attrs.nil? | |
end | |
def to_s | |
"" | |
end | |
def [](key) | |
if blank? | |
return self | |
end | |
value = attrs[key] | |
case value | |
when Hash, HashWithIndifferentAccess | |
FuzzyHash.new(value.dup) | |
when nil | |
FuzzyHash.new(nil) | |
else | |
value | |
end | |
end | |
def method_missing(*args) | |
if blank? | |
self | |
else | |
self[args.first] | |
end | |
end | |
end | |
def fuzzy_attributes | |
FuzzyHash.new(attributes.dup) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment