Class: Pslm::ConsoleOutputter

Inherits:
Outputter show all
Defined in:
lib/pslm/consoleoutputter.rb

Overview

formats a psalm for console output

Defined Under Namespace

Classes: Formatter, PartsFormatter, PointingFormatter, SkipVersesFormatter, TitleFormatter

Constant Summary collapse

FORMATTER_ORDER =
[
  :pointing,
  :parts,
  :verses,
  :skip_verses,
  :title,
  :strophes,
  :prepend_text,
  :output_append_text,
  :line_break_last_line,
  :mark_short_verses
]

Constants inherited from Outputter

Outputter::DEFAULT_SETUP

Instance Method Summary collapse

Instance Method Details

#get_formatter(sym, options) ⇒ Object

takes a Symbol - name of a configuration option, and option/s value; returns an instance of a corresponding Formatter class or nil



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/pslm/consoleoutputter.rb', line 76

def get_formatter(sym, options)
  cls_name = sym.to_s.gsub(/_(\w)/) {|m| m[1].upcase }
  cls_name[0] = cls_name[0].upcase
  cls_name += 'Formatter'

  if self.class.const_defined? cls_name then
    return self.class.const_get(cls_name).new(options)
  else
    return nil
  end
end

#get_formatters(options) ⇒ Object

takes a Hash of options, returns a list of accordingly initialized Formatter instances



66
67
68
69
70
71
72
# File 'lib/pslm/consoleoutputter.rb', line 66

def get_formatters(options)
  return FORMATTER_ORDER.collect do |f|
    next unless options.include? f

    get_formatter(f, options[f])
  end.compact!
end

#process_psalm(psalm, opts = {}) ⇒ Object Also known as: process

takes a Psalm, returns a String with the psalm formatted



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pslm/consoleoutputter.rb', line 24

def process_psalm(psalm, opts={})
  formatters = get_formatters(opts)

  # build the output; on each step apply the appropriate method
  # of each formatter in the given order
  psalm_assembled = psalm.verses.collect do |verse|
    process_verse(verse, opts, psalm, formatters)
  end.delete_if {|v| v == '' }.join "\n"

  return Formatter.format(formatters, :psalm,
                          psalm_assembled,
                          psalm)
end

#process_verse(verse, opts, psalm = nil, formatters = nil) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/pslm/consoleoutputter.rb', line 40

def process_verse(verse, opts, psalm=nil, formatters=nil)
  formatters = formatters || get_formatters(opts)

  verse_assembled = verse.parts.collect do |part|

    part_assembled = part.words.reverse.collect do |word|

      word_assembled = word.syllables.reverse.collect do |syll|
        Formatter.format(formatters, :syllable,
                          syll, part, word, syll)
      end.reverse.join ''

      Formatter.format(formatters, :word,
                        word_assembled, word)
    end.reverse.join ' '

    Formatter.format(formatters, :part,
                      part_assembled, part)
  end.join ''

  return Formatter.format(formatters, :verse,
                          verse_assembled, psalm, verse)
end