Class: XRT::Statement

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

Direct Known Subclasses

Block, Directive, Document, End, TagStart, Text, Whitespace

Defined Under Namespace

Classes: Block, ControlBlock, Directive, Document, End, Tag, TagEnd, TagPair, TagPairEnd, TagStart, 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.to_s == other.to_s
end

#auto_indentObject



90
91
92
93
94
95
# File 'lib/xrt/statement.rb', line 90

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



25
26
27
# File 'lib/xrt/statement.rb', line 25

def children
  []
end

#contains_directive?Boolean

Returns:

  • (Boolean)


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

def contains_directive?
  children.any?{|s|
     s.kind_of? XRT::Statement::Directive
  } || children.any?{|c|
    c.contains_directive?
  }
end

#contentObject



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

def content
  @content
end

#depth(target) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/xrt/statement.rb', line 49

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



84
85
86
87
88
# File 'lib/xrt/statement.rb', line 84

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



72
73
74
75
76
# File 'lib/xrt/statement.rb', line 72

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

#find_blocks_with_directiveObject



78
79
80
81
82
# File 'lib/xrt/statement.rb', line 78

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

#include?(statement) ⇒ Boolean

Returns:

  • (Boolean)


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

def include? statement
  children.each{|child|
    return true if child.equal? statement
    return true if child.include?(statement)
  }
  return false
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



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

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



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

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

#to_sObject



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

def to_s
  inspect
end