Skip to content

Instantly share code, notes, and snippets.

@keithrbennett
Created September 13, 2012 09:15
Show Gist options
  • Select an option

  • Save keithrbennett/3713110 to your computer and use it in GitHub Desktop.

Select an option

Save keithrbennett/3713110 to your computer and use it in GitHub Desktop.
Diff from using Forwardable to replace delegating methods
diff --git a/lib/life_game_viewer/view/life_table_model.rb b/lib/life_game_viewer/view/life_table_model.rb
index 0ee2966..6cbcba1 100644
--- a/lib/life_game_viewer/view/life_table_model.rb
+++ b/lib/life_game_viewer/view/life_table_model.rb
@@ -3,15 +3,26 @@ require 'java'
java_import javax.swing.table.AbstractTableModel
java_import javax.swing.JOptionPane
+require 'forwardable'
+
require_relative 'generations'
# This class is the model used to drive Swing's JTable.
# It contains a LifeModel to which it delegates most calls.
class LifeTableModel < AbstractTableModel
+ extend Forwardable
+
attr_accessor :life_model
attr_reader :generations
+ def_delegator :@life_model, :row_count, :getRowCount
+ def_delegator :@life_model, :column_count, :getColumnCount
+ def_delegator :@life_model, :number_living
+ def_delegator :@life_model, :alive?, :getValueAt
+
+ def_delegator :@generations, :at_first_generation?
+ def_delegator :@generations, :at_last_generation?
def initialize(life_model)
super()
@@ -24,34 +35,10 @@ class LifeTableModel < AbstractTableModel
@generations = Generations.new(life_model)
end
- def getRowCount
- life_model.row_count
- end
-
- def getColumnCount
- life_model.column_count
- end
-
- def getValueAt(row, col)
- life_model.alive?(row, col)
- end
-
def getColumnName(colnum)
nil
end
- def at_first_generation?
- generations.at_first_generation?
- end
-
- def at_last_generation?
- generations.at_last_generation?
- end
-
- def number_living
- life_model.number_living
- end
-
def go_to_next_generation
if at_last_generation?
JOptionPane.show_message_dialog(nil, "Generation ##{generations.current_num} is the last non-repeating generation.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment