Class: SGF::Collection

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_nodeObject

Returns the value of attribute current_node.



6
7
8
# File 'lib/sgf/collection.rb', line 6

def current_node
  @current_node
end

#errorsObject

Returns the value of attribute errors.



6
7
8
# File 'lib/sgf/collection.rb', line 6

def errors
  @errors
end

#gametreesObject

Returns the value of attribute gametrees.



6
7
8
# File 'lib/sgf/collection.rb', line 6

def gametrees
  @gametrees
end

#rootObject (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

#eachObject



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

#inspectObject



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_sObject



47
48
49
# File 'lib/sgf/collection.rb', line 47

def to_s
  SGF::Writer.new.stringify_tree_from @root
end

#update(message, data) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/sgf/collection.rb', line 56

def update(message, data)
  case message
    when :new_children
      data.each do |new_gametree_root|
        @gametrees << SGF::Gametree.new(new_gametree_root)
      end
  end
end