lib/loquor/object_hash.rb in loquor-0.5.4 vs lib/loquor/object_hash.rb in loquor-0.6.0
- old
+ new
@@ -1,12 +1,13 @@
module Loquor
class ObjectHashKeyMissingError < LoquorError
end
class ObjectHash
- def initialize(hash)
+ def initialize(hash, options = {})
@hash = hash
+ @strict = options[:strict]
end
def ==(other)
if other.is_a?(ObjectHash)
@hash == other.get_instance_variable(:@hash)
@@ -18,17 +19,23 @@
end
def [](key)
fetch_indescriminately(key)
rescue ObjectHashKeyMissingError
- nil
+ if @strict
+ raise $!
+ else
+ nil
+ end
end
def method_missing(name, *args)
- fetch_indescriminately(name, *args)
- rescue ObjectHashKeyMissingError
- @hash.send(name, *args)
+ if name[-1] == "="
+ @hash.send(name, *args)
+ else
+ self[name]
+ end
end
private
def fetch_indescriminately(name, *args)
@@ -37,10 +44,10 @@
elsif @hash.has_key?(name.to_s)
@hash[name.to_s]
elsif @hash.has_key?(name.to_s.to_sym)
@hash[name.to_s.to_sym]
else
- raise ObjectHashKeyMissingError.new
+ raise ObjectHashKeyMissingError.new(name)
end
end
end
end