Class: CalendariumRomanum::Util::DateParser

Inherits:
Object
  • Object
show all
Defined in:
lib/calendarium-romanum/util.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(date_str) ⇒ DateParser

Returns a new instance of DateParser.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/calendarium-romanum/util.rb', line 42

def initialize(date_str)
  if date_str =~ /((\d{4})(\/|-)?(\d{0,2})(\/|-)?(\d{0,2}))\z/ # Accepts YYYY-MM-DD, YYYY/MM/DD where both day and month are optional
    year = Regexp.last_match(2).to_i
    month = Regexp.last_match(4).to_i
    day = Regexp.last_match(6).to_i
    @date_range = if (day == 0) && (month == 0) # Only year is given
                    Year.new(year)
                  elsif day == 0 # Year and month are given
                    Month.new(year, month)
                  else
                    Date.new(year, month, day)..Date.new(year, month, day)
                  end
  else
    raise ArgumentError, 'Unparseable date'
  end
end

Instance Attribute Details

#date_rangeObject (readonly)

Returns the value of attribute date_range.



41
42
43
# File 'lib/calendarium-romanum/util.rb', line 41

def date_range
  @date_range
end