rubyx/lib/support/hash_attributes.rb

38 lines
1010 B
Ruby
Raw Normal View History

2014-05-03 21:18:04 +02:00
# Make hash attributes to object attributes
module Support
module HashAttributes
# map any function call to an attribute if possible
def method_missing name , *args , &block
if args.length > 1 or block_given?
puts "NO -#{args.length} BLOCK #{block_given?}"
super
else
2014-05-13 16:06:42 +02:00
sname = name.to_s
2014-05-03 21:18:04 +02:00
if args.length == 1 #must be assignemnt for ir attr= val
2014-05-13 16:06:42 +02:00
if sname.include? "="
2014-05-05 14:59:29 +02:00
#puts "setting :#{name.chop}:#{args[0]}"
2014-05-13 16:06:42 +02:00
return @attributes[sname.chop.to_sym] = args[0]
2014-05-03 21:18:04 +02:00
else
super
end
else
2014-05-05 14:59:29 +02:00
#puts "getting :#{name}:#{@attributes[name.to_sym]}"
2014-05-13 16:06:42 +02:00
return @attributes[sname.to_sym]
2014-05-03 21:18:04 +02:00
end
end
end
end
end
class Binding
#these are defined in 2.1 and thus the definitions should be conditional. TODO
def local_variable_defined? sym
vars = eval("local_variables")
vars.include? sym
end
def local_variable_get sym
eval(sym.to_s)
end
end