-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_handling.rb
47 lines (40 loc) · 1.42 KB
/
file_handling.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# frozen_string_literal: true
require 'date'
require 'csv'
require_relative 'stats'
include Stats
# This module will handle files it will command line argumnets and save them in an array format
module FileHandling
# This class saves all the data of files
class Collection
def initialize
@collection = []
end
def self.extract_file_names(choice, year_month, path)
case choice
when 'e'
Collection.new.filter_files(Dir["#{path}/**/*.txt"], year_month)
when 'a'
Collection.new.filter_files([] << "#{path}/#{Collection.new.appending_filename(year_month, path)}.txt", year_month)
when 'c'
Collection.new.filter_files([] << "#{path}/#{Collection.new.appending_filename(year_month, path)}.txt", year_month)
when 'b'
Collection.new.filter_files([] << "#{path}/#{Collection.new.appending_filename(year_month, path)}.txt", year_month)
end
end
def filter_files(files, year_month)
files.delete_if { |f| !f.include?(year_month) } if files.length != 1
extract_data(files)
end
def extract_data(files)
files.each do |f|
@collection << CSV.parse(File.read(f), headers: true)
end
@collection
end
def appending_filename(year_month, path)
appended = path.split('/')[-1] << '_' << year_month.split('/')[0] << '_'
appended << Date::ABBR_MONTHNAMES[year_month.split('/')[-1].to_i]
end
end
end