Module: Reversi::Configuration

Included in:
Reversi
Defined in:
lib/reversi/configuration.rb

Constant Summary collapse

OPTIONS_KEYS =
[
  :player_b,
  :player_w,
  :disk_b,
  :disk_w,
  :disk_color_b,
  :disk_color_w,
  :initial_position,
  :progress,
  :stack_limit
].freeze
DEFAULTS =
{
  :player_b => Reversi::Player::RandomAI,
  :player_w => Reversi::Player::RandomAI,
  :disk_b => 'b',
  :disk_w => 'w',
  :disk_color_b => 0,
  :disk_color_w => 0,
  :initial_position => {:black => [[4, 5], [5, 4]], :white => [[4, 4], [5, 5]]},
  :progress => false,
  :stack_limit => 3
}

Instance Method Summary collapse

Instance Method Details

#configure {|_self| ... } ⇒ Object

This method is used for setting configuration options.

Yields:

  • (_self)

Yield Parameters:



31
32
33
# File 'lib/reversi/configuration.rb', line 31

def configure
  yield self
end

#optionsObject

Create a hash of options.



36
37
38
# File 'lib/reversi/configuration.rb', line 36

def options
  Hash[*OPTIONS_KEYS.map{|key| [key, send(key)]}.flatten]
end

#resetObject

Reset all options to their default values.



41
42
43
44
45
# File 'lib/reversi/configuration.rb', line 41

def reset
  DEFAULTS.each do |option, default|
    self.send("#{option}=".to_sym, default)
  end
end

#set_defaultsObject

Set default value for the option that is still not set.



48
49
50
51
52
53
54
# File 'lib/reversi/configuration.rb', line 48

def set_defaults
  DEFAULTS.each do |option, default|
    default.class == String ?
    eval("self.#{option} ||= \'#{default}\'") :
    eval("self.#{option} ||= #{default}")
  end
end