Class: XRT::Statement
- Inherits:
-
Object
show all
- Defined in:
- lib/xrt/statement.rb,
lib/xrt/statement.rb
Defined Under Namespace
Modules: Factory
Classes: Block, 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.content == other.content
end
|
#auto_indent ⇒ Object
86
87
88
89
90
91
|
# File 'lib/xrt/statement.rb', line 86
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
|
#children ⇒ Object
21
22
23
|
# File 'lib/xrt/statement.rb', line 21
def children
[]
end
|
#contains_directive? ⇒ Boolean
60
61
62
63
64
65
66
|
# File 'lib/xrt/statement.rb', line 60
def contains_directive?
children.any?{|s|
s.kind_of? XRT::Statement::Directive
} || children.any?{|c|
c.contains_directive?
}
end
|
#content ⇒ Object
9
10
11
|
# File 'lib/xrt/statement.rb', line 9
def content
@content
end
|
#depth(target) ⇒ Object
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/xrt/statement.rb', line 45
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_texts ⇒ Object
80
81
82
83
84
|
# File 'lib/xrt/statement.rb', line 80
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_blocks ⇒ Object
68
69
70
71
72
|
# File 'lib/xrt/statement.rb', line 68
def find_blocks
children.select{|child|
child.kind_of? XRT::Statement::Block
}.concat(children.map{|child| child.find_blocks }.flatten)
end
|
#find_blocks_with_directive ⇒ Object
74
75
76
77
78
|
# File 'lib/xrt/statement.rb', line 74
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
25
26
27
28
29
30
31
|
# File 'lib/xrt/statement.rb', line 25
def include? statement
children.each{|child|
return true if child.equal? statement
return true if child.include?(statement)
}
return false
end
|
#inspect ⇒ Object
17
18
19
|
# File 'lib/xrt/statement.rb', line 17
def inspect
"(#{self.class}:#{self.content})"
end
|
#replace_child(new_child, old_child) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/xrt/statement.rb', line 33
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
|
#statements ⇒ Object
56
57
58
|
# File 'lib/xrt/statement.rb', line 56
def statements
children.concat(children.map{|child| child.children }.flatten)
end
|