Class: Ace::Docs::Atoms::TimeRangeCalculator
- Inherits:
-
Object
- Object
- Ace::Docs::Atoms::TimeRangeCalculator
- Defined in:
- lib/ace/docs/atoms/time_range_calculator.rb
Overview
Pure function for time range calculations
Class Method Summary collapse
-
.calculate_since(date) ⇒ String
Calculate a git-compatible “since” string from a date.
-
.format_human(date) ⇒ String
Format a date for human-readable display.
-
.parse_date(date_string) ⇒ Date
Parse a date from various formats.
Class Method Details
.calculate_since(date) ⇒ String
Calculate a git-compatible “since” string from a date
15 16 17 18 19 20 21 22 |
# File 'lib/ace/docs/atoms/time_range_calculator.rb', line 15 def calculate_since(date) return date if date.is_a?(String) && date.match?(/^\d+\s+(days?|weeks?|months?)\s+ago$/) parsed_date = parse_date(date) days_ago = (Date.today - parsed_date.to_date).to_i format_days_ago(days_ago) end |
.format_human(date) ⇒ String
Format a date for human-readable display
56 57 58 59 60 61 |
# File 'lib/ace/docs/atoms/time_range_calculator.rb', line 56 def format_human(date) parsed_date = parse_date(date) days_ago = (Date.today - parsed_date).to_i format_days_ago(days_ago) end |
.parse_date(date_string) ⇒ Date
Parse a date from various formats
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/ace/docs/atoms/time_range_calculator.rb', line 27 def parse_date(date_string) return date_string if date_string.is_a?(Date) return date_string.to_date if date_string.is_a?(Time) # Handle various string formats case date_string when /^today$/i Date.today when /^yesterday$/i Date.today - 1 when /^(\d+)\s+days?\s+ago$/i Date.today - Regexp.last_match(1).to_i when /^(\d+)\s+weeks?\s+ago$/i Date.today - (Regexp.last_match(1).to_i * 7) when /^(\d+)\s+months?\s+ago$/i Date.today << Regexp.last_match(1).to_i when /^\d{4}-\d{2}-\d{2}$/ Date.parse(date_string) else # Try to parse with Date.parse as fallback Date.parse(date_string) end rescue ArgumentError => e raise ArgumentError, "Invalid date format: #{date_string}. Error: #{e.}" end |