Class: Crucigrama::CLI::Print

Inherits:
Object
  • Object
show all
Defined in:
lib/crucigrama/cli/print.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Print

Returns a new instance of Print.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/crucigrama/cli/print.rb', line 49

def initialize(*args)
  extract_options(*args)
  crossword = begin
    Crucigrama::Crossword.new(MultiJson.decode(options[:json_crossword]))
  rescue Exception => exc
    STDERR.puts exc.message
    STDERR.puts "Could not parse input crossword!"
    exit 1
  end
  begin
    crossword.to_pdf(options[:output_file], :include_solution => options[:include_solution])
  rescue Exception => exc
    STDERR.puts exc.message
    STDERR.puts "Could not print crossword!"
    exit 1
  end
end

Instance Method Details

#extract_options(*args) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/crucigrama/cli/print.rb', line 36

def extract_options(*args)
  @options = {}
  options_parser.parse!(args)
  unless options[:json_crossword]
    STDERR.puts "Must specify input file!"
    exit 1
  end
  unless options[:output_file] 
    STDERR.puts "Must specify output file!"
    exit 1
  end
end

#options_parserObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/crucigrama/cli/print.rb', line 6

def options_parser
  @options_parser ||= OptionParser.new do |opts|
    opts.banner = "Usage: #{Crucigrama::CLI.cli_command} print options"
    opts.separator ""
    opts.separator "Options:"
    opts.on("-o", "--output PDF_FILE", "Prints the crossword to PDF_FILE") do |file|
      @options[:output_file] = file
    end
    opts.on("-i", "--input JSON_FILE", "Extracts the crossword from the JSON_FILE file") do |file|
      begin
        @options[:json_crossword] = File.read(file)
      rescue Exception => exc
        STDERR.puts exc.message
      end
    end
    opts.on("--[no-]solution", "Prints a reduced and inverted solution for the crossword next to it") do |value|
      @options[:include_solution] = value
    end
    opts.separator ""
    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end
    opts.on_tail("--version", "Show version") do
      puts Crucigrama::VERSION
      exit
    end
  end
end