Class: Crucigrama::CLI::Generate

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

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Generate

Returns a new instance of Generate.



61
62
63
64
65
# File 'lib/crucigrama/cli/generate.rb', line 61

def initialize(*args)
  extract_options(*args)
  crossword = generate_crossword
  dump_crossword(crossword)
end

Instance Method Details

#extract_options(*args) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/crucigrama/cli/generate.rb', line 49

def extract_options(*args)
  @options = {}
  options_parser.parse!(args)
  unless options[:lemary] 
    STDERR.puts "Must specify lemary file!"
    exit 1
  end
  if @options[:allow_reverse]
    @options[:lemary] = @options[:lemary].collect{|word| [word, word.reverse]}.flatten.uniq
  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
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/crucigrama/cli/generate.rb', line 6

def options_parser
  @options_parser ||= OptionParser.new do |opts|
    opts.banner = "Usage: #{Crucigrama::CLI.cli_command} generate options"
    opts.separator ""
    opts.separator "Options:"
    opts.on("-l", "--lemary LEMARY", "Uses LEMARY as words source") do |lemary|
      begin
        @options[:lemary] = File.read(lemary).split("\n")
      rescue Exception => exc
        STDERR.puts exc.message
      end 
    end
    opts.on("-d", "--dimensions DIMENSIONS", "Generates crossword of DIMENSIONS size",
                                             "in nxn format (10x10 by default)") do |dimensions|
      unless match = /(\d)+x(\d)+/.match(dimensions)
        STDERR.puts "Incorrect dimensions format, it must be nxn (for example 10x11)"
        exit 1
      end                                        
      @options[:dimensions] = Hash[[:horizontal, :vertical].collect.with_index do |direction, i|
        [direction, match[i].to_i]
      end]
    end
    opts.on("-o", "--output FILE", "Dumps the generated crossword to FILE") do |file|
      @options[:file] = file
    end
    opts.on("--[no-]definitions", "Asks for definitions for the generated crossword") do |definitions|
      @options[:definitions] = definitions
    end
    opts.on("--[no-]reverse-words", "Allow reverse words for the generated crossword") do |value|
      @options[:allow_reverse] = 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