Class: CalendariumRomanum::SanctoraleLoader

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

Overview

understands a plaintext calendar format and knows how to transform it to Celebrations and fill them in a Sanctorale

Format of the file: 1/31 m : S. Ioannis Bosco, presbyteri

<month>/<day> <rank shortcut> : <title> rank shortcut is optional, default value is optional memorial

Constant Summary collapse

RANK_CODES =
{
  nil => Ranks::MEMORIAL_OPTIONAL,
  'm' => Ranks::MEMORIAL_GENERAL,
  'f' => Ranks::FEAST_GENERAL,
  's' => Ranks::SOLEMNITY_GENERAL
}.freeze
COLOUR_CODES =
{
  nil => Colours::WHITE,
  'w' => Colours::WHITE,
  'v' => Colours::VIOLET,
  'g' => Colours::GREEN,
  'r' => Colours::RED
}.freeze

Instance Method Summary collapse

Instance Method Details

#load(src, dest = nil) ⇒ Object Also known as: load_from_string

dest should be a Sanctorale, src anything with #each_line



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/calendarium-romanum/sanctorale_loader.rb', line 30

def load(src, dest = nil)
  dest ||= Sanctorale.new

  in_front_matter = false
  month_section = nil
  src.each_line.with_index(1) do |l, line_num|
    # skip YAML front matter
    if line_num == 1 && l.start_with?('---')
      in_front_matter = true
      next
    elsif in_front_matter
      if l.start_with?('---')
        in_front_matter = false
      end

      next
    end

    # strip whitespace and comments
    l.sub!(/#.*/, '')
    l.strip!
    next if l.empty?

    # month section heading
    n = l.match(/^=\s*(\d+)\s*$/)
    unless n.nil?
      month_section = n[1].to_i
      unless month_section >= 1 && month_section <= 12
        raise error("Invalid month #{month_section}", line_num)
      end
      next
    end

    begin
      celebration = load_line l, month_section
    rescue RangeError, RuntimeError => err
      raise error(err.message, line_num)
    end

    dest.add(
      celebration.date.month,
      celebration.date.day,
      celebration
    )
  end

  dest
end

#load_from_file(filename, dest = nil, encoding = 'utf-8') ⇒ Object



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

def load_from_file(filename, dest = nil, encoding = 'utf-8')
  load File.open(filename, 'r', encoding: encoding), dest
end