Class: Peony::Scope

Inherits:
Hash
  • Object
show all
Defined in:
lib/peony/scope.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, parent = nil) {|name, _self| ... } ⇒ Scope

Returns a new instance of Scope.

Yields:

Yield Parameters:

  • _self (Peony::Scope)

    the object that the method was called on



5
6
7
8
9
# File 'lib/peony/scope.rb', line 5

def initialize(name = nil, parent = nil)
  @name = name
  @parent = parent
  yield name, self if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/peony/scope.rb', line 3

def name
  @name
end

Instance Method Details

#[](key) ⇒ Object



13
14
15
# File 'lib/peony/scope.rb', line 13

def [](key)
  super || (@parent && @parent[key])
end

#[]=(key, value) ⇒ Object Also known as: set



23
24
25
26
27
28
29
# 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

#childrenObject



72
73
74
# File 'lib/peony/scope.rb', line 72

def children
  @children ||= {}
end

#evaluate(value) ⇒ Object



67
68
69
70
# 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) ⇒ Boolean Also known as: include?, key?, member?

also change the method key?, include?, member?

Returns:

  • (Boolean)


19
20
21
# File 'lib/peony/scope.rb', line 19

def has_key?(key)
  local?(key) || (!@parent.nil? && @parent.key?(key))
end

#local?Object



11
# File 'lib/peony/scope.rb', line 11

alias_method :local?, :has_key?

#new_scope(name) ⇒ Object



76
77
78
79
80
81
82
83
84
# 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) ⇒ Object



31
32
33
34
35
36
37
# 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) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/peony/scope.rb', line 39

def respond_to_missing?(method, _ = true)
  self.include?(method) || method.to_s =~ /[a-z]\w*[?=!]?$/
end