Class: HTOTConv::Outline
- Inherits:
-
Object
show all
- Defined in:
- lib/htot_conv/outline.rb
Defined Under Namespace
Classes: Item, Tree, ValidationError
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of Outline.
4
5
6
|
# File 'lib/htot_conv/outline.rb', line 4
def initialize
@item = []
end
|
Instance Attribute Details
#item ⇒ Object
Returns the value of attribute item.
10
11
12
|
# File 'lib/htot_conv/outline.rb', line 10
def item
@item
end
|
Returns the value of attribute key_header.
8
9
10
|
# File 'lib/htot_conv/outline.rb', line 8
def
end
|
Returns the value of attribute value_header.
9
10
11
|
# File 'lib/htot_conv/outline.rb', line 9
def
end
|
Instance Method Details
#==(v) ⇒ Object
48
49
50
51
52
|
# File 'lib/htot_conv/outline.rb', line 48
def ==(v)
( == v.) &&
( == v.) &&
(@item == v.item)
end
|
#add_item(*args) ⇒ Object
12
13
14
|
# File 'lib/htot_conv/outline.rb', line 12
def add_item(*args)
@item << Item.new(*args)
end
|
#max_level ⇒ Object
34
35
36
37
38
39
|
# File 'lib/htot_conv/outline.rb', line 34
def max_level
[
.length,
*(@item.map { |v| v.level.to_i }),
].max
end
|
#max_value_length ⇒ Object
41
42
43
44
45
46
|
# File 'lib/htot_conv/outline.rb', line 41
def max_value_length
[
.length,
*(@item.map { |v| (v.value)? v.value.length : 0 }),
].max
end
|
#to_tree ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/htot_conv/outline.rb', line 54
def to_tree
root = Tree.new
last_node = root
@item.each_with_index do |item,i|
parent_node = root
if ((item.level > 1) && !(last_node.root?))
if item.level > last_node.item.level
parent_node = last_node
else
parent_node = last_node.parent
parent_node = parent_node.parent until (parent_node.root? || parent_node.item.level < item.level)
end
end
parent_node << item
last_node = parent_node.to_a.last
end
root
end
|
#valid? ⇒ Boolean
25
26
27
28
29
30
31
32
|
# File 'lib/htot_conv/outline.rb', line 25
def valid?
begin
validate
true
rescue ValidationError
false
end
end
|
#validate ⇒ Object
16
17
18
19
20
21
22
23
|
# File 'lib/htot_conv/outline.rb', line 16
def validate
raise ValidationError, "key_header must be an array" unless .kind_of?(Array)
raise ValidationError, "key_header elements must be strings." unless .all? { |v| v.nil? || v.kind_of?(String) }
raise ValidationError, "value_header must be an array" unless .kind_of?(Array)
raise ValidationError, "value_header elements must be strings." unless .all? { |v| v.nil? || v.kind_of?(String) }
raise ValidationError, "item must be an array" unless @item.kind_of?(Array)
@item.each { |item| item.validate }
end
|