Class: Dryml::ScopedVariables
- Inherits:
-
Object
- Object
- Dryml::ScopedVariables
show all
- Defined in:
- lib/dryml/scoped_variables.rb
Instance Method Summary
collapse
Constructor Details
#initialize(variables = nil) ⇒ ScopedVariables
Returns a new instance of ScopedVariables.
5
6
7
|
# File 'lib/dryml/scoped_variables.rb', line 5
def initialize(variables=nil)
@scopes = variables ? [variables] : []
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
32
33
34
35
36
37
38
|
# File 'lib/dryml/scoped_variables.rb', line 32
def method_missing(name, *args)
if name.to_s =~ /=$/
self[name.to_s[0..-2].to_sym] = args.first
else
self[name]
end
end
|
Instance Method Details
#[](key) ⇒ Object
9
10
11
|
# File 'lib/dryml/scoped_variables.rb', line 9
def [](key)
s = scope_with_key(key) and s[key]
end
|
#[]=(key, val) ⇒ Object
13
14
15
16
|
# File 'lib/dryml/scoped_variables.rb', line 13
def []=(key, val)
s = scope_with_key(key) or raise ArgumentError, "no such scoped variable: #{key}"
s[key] = val
end
|
#new_scope(variables) ⇒ Object
18
19
20
21
22
23
|
# File 'lib/dryml/scoped_variables.rb', line 18
def new_scope(variables)
@scopes << variables.dup
res = yield
@scopes.pop
res
end
|
#scope_with_key(key) ⇒ Object
25
26
27
28
29
30
|
# File 'lib/dryml/scoped_variables.rb', line 25
def scope_with_key(key)
@scopes.reverse_each do |s|
return s if s.has_key?(key)
end
nil
end
|