Class: XRT::Statement

Inherits:
Object
  • Object
show all
Defined in:
lib/xrt/statement.rb,
lib/xrt/statement.rb

Direct Known Subclasses

Directive, Document, Text, Whitespace

Defined Under Namespace

Modules: Factory Classes: Block, Directive, Document, End, Text, Whitespace

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ Statement

Returns a new instance of Statement.



5
6
7
# File 'lib/xrt/statement.rb', line 5

def initialize(content)
  @content = content
end

Instance Method Details

#==(other) ⇒ Object



13
14
15
# File 'lib/xrt/statement.rb', line 13

def == other
  self.class == other.class && self.content == other.content
end

#auto_indentObject



64
65
66
67
68
69
# File 'lib/xrt/statement.rb', line 64

def auto_indent
  lines = content.split(/\n/)[1..-1]
  whitespaces = lines.map{|line| line.scan(/^\s+/).first }.compact
  indent = whitespaces.sort_by{|whitespace| whitespace.length }.first
  content.gsub(/^#{indent}/, '')
end

#childrenObject



21
22
23
# File 'lib/xrt/statement.rb', line 21

def children
  []
end

#contentObject



9
10
11
# File 'lib/xrt/statement.rb', line 9

def content
  @content
end

#depth(target) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/xrt/statement.rb', line 37

def depth(target)
  children.each{|child|
    return 0 if child.equal? target
    d = child.depth(target)
    if d
      return d + 1
    end
  }
  nil
end

#find_block_textsObject



58
59
60
61
62
# File 'lib/xrt/statement.rb', line 58

def find_block_texts
  children.select{|child|
    child.kind_of?(XRT::Statement::Block) || child.kind_of?(XRT::Statement::Text)
  }.concat(children.map{|child| child.find_block_texts }.flatten)
end

#find_blocksObject



52
53
54
55
56
# File 'lib/xrt/statement.rb', line 52

def find_blocks
  children.select{|child|
    child.kind_of? XRT::Statement::Block
  }.concat(children.map{|child| child.find_blocks }.flatten)
end

#inspectObject



17
18
19
# File 'lib/xrt/statement.rb', line 17

def inspect
  "<#{self.class}:#{self.content}>"
end

#replace_child(new_child, old_child) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/xrt/statement.rb', line 25

def replace_child(new_child, old_child)
  children.each_with_index{|child, index|
    if child.equal? old_child
      children[index] = new_child
      return old_child
    elsif child.replace_child(new_child, old_child)
      return old_child
    end
  }
  nil
end

#statementsObject



48
49
50
# File 'lib/xrt/statement.rb', line 48

def statements
  children.concat(children.map{|child| child.children }.flatten)
end