Class: Bade::Optimizer

Inherits:
Object show all
Defined in:
lib/bade/optimizer.rb

Instance Method Summary collapse

Constructor Details

#initialize(root_node) ⇒ Optimizer

Returns a new instance of Optimizer.

Parameters:



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

Parameters:



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

#optimizeBade::Node

Returns:

  • (Bade::Node)


16
17
18
19
20
# File 'lib/bade/optimizer.rb', line 16

def optimize
  optimize_static_texts

  @root_node
end

#optimize_static_textsObject



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

Parameters:

Yields:

  • (node)


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