Permalink
Browse files

Add listeners to Demisus::SudokuSolver

* Add two listeners to the solver class: {before,after}_applying_rule
* Change the execute_rule method to receive a list of parameters,
  instead of a single parameter
  • Loading branch information...
emanchado committed Apr 2, 2009
1 parent 69563ab commit ea322df393c674ec47017e792b966f68b0dd1bdb
Showing with 13 additions and 4 deletions.
  1. +11 −2 demisus/solver.rb
  2. +2 −2 test/solver_rules.rb
View
@@ -1,12 +1,18 @@
require 'set'
require 'demisus/board'
+require 'demisus/mixins/listeners'
module Demisus
class UnsolvableSudokuError < StandardError; end
class SudokuSolver
attr_reader :board
+ include Demisus::Mixins::Listeners
+
+ def event_types; [:before_applying_rule,
+ :after_applying_rule]; end
+
# Class methods ==========================================================
def self.rules(type=:all)
@@ -50,6 +56,7 @@ def self.define_rule(type, id, name, description, &blk)
# objects; nil for the unknown numbers)
def initialize(numbers)
@board = SudokuBoard.new(numbers)
+ initialize_listeners
end
def rules(type)
@@ -109,8 +116,10 @@ def number_unsolved_cells
@board.unsolved_cells.size
end
- def execute_rule(rule, cell_group)
- rule[:action].call(cell_group)
+ def execute_rule(rule, *params)
+ call_listeners(:before_applying_rule, [rule])
+ rule[:action].call(*params)
+ call_listeners(:after_applying_rule, [rule])
ensure_consistency!
end
View
@@ -4,8 +4,8 @@
require 'flexmock/test_unit'
class TestSudokuSolverRules < Test::Unit::TestCase
- def execute_rule(id, cells)
- Demisus::SudokuSolver.rule_by_id(id)[:action].call(cells)
+ def execute_rule(id, *params)
+ Demisus::SudokuSolver.rule_by_id(id)[:action].call(*params)
end
def test_final_numbers

0 comments on commit ea322df

Please sign in to comment.