Class: Interscript::Node::Item::Group
- Inherits:
-
Interscript::Node::Item
- Object
- Interscript::Node
- Interscript::Node::Item
- Interscript::Node::Item::Group
- Defined in:
- lib/interscript/node/item/group.rb,
lib/interscript/visualize/nodes.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
Attributes inherited from Interscript::Node::Item
Instance Method Summary collapse
- #+(item) ⇒ Object
- #==(other) ⇒ Object
- #compact ⇒ Object
- #downcase ⇒ Object
- #first_string ⇒ Object
-
#initialize(*children) ⇒ Group
constructor
A new instance of Group.
- #inspect ⇒ Object
- #max_length ⇒ Object
- #nth_string ⇒ Object
- #to_hash ⇒ Object
- #to_html(doc) ⇒ Object
- #upcase ⇒ Object
-
#verify! ⇒ Object
Verify if a group is valid.
Methods inherited from Interscript::Node::Item
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
#children ⇒ Object
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 |
#compact ⇒ Object
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 |
#downcase ⇒ Object
39 |
# File 'lib/interscript/node/item/group.rb', line 39 def downcase; self.dup.tap { |i| i.children = i.children.map(&:downcase) }; end |
#first_string ⇒ Object
55 56 57 |
# File 'lib/interscript/node/item/group.rb', line 55 def first_string self.children.map(&:first_string).reduce(&:+) end |
#inspect ⇒ Object
76 77 78 |
# File 'lib/interscript/node/item/group.rb', line 76 def inspect @children.map(&:inspect).join("+") end |
#max_length ⇒ Object
63 64 65 |
# File 'lib/interscript/node/item/group.rb', line 63 def max_length @children.map { |i| i.max_length }.sum end |
#nth_string ⇒ Object
59 60 61 |
# File 'lib/interscript/node/item/group.rb', line 59 def nth_string self.children.map(&:nth_string).reduce(&:+) end |
#to_hash ⇒ Object
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 |
#upcase ⇒ Object
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 |