class CalendariumRomanum::SanctoraleWriter

Understands a custom plaintext calendar format and knows how to transform the {Celebration}s in a {Sanctorale} to this format.

For specification of the data format see {file:data/README.md} of the data directory, For a complete example see e.g. {file:universal-en.txt the file describing General Roman Calendar}.

@since 0.8.0

Constants

COLOUR_CODES

@api private

RANK_CODES

@api private

Public Class Methods

new(front_matter: true) click to toggle source

@param front_matter [Boolean] Should YAML front matter be written?

# File lib/calendarium-romanum/sanctorale_writer.rb, line 46
def initialize(front_matter: true)
  @write_front_matter = front_matter
end

Public Instance Methods

write(src, dest = nil) click to toggle source

Write to an object which understands +#<<+

@param src [Sanctorale]

source of the loaded data

@param dest [String, File, <<]

object to populate. If not provided, a new {String}
instance will be created and returned

@return [String]

# File lib/calendarium-romanum/sanctorale_writer.rb, line 58
def write(src, dest = nil)
  dest ||= String.new

  # Write metadata to YAML if present
  unless (!@write_front_matter) || src.metadata.nil? || src.metadata.empty?
    dest << src.metadata.to_yaml
    dest << "---\n"
  end

  # Write each celebration, grouped by month with headings
  current_month = 0
  src.each_day.sort_by{ |date, _| date }.each do |date, celebrations|
    if date.month > current_month
      current_month = date.month
      dest << "\n= #{current_month}\n"
    end

    celebrations.each do |c|
      dest << celebration_line(date, c)
      dest << "\n"
    end
  end

  dest
end
Also aliased as: write_to_string
write_to_file(sanctorale, filename, encoding = 'utf-8') click to toggle source

Write to a filesystem path

@param sanctorale [Sanctorale] @param filename [String] @param encoding [String] @return [void]

# File lib/calendarium-romanum/sanctorale_writer.rb, line 92
def write_to_file(sanctorale, filename, encoding = 'utf-8')
  File.open(filename, 'w', encoding: encoding) do |f|
    write(sanctorale, f)
  end
end
write_to_string(src, dest = nil)
Alias for: write

Private Instance Methods

celebration_line(date, celebration) click to toggle source

Convert a {Celebration} to a {String} for writing

# File lib/calendarium-romanum/sanctorale_writer.rb, line 101
def celebration_line(date, celebration)
  line = "#{date.day} "

  unless celebration.rank == Ranks::MEMORIAL_OPTIONAL
    code = RANK_CODES[celebration.rank]
    line << "#{code} "
  end

  unless celebration.colour == Colours::WHITE
    code = COLOUR_CODES[celebration.colour]
    line << "#{code} "
  end

  unless celebration.symbol.nil?
    line << "#{celebration.symbol} "
  end

  line << ': '
  line << celebration.title

  line
end