Class: RepositoryListener
- Inherits:
-
Object
- Object
- RepositoryListener
- Defined in:
- lib/event/repository_listener.rb
Overview
helper class which create a connection between event handling and the DataRepository
Instance Attribute Summary collapse
-
#data_repository ⇒ DataRepository
readonly
private
The represented repository.
-
#subselect_ranking ⇒ Object
private
as (attribute => occurrence) from the subselect.
Instance Method Summary collapse
-
#change_index(key) ⇒ Object
method to notify the repository to create a new index for the given key.
-
#check_subselect ⇒ Object
private
method to check if a subselect was already generated.
-
#generate_and_output_index ⇒ Object
method to generate the ranking over the index and print its results.
-
#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.
-
#initialize(data_repository) ⇒ RepositoryListener
constructor
initialization.
-
#initialize_subselect ⇒ Object
method to generate the required ranking and present a menu to select the key for the subselect on that index.
-
#listen_to(listenable) ⇒ Object
method to register an instance of this class at a Listenable.
-
#output_barchart(source) ⇒ Object
method to generate a bar chart based on the source parameter.
-
#output_index_ranking ⇒ Object
method to print the calculated ranking in the terminal.
-
#output_ranking(ranking) ⇒ Object
method to print the calculated ranking in the terminal.
Constructor Details
#initialize(data_repository) ⇒ RepositoryListener
initialization
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_repository ⇒ DataRepository (readonly, private)
Returns the represented repository.
94 95 96 |
# File 'lib/event/repository_listener.rb', line 94 def data_repository @data_repository end |
#subselect_ranking ⇒ Object (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
27 28 29 |
# File 'lib/event/repository_listener.rb', line 27 def change_index(key) @data_repository.create_index(key) end |
#check_subselect ⇒ Object (private)
method to check if a subselect was already 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_index ⇒ Object
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
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_subselect ⇒ Object
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) = Menu::SubselectMenu.new(ranking) .add_listener(:repo_listener, self) . end |
#listen_to(listenable) ⇒ Object
method to register an instance of this class at a Listenable
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
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/event/repository_listener.rb', line 77 def (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_ranking ⇒ Object
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
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 |