Class: CalendariumRomanum::PerpetualCalendar

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

Overview

Has mostly the same public interface as Calendar, but represents a “perpetual” calendar, not a calendar for a single year, thus allowing the client code to query for liturgical data of any day, without bothering about boundaries of liturgical years.

Internally builds Calendar instances as needed and delegates method calls to them.

Since:

  • 0.4.0

Instance Method Summary collapse

Constructor Details

#initialize(sanctorale: nil, temporale_factory: nil, temporale_options: nil, vespers: false, cache: {}) ⇒ PerpetualCalendar

Returns a new instance of PerpetualCalendar.

Parameters:

  • sanctorale (Sanctorale, nil) (defaults to: nil)
  • temporale_factory (Proc, nil) (defaults to: nil)

    Proc receiving a single parameter - year - and returning a Temporale instance.

  • temporale_options (Hash, nil) (defaults to: nil)

    Hash of arguments for Temporale#initialize. temporale_factory and temporale_options are mutually exclusive - pass either (or none) of them, never both.

  • vespers (Boolean) (defaults to: false)

    See argument of the same name to Calendar#initialize

  • cache (Hash) (defaults to: {})

    object to be used as internal cache of Calendar instances - anything exposing #[]= and #[] and “behaving mostly like a Hash” will work. There’s no need to pass it unless you want to have control over the cache. That may be sometimes useful in order to prevent a long-lived PerpetualCalendar instance flooding the memory by huge amount of cached Calendar instances. (By default, once a Calendar for a certain year is built, it is cached for the PerpetualCalendar instances’ lifetime.)

Since:

  • 0.4.0



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/calendarium-romanum/perpetual_calendar.rb', line 34

def initialize(sanctorale: nil, temporale_factory: nil, temporale_options: nil, vespers: false, cache: {})
  if temporale_factory && temporale_options
    raise ArgumentError.new('Specify either temporale_factory or temporale_options, not both')
  end

  @sanctorale = sanctorale
  @temporale_factory = temporale_factory || build_temporale_factory(temporale_options)
  @vespers = vespers

  @cache = cache
end

Instance Method Details

#[](arg) ⇒ Day+

Returns:

See Also:

Since:

  • 0.6.0



55
56
57
58
59
60
61
62
63
# File 'lib/calendarium-romanum/perpetual_calendar.rb', line 55

def [](arg)
  if arg.is_a? Range
    return arg.collect do |date|
      calendar_for(date).day(date)
    end
  end

  day(arg)
end

#calendar_for(*args) ⇒ Calendar

Returns a Calendar instance for the liturgical year containing the specified day

Parameters like Calendar#day

Returns:

Since:

  • 0.4.0



71
72
73
74
75
# File 'lib/calendarium-romanum/perpetual_calendar.rb', line 71

def calendar_for(*args)
  date = Calendar.mk_date(*args)
  year = Temporale.liturgical_year date
  calendar_instance year
end

#calendar_for_year(year) ⇒ Calendar

Returns a Calendar instance for the specified liturgical year

Parameters:

  • year (Integer)

Returns:

Since:

  • 0.4.0



81
82
83
# File 'lib/calendarium-romanum/perpetual_calendar.rb', line 81

def calendar_for_year(year)
  calendar_instance year
end

#day(*args) ⇒ Day

Returns:

See Also:

Since:

  • 0.4.0



48
49
50
# File 'lib/calendarium-romanum/perpetual_calendar.rb', line 48

def day(*args)
  calendar_for(*args).day(*args)
end