Class: Rabbit::Console

Inherits:
Object
  • Object
show all
Includes:
GetText
Defined in:
lib/rabbit/console.rb

Constant Summary collapse

@@locale_dir_option_name =
"--locale-dir"

Constants included from GetText

GetText::DOMAIN

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GetText

included

Constructor Details

#initialize(logger = nil) ⇒ Console

Returns a new instance of Console.


50
51
52
# File 'lib/rabbit/console.rb', line 50

def initialize(logger=nil)
  @logger = logger || Logger.default
end

Class Method Details

.get_last_name(klass) ⇒ Object


45
46
47
# File 'lib/rabbit/console.rb', line 45

def get_last_name(klass)
  klass.name.split("::").last
end

.parse!(args, logger = nil, &block) ⇒ Object


41
42
43
# File 'lib/rabbit/console.rb', line 41

def parse!(args, logger=nil, &block)
  new(logger).parse!(args, &block)
end

Instance Method Details

#parse!(args) ⇒ Object


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rabbit/console.rb', line 54

def parse!(args)
  options = OpenStruct.new
  options.logger = @logger
  options.default_logger = @logger
  options.druby_uri = "druby://127.0.0.1:10101"
  options.version = VERSION
  options.options_file = nil
  options.rest = []
  options.before_hooks = []
  options.after_hooks = []

  process_locale_options(args)

  parser = OptionParser.new(banner) do |_parser|
    yield(_parser, options)
    setup_common_options(_parser, options)
  end

  begin
    options_file = options.options_file
    if options_file and File.file?(options_file)
      read_options_file(parser, options, options_file)
    end
    options.before_hooks.each do |hook|
      hook.call(self, parser, options)
    end
    options.rest.concat(parser.parse!(args))
    options.after_hooks.each do |hook|
      hook.call(self, parser, options)
    end
  rescue => error
    @logger.error("#{error.class}: #{error.message}")
    error.backtrace.each do |line|
      @logger.error(line)
    end
    raise
  end

  [options, options.logger]
end

#read_options_file(parser, options, options_file) ⇒ Object


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/rabbit/console.rb', line 95

def read_options_file(parser, options, options_file)
  options_in_file = []
  File.open(options_file) do |file|
    file.each_line do |line|
      options_in_file.concat(Shellwords.split(line))
    end
  end
  source_info = parser.parse(options_in_file)

  source_info = source_info.collect do |path|
    if URI(path).scheme
      path
    else
      if Pathname(path).absolute?
        path
      else
        File.join(File.dirname(options_file), path)
      end
    end
  end
  options.rest.concat(source_info)
end