Class: CalendariumRomanum::SanctoraleLoader

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

Overview

Understands a custom plaintext calendar format and knows how to transform it to Celebrations and fill them in a Sanctorale.

For specification of the data format see README of the data directory, For a complete example see e.g. the file describing General Roman Calendar.

Constant Summary collapse

RANK_CODES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  nil => Ranks::MEMORIAL_OPTIONAL, # default
  'm' => Ranks::MEMORIAL_GENERAL,
  'mp' => Ranks::MEMORIAL_PROPER,
  'f' => Ranks::FEAST_GENERAL,
  'fl' => Ranks::FEAST_LORD_GENERAL,
  'fp' => Ranks::FEAST_PROPER,
  's' => Ranks::SOLEMNITY_GENERAL,
  'sp' => Ranks::SOLEMNITY_PROPER,
}.freeze
COLOUR_CODES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  nil => Colours::WHITE, # default
  'w' => Colours::WHITE,
  'v' => Colours::VIOLET,
  'g' => Colours::GREEN,
  'r' => Colours::RED
}.freeze

Instance Method Summary collapse

Instance Method Details

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

Load from an object which understands #each_line

Parameters:

  • src (String, File, #each_line)

    source of the loaded data

  • dest (Sanctorale, nil) (defaults to: nil)

    objects to populate. If not provided, a new CalendariumRomanum::Sanctorale instance will be created

Returns:

Raises:


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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/calendarium-romanum/sanctorale_loader.rb', line 44

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

  in_front_matter = false
  front_matter = ''
  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
      front_matter += l
      next
    elsif in_front_matter
      if l.start_with?('---')
        in_front_matter = false
        dest. = YAML.load(front_matter).freeze
      end

      front_matter += l

      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') ⇒ Sanctorale

Load from a filesystem path

Parameters:

  • filename (String)
  • dest (Sanctorale, nil) (defaults to: nil)
  • encoding (String) (defaults to: 'utf-8')

Returns:

Raises:


107
108
109
# File 'lib/calendarium-romanum/sanctorale_loader.rb', line 107

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