Class: ConfigNode

Inherits:
Object
  • Object
show all
Defined in:
lib/node.rb

Constant Summary collapse

METALABELS =
{:black_rank => "BR", :white_rank => "WR",:white_player => "PW", :black_player => "PB",
:komi => "KM", :date => "DT", :result => "RE",
:file_format => "FF", :black_country => "BC",
:white_country => "WC", :event => "EV", :source => "SO",
:encoding => "CA", :size => "SZ", :rules => "RU", :time_set => "OT",:handicap => "HA"}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(property = "") ⇒ ConfigNode

Returns a new instance of ConfigNode.



85
86
87
88
89
90
91
# File 'lib/node.rb', line 85

def initialize(property="")
  @node_text = property.dup
  write_property(:file_format,4)
  handicap = property(:handicap)
  @comments = []
  @children = []
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



83
84
85
# File 'lib/node.rb', line 83

def children
  @children
end

#node_textObject

Returns the value of attribute node_text.



83
84
85
# File 'lib/node.rb', line 83

def node_text
  @node_text
end

Instance Method Details

#add_child(node) ⇒ Object



97
98
99
# File 'lib/node.rb', line 97

def add_child(node)
  @children.push(node)
end

#add_comment(comment) ⇒ Object



105
106
107
# File 'lib/node.rb', line 105

def add_comment(comment)
  @comments << (comment + "\n")
end

#commentsObject



108
109
110
# File 'lib/node.rb', line 108

def comments
  @comments.empty? ? "" : "C[#{@comments.join.gsub("]","\\]").gsub(")","\\)")}]"
end

#parentObject



93
94
95
# File 'lib/node.rb', line 93

def parent
  #stub
end

#property(symbol) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/node.rb', line 128

def property(symbol)
  return node_text if symbol == :all
  dup = node_text.dup
  dup.slice!(/.*#{METALABELS[symbol]}\[/)
  return nil if dup.length == node_text.length #means it wasnt found
  dup.slice!(/\].*/)
  return dup
end

#to_move_listObject



117
118
119
120
# File 'lib/node.rb', line 117

def to_move_list
  children_text = @children.first.to_move_list
  ";"+node_text + children_text
end

#to_sObject



112
113
114
115
# File 'lib/node.rb', line 112

def to_s
  children_text = @children.first.to_s
  ";"+node_text + comments + children_text
end

#validate_node_formatObject



101
102
103
# File 'lib/node.rb', line 101

def validate_node_format
  return true
end

#write_property(symbol, value) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/node.rb', line 137

def write_property(symbol, value)
  return unless value
  raise "Invalid property #{symbol}" unless METALABELS[symbol]
  node = "#{METALABELS[symbol]}[#{value}]"
  @node_text.gsub!(/#{METALABELS[symbol]}\[\w*\]/, "") #in case it already had it
  @node_text = node + @node_text 
  size = property(:size)
  #a little hackish to insert the AB node only
  @node_text += SGF.handi_node(property(:size),value)[5..-1] if(size && 
                                                         symbol == :handicap)
end