Class: Bade::Optimizer
Instance Method Summary collapse
-
#initialize(root_node) ⇒ Optimizer
constructor
A new instance of Optimizer.
- #iterate(node) ⇒ Object
- #optimize ⇒ Bade::Node
- #optimize_static_texts ⇒ Object
- #traverse(node) {|node| ... } ⇒ Object
Constructor Details
#initialize(root_node) ⇒ Optimizer
Returns a new instance of Optimizer.
8 9 10 11 12 |
# File 'lib/bade/optimizer.rb', line 8 def initialize(root_node) @root_node = root_node @new_root = Marshal.load(Marshal.dump(root_node)) end |
Instance Method Details
#iterate(node) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/bade/optimizer.rb', line 45 def iterate(node) previous = nil node.children.delete_if do |subnode| returned = yield subnode, previous, node previous = subnode unless returned returned end end |
#optimize ⇒ Bade::Node
16 17 18 19 20 |
# File 'lib/bade/optimizer.rb', line 16 def optimize optimize_static_texts @root_node end |
#optimize_static_texts ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/bade/optimizer.rb', line 22 def optimize_static_texts traverse(@root_node) do |tr_node| iterate(tr_node) do |node, previous_node, _parent| if previous_node && previous_node.type == :static_text && node.type == :static_text previous_node.value += node.value true end end end end |
#traverse(node) {|node| ... } ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/bade/optimizer.rb', line 35 def traverse(node, &block) yield node node.children.each do |subnode| traverse(subnode, &block) end end |