Class: Interscript::Node::Item::Group

Inherits:
Interscript::Node::Item show all
Defined in:
lib/interscript/node/item/group.rb,
lib/interscript/visualize/nodes.rb

Instance Attribute Summary collapse

Attributes inherited from Interscript::Node::Item

#item

Instance Method Summary collapse

Methods inherited from Interscript::Node::Item

try_convert

Constructor Details

#initialize(*children) ⇒ Group

Returns a new instance of Group.



4
5
6
7
8
# File 'lib/interscript/node/item/group.rb', line 4

def initialize *children
  @children = children.flatten.map do |i|
    Interscript::Node::Item.try_convert(i)
  end
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



2
3
4
# File 'lib/interscript/node/item/group.rb', line 2

def children
  @children
end

Instance Method Details

#+(item) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/interscript/node/item/group.rb', line 10

def +(item)
  item = Interscript::Node::Item.try_convert(item)
  out = self.dup
  if Interscript::Node::Item::Group === item
    out.children += item.children
  else
    out.children << item
  end
  out.verify!
  out
end

#==(other) ⇒ Object



72
73
74
# File 'lib/interscript/node/item/group.rb', line 72

def ==(other)
  super && self.children == other.children
end

#compactObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/interscript/node/item/group.rb', line 22

def compact
  out = self.dup do |n|
    n.children = n.children.reject do |i|
      (Interscript::Node::Alias === i && i.name == :none) ||
      (Interscript::Node::String === i && i.data == "")
    end
  end

  if out.children.count == 0
    Interscript::Node::Alias.new(:none)
  elsif out.children.count == 1
    out.children.first
  else
    out
  end
end

#downcaseObject



39
# File 'lib/interscript/node/item/group.rb', line 39

def downcase; self.dup.tap { |i| i.children = i.children.map(&:downcase) }; end

#first_stringObject



55
56
57
# File 'lib/interscript/node/item/group.rb', line 55

def first_string
  self.children.map(&:first_string).reduce(&:+)
end

#inspectObject



76
77
78
# File 'lib/interscript/node/item/group.rb', line 76

def inspect
  @children.map(&:inspect).join("+")
end

#max_lengthObject



63
64
65
# File 'lib/interscript/node/item/group.rb', line 63

def max_length
  @children.map { |i| i.max_length }.sum
end

#nth_stringObject



59
60
61
# File 'lib/interscript/node/item/group.rb', line 59

def nth_string
  self.children.map(&:nth_string).reduce(&:+)
end

#to_hashObject



67
68
69
70
# File 'lib/interscript/node/item/group.rb', line 67

def to_hash
  { :class => self.class.to_s,
    :children => self.children.map{|x| x.to_hash} }
end

#to_html(doc) ⇒ Object



58
59
60
# File 'lib/interscript/visualize/nodes.rb', line 58

def to_html(doc)
  @children.map{|i|i.to_html(doc)}.join(" + ")
end

#upcaseObject



40
# File 'lib/interscript/node/item/group.rb', line 40

def upcase; self.dup.tap { |i| i.children = i.children.map(&:upcase) }; end

#verify!Object

Verify if a group is valid



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/interscript/node/item/group.rb', line 43

def verify!
  wrong = @children.find do |i|
    Interscript::Node::Item::Stage === i ||
    ! (Interscript::Node::Item === i) ||
    i.class == Interscript::Node::Item
  end

  if wrong
    raise Interscript::MapLogicError, "An I::Node::Item::Group can't contain an #{wrong.class} item."
  end
end