Class: Temple::Mixins::CompiledDispatcher::DispatchNode Private

Inherits:
Hash
  • Object
show all
Defined in:
lib/temple/mixins/dispatcher.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

API:

  • private

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDispatchNode

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of DispatchNode.

API:

  • private



82
83
84
85
# File 'lib/temple/mixins/dispatcher.rb', line 82

def initialize
  super { |hsh,key| hsh[key] = DispatchNode.new }
  @method = nil
end

Instance Attribute Details

#methodObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



80
81
82
# File 'lib/temple/mixins/dispatcher.rb', line 80

def method
  @method
end

Instance Method Details

#compile(level = 0, call_parent = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/temple/mixins/dispatcher.rb', line 87

def compile(level = 0, call_parent = nil)
  call_method = method ? (level == 0 ? "#{method}(*exp)" :
                          "#{method}(*exp[#{level}..-1])") : call_parent
  if empty?
    raise 'Invalid dispatcher node' unless method
    call_method
  else
    code = String.new
    code << "case(exp[#{level}])\n"
    each do |key, child|
      code << "when #{key.inspect}\n  " <<
        child.compile(level + 1, call_method).gsub("\n".freeze, "\n  ".freeze) << "\n".freeze
    end
    code << "else\n  " << (call_method || 'exp') << "\nend"
  end
end