Class: DailyLog::LatestDate

Inherits:
Object
  • Object
show all
Defined in:
lib/daily_log/latest_date.rb

Overview

Finds the latest past Date that we have an Entry for in the local filesystem

Constant Summary collapse

DATE_PATH_MATCHER =

Match a file path to extract a date component

/(?<year>\d{4})\/(?<month>\d{2})\/(?<day>\d{2})/

Instance Method Summary collapse

Instance Method Details

#findObject

The latest entry in the local file system

Returns Date Returns nil



14
15
16
17
18
19
20
21
22
# File 'lib/daily_log/latest_date.rb', line 14

def find
  return nil unless entry_paths.any?
  past_entries = entry_paths.map do |path|
    match = path.match(DATE_PATH_MATCHER)
    Date.parse(match.to_s)
  end.select { |date| date < today }

  past_entries.last
end

#to_dateObject

Return the Date as a Date object



31
32
33
# File 'lib/daily_log/latest_date.rb', line 31

def to_date
  find
end

#to_sObject

Convert the Date to a String Returns String



26
27
28
# File 'lib/daily_log/latest_date.rb', line 26

def to_s
  find.to_s.strftime(Day::DATE_FORMAT)
end