Class: SGF::Collection
- Inherits:
-
Object
- Object
- SGF::Collection
- Includes:
- Enumerable, Observable
- Defined in:
- lib/sgf/collection.rb
Overview
Collection holds most of the logic, for now. It has all the nodes, can iterate over them, and can even save to a file!
Instance Attribute Summary collapse
-
#current_node ⇒ Object
Returns the value of attribute current_node.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#gametrees ⇒ Object
Returns the value of attribute gametrees.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #<<(gametree) ⇒ Object
-
#==(other) ⇒ Object
Compares a tree to another tree, node by node.
- #each ⇒ Object
-
#initialize(root = SGF::Node.new) ⇒ Collection
constructor
A new instance of Collection.
- #inspect ⇒ Object
-
#save(filename) ⇒ Object
Saves the Collection as an SGF file.
- #to_s ⇒ Object
- #update(message, data) ⇒ Object
Constructor Details
#initialize(root = SGF::Node.new) ⇒ Collection
Returns a new instance of Collection.
9 10 11 12 13 14 15 16 17 |
# File 'lib/sgf/collection.rb', line 9 def initialize(root = SGF::Node.new) @root = root @current_node = @root @root.add_observer(self) @errors = [] @gametrees = @root.children.map do |root_of_tree| SGF::Gametree.new(root_of_tree) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object (private)
71 72 73 74 |
# File 'lib/sgf/collection.rb', line 71 def method_missing method_name, *args super(method_name, args) if @root.children.empty? || !@root.children[0].properties.has_key?(method_name) @root.children[0].properties[method_name] end |
Instance Attribute Details
#current_node ⇒ Object
Returns the value of attribute current_node.
6 7 8 |
# File 'lib/sgf/collection.rb', line 6 def current_node @current_node end |
#errors ⇒ Object
Returns the value of attribute errors.
6 7 8 |
# File 'lib/sgf/collection.rb', line 6 def errors @errors end |
#gametrees ⇒ Object
Returns the value of attribute gametrees.
6 7 8 |
# File 'lib/sgf/collection.rb', line 6 def gametrees @gametrees end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
7 8 9 |
# File 'lib/sgf/collection.rb', line 7 def root @root end |
Instance Method Details
#<<(gametree) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/sgf/collection.rb', line 27 def <<(gametree) unless gametree.instance_of?(SGF::Gametree) raise ArgumentError, "Expected instance of class SGF::Gametree but was instance of #{gametree.class}" end @root.add_children gametree.root end |
#==(other) ⇒ Object
Compares a tree to another tree, node by node. Nodes must be the same (same properties, parents and children).
36 37 38 |
# File 'lib/sgf/collection.rb', line 36 def == other self.map { |node| node } == other.map { |node| node } end |
#each ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/sgf/collection.rb', line 19 def each gametrees.each do |game| game.each do |node| yield node end end end |
#inspect ⇒ Object
40 41 42 43 44 45 |
# File 'lib/sgf/collection.rb', line 40 def inspect out = "#<SGF::Collection:#{self.object_id}, " out << "#{gametrees.count} Games, " out << "#{node_count} Nodes" out << ">" end |
#save(filename) ⇒ Object
Saves the Collection as an SGF file. Takes a filename as argument.
52 53 54 |
# File 'lib/sgf/collection.rb', line 52 def save filename SGF::Writer.new.save(@root, filename) end |
#to_s ⇒ Object
47 48 49 |
# File 'lib/sgf/collection.rb', line 47 def to_s SGF::Writer.new.stringify_tree_from @root end |