lib/sugarcube-anonymous/anonymous.rb in sugarcube-1.4.3 vs lib/sugarcube-anonymous/anonymous.rb in sugarcube-1.4.4
- old
+ new
@@ -9,26 +9,42 @@
#
# @example
# obj = {
# 'first_name': 'Katsuyoshi', # strings
# company: 'Ito Soft Design', # and symbols are supported
+ # great: true,
# }.to_object
#
# obj.first_name
# # => 'Katsuyoshi'
# obj.company
# # => 'Ito Soft Design'
+ # # adding a '?' will return true/false
+ # obj.great?
+ # # => true
+ # obj.great
+ # # => true
+ #
# obj.bla # => raises NoMethodError
# obj.bla = 'bla' # => raises NoMethodError
class Anonymous < Hash
def method_missing(symbol, *args)
if args.size == 0
+ if symbol.to_s[-1] == '?'
+ symbol = symbol.to_s[0...-1].to_sym
+ return_bool = true
+ else
+ return_bool = false
+ end
key = symbol
key = symbol.to_s unless self.include? key
if self.include?(key)
self[key] = self[key].to_object
+ if return_bool
+ return !! self[key]
+ end
return self[key]
end
elsif args.size == 1 && /(.*)=$/ =~ symbol.to_s
key = $1.to_sym
key = key.to_s unless self.include? key
@@ -43,10 +59,10 @@
self
end
# replace enumerable methods
alias :anonymous_each :each
-
+
def each
anonymous_each {|k,v| yield k, v.to_object}
end
end