Class: Pslm::PsalmPointer

Inherits:
Object
  • Object
show all
Defined in:
lib/pslm/psalmpointer.rb

Overview

Workhorse of the pslm CLI utility.

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ PsalmPointer

Returns a new instance of PsalmPointer.



8
9
10
11
12
13
14
15
16
17
# File 'lib/pslm/psalmpointer.rb', line 8

def initialize(options)
  @options = options

  # make @options a StructuredSetup - because of deep #dup and recursive #update
  unless @options.is_a? StructuredSetup
    @options = StructuredSetup.new @options
  end

  @reader = PslmReader.new
end

Instance Method Details

#process(psalm_files, output_file = nil, overwr_options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
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
# File 'lib/pslm/psalmpointer.rb', line 19

def process(psalm_files, output_file=nil, overwr_options={})
  options = @options.dup
  options.update overwr_options

  if output_file != nil then
    outf = File.open output_file, 'w'
  else
    outf = STDOUT
  end

  unless psalm_files.is_a? Array
    psalm_files = [ psalm_files ]
  end

  psalms = psalm_files.collect do |pf|
    inf = open_psalm_file(pf)
    if options[:input][:append] then
      inf = JoinedInput.new inf, StringIO.new(options[:input][:append])
    end
    @reader.load_psalm(inf)
  end

  if options[:input][:join] then
    while p = psalms.slice!(1) do
      psalms[0] += p
    end
  end

  if options[:input][:title] then
    psalms.first.header.title = options[:input][:title] # should it be set for all instead of for the first one only?
  end

  outputter = get_outputter options[:general][:format]
  psalms.each do |ps|
    # yield the psalm before producing output to allow modification
    modified = yield ps if block_given?
    if modified != ps and modified.is_a?(Psalm) then
      ps = modified
    end

    outf.puts outputter.process_psalm ps, options[:output]
  end

  if outf != STDOUT then
    outf.close
  end
end