module DynamicVars
Public Class Methods
[](key)
click to toggle source
# File lib/dynamic_vars.rb, line 37 def [](key) variables[key] end
[]=(key, value)
click to toggle source
# File lib/dynamic_vars.rb, line 41 def []=(key, value) variables[key] = value end
here!()
click to toggle source
# File lib/dynamic_vars.rb, line 7 def here! Thread.current[:DYNAMIC] = Hash.new { |hash, key| raise NameError, "no such dynamic variable: #{key}" }.update Thread.main[:DYNAMIC] end
let(bindings, &block)
click to toggle source
# File lib/dynamic_vars.rb, line 52 def let(bindings, &block) save = {} bindings.to_hash.collect { |key, value| save[key] = self[key] self[key] = value } begin block.call ensure variables.update save end end
method_missing(name, *args)
click to toggle source
# File lib/dynamic_vars.rb, line 65 def method_missing(name, *args) if match = /=\Z/.match(name.to_s) # setter? raise ArgumentError, "invalid setter call" unless args.size == 1 self[match.pre_match.intern] = args.first else raise ArgumentError, "invalid getter call" unless args.empty? self[name] end end
undefine(*keys)
click to toggle source
# File lib/dynamic_vars.rb, line 45 def undefine(*keys) keys.each { |key| self[key] variables.delete key } end
variable(definition)
click to toggle source
# File lib/dynamic_vars.rb, line 17 def variable(definition) case definition when Symbol if variables.has_key? definition raise NameError, "dynamic variable `#{definition}' already exists" end variables[definition] = nil when Hash definition.each { |key, value| if variables.has_key? key raise NameError, "dynamic variable `#{key}' already exists" end variables[key] = value } else raise ArgumentError, "can't create a new dynamic variable from #{definition.class}" end end
variables()
click to toggle source
# File lib/dynamic_vars.rb, line 13 def variables Thread.current[:DYNAMIC] or here! end