class CalendariumRomanum::SanctoraleFactory

Utility loading {Sanctorale} from several sources and building a single {Sanctorale} by layering them over each other.

Public Class Methods

create_layered(*instances) click to toggle source

Takes several {Sanctorale} instances, returns a new one, created by merging them all together (using {Sanctorale#update})

@return [Sanctorale]

@example

include CalendariumRomanum

prague_sanctorale = SanctoraleFactory.create_layered(
  Data['czech-cs'].load, # Czech Republic
  Data['czech-cechy-cs'].load, # Province of Bohemia
  Data['czech-praha-cs'].load, # Archdiocese of Prague
)
# File lib/calendarium-romanum/sanctorale_factory.rb, line 21
def create_layered(*instances)
  r = Sanctorale.new
  instances.each {|i| r.update i }

  metadata = instances
               .collect(&:metadata)
               .select {|i| i.is_a? Hash }
  r.metadata = metadata.inject((metadata.first || {}).dup) {|merged,i| merged.update i }
  r.metadata.delete 'extends'
  r.metadata['components'] = instances.collect(&:metadata)

  r
end
load_layered_from_files(*paths) click to toggle source

Takes several filesystem paths, loads a {Sanctorale} from each of them (using {SanctoraleLoader}) and then merges them (using {.create_layered})

@return [Sanctorale]

@example

include CalendariumRomanum

my_sanctorale = SanctoraleFactory.load_layered_from_files(
  'data/czech-cs.txt',
  'data/czech-cechy-cs.txt'
)
# File lib/calendarium-romanum/sanctorale_factory.rb, line 48
def load_layered_from_files(*paths)
  loader = SanctoraleLoader.new
  instances = paths.collect do |p|
    loader.load_from_file p
  end
  create_layered(*instances)
end
load_with_parents(path) click to toggle source

Takes a single filesystem path. If the file's YAML front matter references any parent data files using the 'extends' key, it loads all the parents and assembles the resulting {Sanctorale}. If the data file doesn't reference any parents, result is the same as {SanctoraleLoader#load_from_file}.

@return [Sanctorale] @since 0.7.0

# File lib/calendarium-romanum/sanctorale_factory.rb, line 65
def load_with_parents(path)
  loader = SanctoraleLoader.new

  hierarchy = load_parent_hierarchy(path, loader)
  return hierarchy.first if hierarchy.size == 1

  create_layered *hierarchy
end

Private Class Methods

load_parent_hierarchy(path, loader) click to toggle source
# File lib/calendarium-romanum/sanctorale_factory.rb, line 76
def load_parent_hierarchy(path, loader)
  main = loader.load_from_file path
  return [main] unless main.metadata.is_a?(Hash) && main.metadata.has_key?('extends')

  to_merge = [main]
  parents = main.metadata['extends']
  parents = [parents] unless parents.is_a? Array
  parents.reverse.each do |parent_path|
    expanded_path = File.expand_path parent_path, File.dirname(path)
    subtree = load_parent_hierarchy(expanded_path, loader)
    to_merge = subtree + to_merge
  end

  to_merge
end