Module: Dynamic::ClassMethods
- Included in:
- Dynamic
- Defined in:
- lib/goat/dynamic.rb
Overview
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
80
81
82
83
84
85
86
87
88
|
# File 'lib/goat/dynamic.rb', line 80
def method_missing(name, *args)
if match = /=\Z/.match(name.to_s) 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
|
Instance Method Details
51
52
53
|
# File 'lib/goat/dynamic.rb', line 51
def [](key)
variables[key]
end
|
#[]=(key, value) ⇒ Object
55
56
57
|
# File 'lib/goat/dynamic.rb', line 55
def []=(key, value)
variables[key] = value
end
|
17
18
19
20
21
|
# File 'lib/goat/dynamic.rb', line 17
def here!
Thread.current[:DYNAMIC] = Hash.new { |hash, key|
raise NameError, "no such dynamic variable: #{key}"
}.update Thread.main[:DYNAMIC]
end
|
#let(bindings, &block) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/goat/dynamic.rb', line 66
def let(bindings, &block)
save = {}
save_keys = variables.keys
save_keys.each {|k, v| save[k] = self[k]}
bindings.to_hash.collect { |key, value|
self.variable(key)
self[key] = value
}
block.call
ensure
variables.update save
undefine(*(variables.keys - save_keys))
end
|
#undefine(*keys) ⇒ Object
59
60
61
62
63
64
|
# File 'lib/goat/dynamic.rb', line 59
def undefine(*keys)
keys.each { |key|
self[key]
variables.delete key
}
end
|
#variable(definition) ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/goat/dynamic.rb', line 31
def variable(definition)
case definition
when Symbol
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
|
#variable?(v) ⇒ Boolean
23
24
25
|
# File 'lib/goat/dynamic.rb', line 23
def variable?(v)
variables.include?(v)
end
|
#variables ⇒ Object
27
28
29
|
# File 'lib/goat/dynamic.rb', line 27
def variables
Thread.current[:DYNAMIC] or here!
end
|