Class: CustomTracker::Tracker

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

Overview

A class to handle several tables.

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Tracker

Creates new tracker.

Parameters:

  • options (Hash)

Options Hash (options):

  • saving_block (#call)

    callable object which must accept following parameters: Array<Entry> - entries to save, Symbol - name of table and Table for which this block will be called.



29
30
31
32
33
34
35
36
# File 'lib/custom_tracker/tracker.rb', line 29

def initialize(options)
  @saving_block = options[: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.

Returns:



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.

Parameters:

Returns:

  • (Table)

    created table.

Raises:

  • (ArgumentError)


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, options)
  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(
    options.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.

Parameters:

  • table_name (Symbol)

    name of table.

  • entry (Entry)

Returns:

  • (Entry, nil)

    Entry if it was saved or nil if it wasn’t.



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

Parameters:

Returns:

  • (Integer)

    amount of tables which registred



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.

Parameters:

  • table_name (Symbol)

    name of table.

Returns:

  • (Integer)

    amount of saved entries.



96
97
98
# File 'lib/custom_tracker/tracker.rb', line 96

def save(table_name)
  @tables[table_name].save
end

#save_allnil

Saves all the tables.

Returns:

  • (nil)


85
86
87
88
# File 'lib/custom_tracker/tracker.rb', line 85

def save_all
  @tables.values.each(&:save)
  nil
end

#tablesArray<Symbol>

Returns array of table names.

Returns:

  • (Array<Symbol>)

    array of table names.



9
10
11
# File 'lib/custom_tracker/tracker.rb', line 9

def tables
  @tables.keys
end