Class: CustomTracker::Tracker
- Inherits:
-
Object
- Object
- CustomTracker::Tracker
- Defined in:
- lib/custom_tracker/tracker.rb
Overview
A class to handle several tables.
Instance Method Summary collapse
-
#[](sym) ⇒ Table
Access stored table.
-
#initialize(options) ⇒ Tracker
constructor
Creates new tracker.
-
#new_table(sym, options) ⇒ Table
Create new table and add it to handling.
-
#record(table_name, entry) ⇒ Entry?
Records entry for table.
-
#record_all(entry) ⇒ Integer
Records entry for every table.
-
#save(table_name) ⇒ Integer
Saves the table.
-
#save_all ⇒ nil
Saves all the tables.
-
#tables ⇒ Array<Symbol>
Array of table names.
Constructor Details
#initialize(options) ⇒ Tracker
Creates new tracker.
29 30 31 32 33 34 35 36 |
# File 'lib/custom_tracker/tracker.rb', line 29 def initialize() @saving_block = [:saving_block] unless @saving_block.respond_to? :call raise ArgumentError, "saving_block is not responding to call method!", caller end @tables = {} end |
Instance Method Details
#[](sym) ⇒ Table
Access stored table.
17 18 19 |
# File 'lib/custom_tracker/tracker.rb', line 17 def [](sym) @tables[sym] end |
#new_table(sym, options) ⇒ Table
Create new table and add it to handling.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/custom_tracker/tracker.rb', line 45 def new_table(sym, ) raise ArgumentError, "sym must be a Symbol", caller unless sym.is_a? Symbol raise ArgumentError, "This table already exists", caller if @tables.has_key? sym table = Table.new( .merge( saving_block: Proc.new do |arr, table_local| @saving_block.call(arr, sym, table_local) end ) ) @tables[sym] = table table end |
#record(table_name, entry) ⇒ Entry?
Records entry for table.
77 78 79 |
# File 'lib/custom_tracker/tracker.rb', line 77 def record(table_name, entry) @tables[table_name].record entry end |
#record_all(entry) ⇒ Integer
Records entry for every table
66 67 68 |
# File 'lib/custom_tracker/tracker.rb', line 66 def record_all(entry) @tables.values.count { |t| t.record entry } end |
#save(table_name) ⇒ Integer
Saves the table.
96 97 98 |
# File 'lib/custom_tracker/tracker.rb', line 96 def save(table_name) @tables[table_name].save end |
#save_all ⇒ nil
Saves all the tables.
85 86 87 88 |
# File 'lib/custom_tracker/tracker.rb', line 85 def save_all @tables.values.each(&:save) nil end |
#tables ⇒ Array<Symbol>
Returns array of table names.
9 10 11 |
# File 'lib/custom_tracker/tracker.rb', line 9 def tables @tables.keys end |