Class: DataRepository
- Inherits:
-
Object
- Object
- DataRepository
- Defined in:
- lib/data/data_repository.rb
Overview
This class serves as a data repository storing the read data and handling the meta information
Instance Attribute Summary collapse
-
#index ⇒ Object
readonly
Hash returns specific mapping on the given key (values => Array).
-
#index_criteria ⇒ Symbol
readonly
private
The symbol that represents the current index.
-
#repository ⇒ Object
readonly
Array an array containing all entries.
Instance Method Summary collapse
-
#create_entries(data) ⇒ Object
private
creates Entrys of the parsed data and stores it in the repository.
-
#create_index(key) ⇒ Object
creates an index, mapping all Entrys.
-
#get_count_to_key(key) ⇒ Integer
method to get the number of entries for the given key.
-
#get_entries_from_index_to(key) ⇒ Array
method to return all entries with the key from the index.
-
#get_entries_to(criteria, key) ⇒ Array
method to get all entries for a given value in the given entry attribute.
-
#get_keys_and_their_count ⇒ Hash
method to retrieve the number of entries for every key.
-
#initialize(filename, key = nil) ⇒ DataRepository
constructor
initialization.
-
#map_key_to_attribute(key, entry) ⇒ Object
private
method to determine the mapping to the given attribute of the Entry.
-
#read_file(filename) ⇒ Array
private
calls the FileReader to get the content of the file.
Constructor Details
#initialize(filename, key = nil) ⇒ DataRepository
initialization
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/data/data_repository.rb', line 16 def initialize(filename, key = nil) key = :http_status if (key == nil) @index = Hash.new() @repository = Array.new() begin create_entries(read_file(filename)) create_index(key) rescue StandardError => e puts "Error: #{e.}".red end end |
Instance Attribute Details
#index ⇒ Object (readonly)
Returns Hash returns specific mapping on the given key (values => Array).
9 10 11 |
# File 'lib/data/data_repository.rb', line 9 def index @index end |
#index_criteria ⇒ Symbol (readonly, private)
Returns the symbol that represents the current index.
86 87 88 |
# File 'lib/data/data_repository.rb', line 86 def index_criteria @index_criteria end |
#repository ⇒ Object (readonly)
Returns Array an array containing all entries.
11 12 13 |
# File 'lib/data/data_repository.rb', line 11 def repository @repository end |
Instance Method Details
#create_entries(data) ⇒ Object (private)
creates Entrys of the parsed data and stores it in the repository
90 91 92 93 94 |
# File 'lib/data/data_repository.rb', line 90 def create_entries(data) data.each { |line| @repository << Entry::Entry.new(line) if (!line.empty?) } end |
#create_index(key) ⇒ Object
creates an index, mapping all Entrys
73 74 75 76 77 78 79 80 81 |
# File 'lib/data/data_repository.rb', line 73 def create_index(key) @index_criteria = key @index = Hash.new() if (!@index.empty?) @repository.each { |entry| mapped_key = map_key_to_attribute(key, entry) @index[mapped_key] = Array.new() if (!@index[mapped_key]) @index[mapped_key] << entry } end |
#get_count_to_key(key) ⇒ Integer
method to get the number of entries for the given key
31 32 33 34 35 36 37 |
# File 'lib/data/data_repository.rb', line 31 def get_count_to_key(key) if (!@index.empty?) @index[key].size else puts "Error: index is empty. No index has been created!".red end end |
#get_entries_from_index_to(key) ⇒ Array
method to return all entries with the key from the index
53 54 55 |
# File 'lib/data/data_repository.rb', line 53 def get_entries_from_index_to(key) get_entries_to(@index_criteria, key) end |
#get_entries_to(criteria, key) ⇒ Array
method to get all entries for a given value in the given entry attribute
62 63 64 65 66 67 68 69 |
# File 'lib/data/data_repository.rb', line 62 def get_entries_to(criteria, key) result = Array.new() @repository.each { |entry| attribute = Entry::EntryAttributeFactory.get_attribute_to(criteria, entry) result << entry if (key.eql?(attribute)) } return result end |
#get_keys_and_their_count ⇒ Hash
method to retrieve the number of entries for every key
41 42 43 44 45 46 47 |
# File 'lib/data/data_repository.rb', line 41 def get_keys_and_their_count result = Hash.new() @index.each_key { |key| result[key] = @index[key].size } return result end |
#map_key_to_attribute(key, entry) ⇒ Object (private)
method to determine the mapping to the given attribute of the Entry
100 101 102 103 104 105 106 107 108 |
# File 'lib/data/data_repository.rb', line 100 def map_key_to_attribute(key, entry) case key when :source then entry.source when :http_status then entry.http_status when :http_request then entry.http_request else entry. end end |
#read_file(filename) ⇒ Array (private)
calls the FileReader to get the content of the file
113 114 115 |
# File 'lib/data/data_repository.rb', line 113 def read_file(filename) RubyUtils::FileReader.new(filename, " ").data end |