Class: Rapid::Template::Node::IfNode

Inherits:
Base
  • Object
show all
Defined in:
lib/rapid/template/node/if_node.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#parent

Instance Method Summary collapse

Methods inherited from Base

#content, #next_node, #static?

Constructor Details

#initialize(statement, then_nodes, else_nodes = nil) ⇒ IfNode

Returns a new instance of IfNode.



9
10
11
12
13
14
15
16
17
# File 'lib/rapid/template/node/if_node.rb', line 9

def initialize statement, then_nodes, else_nodes = nil
  @statement = statement
  
  @then_node = polish_nodes then_nodes
  @else_node = polish_nodes else_nodes
  
  @then_node.send :parent=, self if @then_node
  @else_node.send :parent=, self if @else_node
end

Instance Attribute Details

#else_nodeObject (readonly)

Returns the value of attribute else_node.



7
8
9
# File 'lib/rapid/template/node/if_node.rb', line 7

def else_node
  @else_node
end

#statementObject (readonly)

Returns the value of attribute statement.



7
8
9
# File 'lib/rapid/template/node/if_node.rb', line 7

def statement
  @statement
end

#then_nodeObject (readonly)

Returns the value of attribute then_node.



7
8
9
# File 'lib/rapid/template/node/if_node.rb', line 7

def then_node
  @then_node
end

Instance Method Details

#==(obj) ⇒ Object



38
39
40
41
42
43
# File 'lib/rapid/template/node/if_node.rb', line 38

def == obj
  obj.is_a?(self.class) && 
    obj.statement == statement && 
    obj.then_node == then_node && 
    obj.else_node == @else_node
end

#inspectObject



45
46
47
# File 'lib/rapid/template/node/if_node.rb', line 45

def inspect
  %(#<if @statement=#{@statement.inspect} @then=#{@then_node.inspect} @else=#{@else_node.inspect}>)
end

#next_to(node) ⇒ Object



34
35
36
# File 'lib/rapid/template/node/if_node.rb', line 34

def next_to node
  parent.next_to self if parent
end

#pull(result) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rapid/template/node/if_node.rb', line 19

def pull result
  then_content = then_node.content if then_node
  if then_content.nil?
    raise Rapid::InvalidTemplateError.new("if statement must have content within it")
  end
  
  if result.starts_with? then_content
    then_node.pull result
    pull_variable result, statement, true
  else
    pull_variable result, statement, false
    else_node.pull result if else_node
  end
end