Class: StructuredStore::Converters::ChronicDateRangeConverter
- Inherits:
-
Object
- Object
- StructuredStore::Converters::ChronicDateRangeConverter
- Defined in:
- lib/structured_store/converters/chronic_date_range_converter.rb
Overview
This class is responsible for converting date ranges to and from a string format.
Instance Method Summary collapse
-
#convert_to_dates(value) ⇒ Array<Time>
Converts a natural language date range string into an array containing start and end dates.
-
#convert_to_string(date1, date2) ⇒ String
Formats two dates into a human readable date range string.
Instance Method Details
#convert_to_dates(value) ⇒ Array<Time>
Converts a natural language date range string into an array containing start and end dates
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/structured_store/converters/chronic_date_range_converter.rb', line 14 def convert_to_dates(value) return [nil, nil] if value.blank? if /\A\d{4}\z/.match?(value.strip) # If the value is a year, return the start and end of that year year = value.strip.to_i return [Time.new(year, 1, 1), Time.new(year, 12, 31)] end parsed_date_range = Chronic.parse(value, endian_precedence: :little, guess: false) [parsed_date_range&.begin, parsed_date_range&.end&.days_ago(1)] end |
#convert_to_string(date1, date2) ⇒ String
Formats two dates into a human readable date range string
42 43 44 45 46 47 48 |
# File 'lib/structured_store/converters/chronic_date_range_converter.rb', line 42 def convert_to_string(date1, date2) return format_single_date(date1) if date1 == date2 return format_full_month(date1) if full_month_range?(date1, date2) return format_full_year(date1) if full_year_range?(date1, date2) format_date_range(date1, date2) end |