Class: Yew::Tree

Inherits:
BasicObject
Defined in:
lib/yew.rb

Overview

The object tree. Allows root-to-leave navigation in the form of ordinary method calls.

Instance Method Summary collapse

Constructor Details

#initialize(env, root = nil) ⇒ Tree

Returns a new instance of Tree.

Parameters:

  • env (Hash)
  • root (defaults to: nil)

    Tree’s root. Used for notifying a not found entry at an exact path point.



23
24
25
26
# File 'lib/yew.rb', line 23

def initialize(env, root = nil)
  @env = env
  @root = root
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *attrs, &block) ⇒ Object



47
48
49
# File 'lib/yew.rb', line 47

def method_missing(m, *attrs, &block)
  Utils.fetch(m, @env, @root)
end

Instance Method Details

#[](*key) ⇒ Yew::Tree, ...

Fetches a branch or leaf.

If no key is received, returns the underlying Hash.

Parameters:

  • key

    Node’s name.

Returns:

  • (Yew::Tree)

    When the request node is still a tree.

  • (Hash)

    Underlying Hash.

  • (Object)

    When the requested node is a leaf.



37
38
39
40
41
42
43
44
45
# File 'lib/yew.rb', line 37

def [](*key)
  if key.any?
    key = key.first
    Utils.fetch(key, @env, @root)

  else
    @env
  end
end

#inspectObject



51
52
53
54
55
56
57
# File 'lib/yew.rb', line 51

def inspect
  if $YEW_DEBUG
    "<Yew::Env:#{__id__} -> #@env>"
  else
    super
  end
end

#to_sObject



59
60
61
62
63
64
65
# File 'lib/yew.rb', line 59

def to_s
  if $YEW_DEBUG
    "Env -> #@env"
  else
    super
  end
end