Class: EventReporter::Database
- Inherits:
-
Object
- Object
- EventReporter::Database
- Includes:
- ParseInput
- Defined in:
- lib/event_reporter/database.rb
Instance Method Summary collapse
- #add(instructions) ⇒ Object
- #advanced_find(scope, instructions, logic) ⇒ Object
-
#initialize ⇒ Database
constructor
A new instance of Database.
- #initiate_search(scope, input) ⇒ Object
- #load_csv(filename) ⇒ Object
- #parse_atts_crits(input) ⇒ Object
- #parse_maths_input(instructions) ⇒ Object
- #route_find(scope, instructions) ⇒ Object
- #route_queue(input) ⇒ Object
- #search(scope, attribute, criteria) ⇒ Object
- #subtract(instructions) ⇒ Object
- #validate_input(attribute, criterias) ⇒ Object
Methods included from ParseInput
Constructor Details
Instance Method Details
#add(instructions) ⇒ Object
100 101 102 103 104 |
# File 'lib/event_reporter/database.rb', line 100 def add(instructions) attribute, criteria = parse_maths_input(instructions) @cache.queue |= search('all', attribute, criteria) @cache.queue_count("") end |
#advanced_find(scope, instructions, logic) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/event_reporter/database.rb', line 40 def advanced_find(scope, instructions, logic) instructions = instructions.join(" ").split(" #{logic} ") search1 = instructions[0].split(" ") search2 = instructions[1].split(" ") result1 = initiate_search(scope, search1) result2 = initiate_search(scope, search2) if logic == "and" return result1 & result2 elsif logic == "or" return result1 | result2 end end |
#initiate_search(scope, input) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/event_reporter/database.rb', line 56 def initiate_search(scope, input) throw(:top, "Incorrect find input") if input.length < 2 attribute, criterias = parse_atts_crits(input) results = criterias.map {|criteria| search(scope, attribute, criteria)} results.reduce(&:|) end |
#load_csv(filename) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/event_reporter/database.rb', line 19 def load_csv(filename) filename = filename.empty? ? "event_attendees.csv" : filename[0] @attendees = CSVParser.new(filename).parse puts "Loaded #{filename} (#{@attendees.length} attendees)" end |
#parse_atts_crits(input) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/event_reporter/database.rb', line 65 def parse_atts_crits(input) attribute = input[0] criterias = input[1..-1].join(" ").split(",") criterias.map! do |criteria| criteria.gsub("(", "").gsub(")", "").strip end validate_input(attribute, criterias) return attribute, criterias end |
#parse_maths_input(instructions) ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'lib/event_reporter/database.rb', line 106 def parse_maths_input(instructions) if @cache.queue.empty? throw(:top, "Can't add/subtract from an empty queue, silly!") end attribute, criteria = instructions[0], instructions[1..-1].join(" ") validate_input(attribute, criteria) return attribute, criteria end |
#route_find(scope, instructions) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/event_reporter/database.rb', line 26 def route_find(scope, instructions) throw(:top, "Your database is empty (try 'load')") if @attendees.empty? @cache.queue = if instructions.include?("and") advanced_find(scope, instructions, "and") elsif instructions.include?("or") advanced_find(scope, instructions, "or") else initiate_search(scope, instructions) end @cache.queue_count("") # remove parameter after Frank's feedback end |
#route_queue(input) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/event_reporter/database.rb', line 10 def route_queue(input) command, leftovers = parse_input(input) if command == 'find' route_find('queue', leftovers) else @cache.route_queue(input) end end |
#search(scope, attribute, criteria) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/event_reporter/database.rb', line 85 def search(scope, attribute, criteria) db = scope == 'all' ? @attendees : @cache.queue db.select do |attendee| criteria.chomp.downcase == attendee[attribute].downcase end end |
#subtract(instructions) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/event_reporter/database.rb', line 92 def subtract(instructions) attribute, criteria = parse_maths_input(instructions) @cache.queue.delete_if do |attendee| attendee[attribute].downcase == criteria.downcase end @cache.queue_count("") end |
#validate_input(attribute, criterias) ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/event_reporter/database.rb', line 77 def validate_input(attribute, criterias) if not HEADERS.include? attribute throw(:top, "Attribute '#{attribute}' not found") elsif criterias.nil? || criterias.empty? throw(:top, "Please enter a find criteria") end end |