Class: RepositoryListener

Inherits:
Object
  • Object
show all
Defined in:
lib/event/repository_listener.rb

Overview

helper class which create a connection between event handling and the DataRepository

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_repository) ⇒ RepositoryListener

initialization

Parameters:

  • data_repository (DataRepository)

    the repository that the Listener should notify



13
14
15
16
# File 'lib/event/repository_listener.rb', line 13

def initialize(data_repository)
  @data_repository = data_repository
  @observed_objects = Array.new()
end

Instance Attribute Details

#data_repositoryDataRepository (readonly, private)

Returns the represented repository.

Returns:



94
95
96
# File 'lib/event/repository_listener.rb', line 94

def data_repository
  @data_repository
end

#subselect_rankingObject (private)

as (attribute => occurrence) from the subselect



97
98
99
# File 'lib/event/repository_listener.rb', line 97

def subselect_ranking
  @subselect_ranking
end

Instance Method Details

#change_index(key) ⇒ Object

method to notify the repository to create a new index for the given key

Parameters:

  • key (Symbol)

    the new index key



27
28
29
# File 'lib/event/repository_listener.rb', line 27

def change_index(key)
  @data_repository.create_index(key)
end

#check_subselectObject (private)

method to check if a subselect was already generated

Raises:

  • (ArgumentError)

    if no subselect has been generated



101
102
103
104
105
# File 'lib/event/repository_listener.rb', line 101

def check_subselect
  if (@subselect_ranking == nil)
    raise ArgumentError, "Error: No subindex created".red
  end
end

#generate_and_output_indexObject

method to generate the ranking over the index and print its results



32
33
34
35
# File 'lib/event/repository_listener.rb', line 32

def generate_and_output_index
  ranking = Statistic.generate_ranking_for_index(@data_repository.index, 10)
  output_ranking(ranking)
end

#generate_subselect(value, criteria) ⇒ Object

method to retrieve all entries with the value at the given criteria from the index and print the top n ranking of the result

Parameters:

  • value (Object)

    the search value

  • criteria (Symbol)

    the ranking criteria



50
51
52
53
54
# File 'lib/event/repository_listener.rb', line 50

def generate_subselect(value, criteria)
  entries = @data_repository.get_entries_from_index_to(value)
  @subselect_ranking = Statistic.generate_ranking_for(entries, 10, criteria)
  puts "Generated subindex.".yellow
end

#initialize_subselectObject

method to generate the required ranking and present a menu to select the key for the subselect on that index



39
40
41
42
43
44
# File 'lib/event/repository_listener.rb', line 39

def initialize_subselect
  ranking = Statistic.generate_ranking_for_index(@data_repository.index, 10)
  subselect_menu = Menu::SubselectMenu.new(ranking)
  subselect_menu.add_listener(:repo_listener, self)
  subselect_menu.print_menu
end

#listen_to(listenable) ⇒ Object

method to register an instance of this class at a Listenable

Parameters:

  • listenable (Object)

    an object of a class that uses the Listenable module



21
22
23
# File 'lib/event/repository_listener.rb', line 21

def listen_to(listenable)
  listenable.add_listener(:repo_listener, self)
end

#output_barchart(source) ⇒ Object

method to generate a bar chart based on the source parameter

Parameters:

  • source (Symbol)

    an indicator for which data a bar chart should be generated

Raises:

  • (ArgumentError)

    if the provided source was not found



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/event/repository_listener.rb', line 77

def output_barchart(source)
  case (source)
    when :index
      ranking = Statistic.
                generate_ranking_for_index(@data_repository.index, 10)
      RankingDiagram.new(ranking)
    when :subselect
      check_subselect
      RankingDiagram.new(@subselect_ranking)
  else
    raise ArgumentError, "Error: Unknown index type for bar chart"
  end
end

#output_index_rankingObject

method to print the calculated ranking in the terminal



68
69
70
71
# File 'lib/event/repository_listener.rb', line 68

def output_index_ranking
  check_subselect
  output_ranking(@subselect_ranking)
end

#output_ranking(ranking) ⇒ Object

method to print the calculated ranking in the terminal

Parameters:

  • ranking (Hash)

    the sorted result with the highest ranking mapped as (attribute => occurrence)



59
60
61
62
63
64
65
# File 'lib/event/repository_listener.rb', line 59

def output_ranking(ranking)
  puts "Output as: number of occurence | entry content".yellow
  ranking.each { |entry|
    print "%5s ".red % [entry[1]]
    puts "times: #{(entry[0]).to_s.magenta} "
  }
end