Class: CalendariumRomanum::Sanctorale
- Inherits:
-
Object
- Object
- CalendariumRomanum::Sanctorale
- Defined in:
- lib/calendarium-romanum/sanctorale.rb
Overview
One of the two main Calendar components. Contains celebrations with fixed date, mostly feasts of saints.
Basically a mapping AbstractDate => Array<Celebration> additionally enforcing some constraints:
-
for a given AbstractDate there may be multiple Celebrations, but only if all of them are in the rank of an optional memorial
-
Celebration#symbol must be unique in the whole set of contained celebrations
Instance Attribute Summary collapse
-
#metadata ⇒ Hash?
Sanctorale metadata.
-
#solemnities ⇒ Hash<AbstractDate=>Celebration>
readonly
Content subset - only Celebrations in the rank(s) of solemnity.
Instance Method Summary collapse
- #==(b) ⇒ Boolean
-
#[](date) ⇒ Array<Celebration>
Retrieves Celebrations for the given date.
-
#add(month, day, celebration) ⇒ void
Adds a new Celebration.
-
#each_day {|AbstractDate, Array<Celebration>| ... } ⇒ void, Enumerator
Enumerates dates for which any Celebrations are available.
-
#empty? ⇒ Boolean
It is empty if it doesn’t contain any Celebration.
-
#freeze ⇒ Object
Freezes the instance.
-
#get(*args) ⇒ Array<Celebration>
Retrieves Celebrations for the given date.
-
#initialize ⇒ Sanctorale
constructor
A new instance of Sanctorale.
-
#replace(month, day, celebrations, symbol_uniqueness: true) ⇒ void
Replaces content of the given day by given Celebrations.
-
#size ⇒ Fixnum
Returns count of days with Celebrations filled.
-
#update(other) ⇒ void
Updates the receiver with Celebrations from another instance.
Constructor Details
#initialize ⇒ Sanctorale
Returns a new instance of Sanctorale.
18 19 20 21 22 23 |
# File 'lib/calendarium-romanum/sanctorale.rb', line 18 def initialize @days = {} @solemnities = {} @symbols = Set.new @metadata = nil end |
Instance Attribute Details
#metadata ⇒ Hash?
Sanctorale metadata.
Data files may contain YAML front matter. If provided, it’s loaded by CalendariumRomanum::SanctoraleLoader and stored in this property. All data files bundled in the gem (see Data) have YAML front matter which is a Hash with a few standardized keys. While YAML also supports top-level content of other types, sanctorale data authors should stick to the convention of using Hash as the top-level data structure of their front matters.
44 45 46 |
# File 'lib/calendarium-romanum/sanctorale.rb', line 44 def @metadata end |
#solemnities ⇒ Hash<AbstractDate=>Celebration> (readonly)
Content subset - only Celebrations in the rank(s) of solemnity.
28 29 30 |
# File 'lib/calendarium-romanum/sanctorale.rb', line 28 def solemnities @solemnities end |
Instance Method Details
#==(b) ⇒ Boolean
204 205 206 207 |
# File 'lib/calendarium-romanum/sanctorale.rb', line 204 def ==(b) self.class == b.class && days == b.days end |
#[](date) ⇒ Array<Celebration>
Retrieves Celebrations for the given date
143 144 145 146 |
# File 'lib/calendarium-romanum/sanctorale.rb', line 143 def [](date) adate = date.is_a?(AbstractDate) ? date : AbstractDate.from_date(date) @days[adate] || [] end |
#add(month, day, celebration) ⇒ void
This method returns an undefined value.
Adds a new Celebration
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/calendarium-romanum/sanctorale.rb', line 54 def add(month, day, celebration) date = AbstractDate.new(month, day) unless @days[date].nil? || @days[date].empty? present = @days[date][0] if present.rank != Ranks::MEMORIAL_OPTIONAL raise ArgumentError.new("On #{date} there is already a #{present.rank}. No more celebrations can be added.") elsif celebration.rank != Ranks::MEMORIAL_OPTIONAL raise ArgumentError.new("Celebration of rank #{celebration.rank} cannot be grouped, but there is already another celebration on #{date}") end end unless celebration.symbol.nil? if @symbols.include? celebration.symbol raise ArgumentError.new("Attempted to add Celebration with duplicate symbol #{celebration.symbol.inspect}") end @symbols << celebration.symbol end unless @days.has_key? date @days[date] = [] end if celebration.solemnity? @solemnities[date] = celebration end @days[date] << celebration end |
#each_day {|AbstractDate, Array<Celebration>| ... } ⇒ void, Enumerator
Enumerates dates for which any Celebrations are available
172 173 174 175 176 177 178 |
# File 'lib/calendarium-romanum/sanctorale.rb', line 172 def each_day return to_enum(__method__) unless block_given? @days.each_pair do |date, celebrations| yield date, celebrations end end |
#empty? ⇒ Boolean
It is empty if it doesn’t contain any Celebration
190 191 192 |
# File 'lib/calendarium-romanum/sanctorale.rb', line 190 def empty? @days.empty? end |
#freeze ⇒ Object
Freezes the instance
195 196 197 198 199 200 |
# File 'lib/calendarium-romanum/sanctorale.rb', line 195 def freeze @days.freeze @days.values.each(&:freeze) @solemnities.freeze super end |
#get(date) ⇒ Array<Celebration> #get(month, day) ⇒ Array<Celebration>
Retrieves Celebrations for the given date
156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/calendarium-romanum/sanctorale.rb', line 156 def get(*args) if args.size == 1 && args[0].is_a?(Date) month = args[0].month day = args[0].day else month, day = args end date = AbstractDate.new(month, day) self[date] end |
#replace(month, day, celebrations, symbol_uniqueness: true) ⇒ void
This method returns an undefined value.
Replaces content of the given day by given Celebrations
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/calendarium-romanum/sanctorale.rb', line 96 def replace(month, day, celebrations, symbol_uniqueness: true) date = AbstractDate.new(month, day) symbols_without_day = @symbols unless @days[date].nil? old_symbols = @days[date].collect(&:symbol).compact symbols_without_day = @symbols - old_symbols end new_symbols = celebrations.collect(&:symbol).compact duplicate = symbols_without_day.intersection new_symbols if symbol_uniqueness && !duplicate.empty? raise ArgumentError.new("Attempted to add Celebrations with duplicate symbols #{duplicate.to_a.inspect}") end @symbols = symbols_without_day @symbols.merge new_symbols if celebrations.first.solemnity? @solemnities[date] = celebrations.first elsif @solemnities.has_key? date @solemnities.delete date end @days[date] = celebrations.dup end |
#size ⇒ Fixnum
Returns count of days with Celebrations filled
183 184 185 |
# File 'lib/calendarium-romanum/sanctorale.rb', line 183 def size @days.size end |
#update(other) ⇒ void
This method returns an undefined value.
Updates the receiver with Celebrations from another instance.
For each date contained in other
the content of self
is replaced by that of other
.
131 132 133 134 135 136 |
# File 'lib/calendarium-romanum/sanctorale.rb', line 131 def update(other) other.each_day do |date, celebrations| replace date.month, date.day, celebrations, symbol_uniqueness: false end rebuild_symbols end |