Class: Rapid::Template::Node::Variable

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

Instance Attribute Summary collapse

Attributes inherited from Base

#parent

Instance Method Summary collapse

Methods inherited from Base

#content, #next_node, #next_to, #static?

Constructor Details

#initialize(name) ⇒ Variable

Returns a new instance of Variable.



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

def initialize name
  if name.blank?
    raise Rapid::InvalidTemplateError.new("Empty ERB statements aren't allowed")
  end
  
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#==(obj) ⇒ Object



44
45
46
# File 'lib/rapid/template/node/variable.rb', line 44

def == obj
  obj.is_a?(self.class) && obj.name == @name
end

#inspectObject



48
49
50
# File 'lib/rapid/template/node/variable.rb', line 48

def inspect
  %(#<variable #{@name.inspect}>)
end

#pull(result) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rapid/template/node/variable.rb', line 17

def pull result
  next_piece = next_node
  if next_piece.nil?
    result[name] = result.content.clone
    result.move! result.content.length
    return
  end
  
  unless next_piece.static?
    raise Rapid::InvalidTemplateError.new("Two ERB variables can't be touching #{result.content[0..20].inspect}")
  end
  
  index = result.index next_piece.content
  if index.nil?
    next_piece_does_not_match result, next_piece.content
  end
  
  if index == 0
    value = ""
  else
    value = result.content[0..index-1]
    result.move! index
  end
  
  result[name] = value
end