class Peony::Scope
Attributes
name[R]
Public Class Methods
new(name = nil, parent = nil) { |name, self| ... }
click to toggle source
# File lib/peony/scope.rb, line 5 def initialize(name = nil, parent = nil) @name = name @parent = parent yield name, self if block_given? end
Public Instance Methods
[](key)
click to toggle source
Calls superclass method
# File lib/peony/scope.rb, line 13 def [](key) super || (@parent && @parent[key]) end
[]=(key, value)
click to toggle source
# File lib/peony/scope.rb, line 23 def []=(key, value) if !local?(key) && @parent && @parent.has_key?(key) @parent.set key, value else store(key, value) end end
Also aliased as: set
children()
click to toggle source
# File lib/peony/scope.rb, line 72 def children @children ||= {} end
evaluate(value) { |: ret| ... }
click to toggle source
# File lib/peony/scope.rb, line 67 def evaluate(value) ret = value.is_a?(Proc) ? value.call : value ret.nil? && block_given? ? yield : ret end
has_key?(key)
click to toggle source
also change the method key?, include?, member?
# File lib/peony/scope.rb, line 19 def has_key?(key) local?(key) || (!@parent.nil? && @parent.key?(key)) end
method_missing(method, *args, &block)
click to toggle source
# File lib/peony/scope.rb, line 51 def method_missing(method, *args, &block) return evaluate(self.[](method), &block) if has_key?(method) match = method.to_s.match(/(.*?)([?=!]?)$/) case match[2] when '=' #self[match[1].to_sym] = args.first || block local(match[1].to_sym, args.first || block) when '?' !!self[match[1].to_sym] when '!' evaluate(fetch(match[1].to_sym), &block) #just fetch local key else evaluate(self[match[1]], &block) #support string key end end
new_scope(name)
click to toggle source
# File lib/peony/scope.rb, line 76 def new_scope(name) clazz = self.class self.send(name) || Scope.new(name, self) do|_name, _scope| children[_name] = _scope clazz.send :define_method, _name do _scope end end end
remove(key, recursively = false)
click to toggle source
# File lib/peony/scope.rb, line 31 def remove(key, recursively = false) delete(key) if recursively && @parent @parent.remove(key, recursively) end self end
respond_to_missing?(method, _ = true)
click to toggle source
# File lib/peony/scope.rb, line 39 def respond_to_missing?(method, _ = true) self.include?(method) || method.to_s =~ /[a-z]\w*[?=!]?$/ end