Class: Storage
- Inherits:
-
Object
- Object
- Storage
- Defined in:
- worklog/storage.rb
Defined Under Namespace
Classes: LogNotFoundError
Constant Summary collapse
- FILE_SUFFIX =
'.yaml'
Instance Method Summary collapse
-
#all_days ⇒ Array<DailyLog>
Return all days with logs.
-
#create_file_skeleton(date) ⇒ Object
Create file for a new day if it does not exist.
-
#create_folder ⇒ Object
Create folder if not exists already.
-
#days_between(start_date, end_date = nil, epics_only = nil, tags_filter = nil) ⇒ Object
Return days between start_date and end_date If end_date is nil, return logs from start_date to today.
-
#filepath(date) ⇒ String
Construct filepath for a given date.
- #folder_exists? ⇒ Boolean
-
#initialize(config) ⇒ Storage
constructor
A new instance of Storage.
- #load_log(file) ⇒ Object
- #load_log!(file) ⇒ Object
- #load_people ⇒ Object
-
#load_people! ⇒ Array<Person>
Load all people from the people file.
- #load_single_log_file(file, headline = true) ⇒ Object
- #write_log(file, daily_log) ⇒ Object
-
#write_people!(people) ⇒ Object
Write people to the people file.
Constructor Details
#initialize(config) ⇒ Storage
Returns a new instance of Storage.
15 16 17 |
# File 'worklog/storage.rb', line 15 def initialize(config) @config = config end |
Instance Method Details
#all_days ⇒ Array<DailyLog>
Return all days with logs
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'worklog/storage.rb', line 25 def all_days return [] unless folder_exists? logs = [] Dir.glob(File.join(@config.storage_path, "*#{FILE_SUFFIX}")).map do |file| next if file.end_with?('people.yaml') logs << load_log(file) end logs end |
#create_file_skeleton(date) ⇒ Object
Create file for a new day if it does not exist
73 74 75 76 77 |
# File 'worklog/storage.rb', line 73 def create_file_skeleton(date) create_folder File.write(filepath(date), YAML.dump(DailyLog.new(date:, entries: []))) unless File.exist?(filepath(date)) end |
#create_folder ⇒ Object
Create folder if not exists already.
143 144 145 |
# File 'worklog/storage.rb', line 143 def create_folder Dir.mkdir(@config.storage_path) unless Dir.exist?(@config.storage_path) end |
#days_between(start_date, end_date = nil, epics_only = nil, tags_filter = nil) ⇒ Object
Return days between start_date and end_date If end_date is nil, return logs from start_date to today
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'worklog/storage.rb', line 45 def days_between(start_date, end_date = nil, epics_only = nil, = nil) return [] unless folder_exists? logs = [] end_date = Date.today if end_date.nil? return [] if start_date > end_date while start_date <= end_date if File.exist?(filepath(start_date)) tmp_logs = load_log!(filepath(start_date)) tmp_logs.entries.keep_if { |entry| entry.epic? } if epics_only if # Safeguard against entries without any tags, not just empty array tmp_logs.entries.keep_if { |entry| entry. && (entry. & ).size > 0 } end logs << tmp_logs if tmp_logs.entries.length > 0 end start_date += 1 end logs end |
#filepath(date) ⇒ String
Construct filepath for a given date.
150 151 152 |
# File 'worklog/storage.rb', line 150 def filepath(date) File.join(@config.storage_path, "#{date}#{FILE_SUFFIX}") end |
#folder_exists? ⇒ Boolean
19 20 21 |
# File 'worklog/storage.rb', line 19 def folder_exists? Dir.exist?(@config.storage_path) end |
#load_log(file) ⇒ Object
79 80 81 82 83 84 |
# File 'worklog/storage.rb', line 79 def load_log(file) load_log!(file) rescue LogNotFoundError WorkLogger.error "No work log found for #{file}. Aborting." nil end |
#load_log!(file) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'worklog/storage.rb', line 86 def load_log!(file) WorkLogger.debug "Loading file #{file}" begin log = YAML.load_file(file, permitted_classes: [Date, Time, DailyLog, LogEntry]) log.entries.each do |entry| entry.time = Time.parse(entry.time) unless entry.time.respond_to?(:strftime) end log rescue Errno::ENOENT raise LogNotFoundError end end |
#load_people ⇒ Object
115 116 117 118 119 120 |
# File 'worklog/storage.rb', line 115 def load_people load_people! rescue Errno::ENOENT WorkLogger.info 'Unable to load people.' [] end |
#load_people! ⇒ Array<Person>
Load all people from the people file
124 125 126 127 128 129 |
# File 'worklog/storage.rb', line 124 def load_people! people_file = File.join(@config.storage_path, 'people.yaml') return [] unless File.exist?(people_file) YAML.load_file(people_file, permitted_classes: [Person]) end |
#load_single_log_file(file, headline = true) ⇒ Object
109 110 111 112 113 |
# File 'worklog/storage.rb', line 109 def load_single_log_file(file, headline = true) daily_log = load_log!(file) puts "Work log for #{Rainbow(daily_log.date).gold}:" if headline daily_log.entries end |
#write_log(file, daily_log) ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'worklog/storage.rb', line 99 def write_log(file, daily_log) create_folder WorkLogger.debug "Writing to file #{file}" File.open(file, 'w') do |f| f.puts daily_log.to_yaml end end |
#write_people!(people) ⇒ Object
Write people to the people file
133 134 135 136 137 138 139 140 |
# File 'worklog/storage.rb', line 133 def write_people!(people) create_folder people_file = File.join(@config.storage_path, 'people.yaml') File.open(people_file, 'w') do |f| f.puts people.to_yaml end end |