Class: ElchScan::Application

Inherits:
Object
  • Object
show all
Includes:
Dispatch, Filter
Defined in:
lib/elch_scan/application/dispatch.rb,
lib/elch_scan/application.rb

Defined Under Namespace

Modules: Dispatch

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Filter

#apply_filter, #filter_script, #permute_script, #record_filter

Methods included from Dispatch

#_index_movies, #collection_size_changed, #dispatch, #dispatch_edit_script, #dispatch_generate_config, #dispatch_index, #dispatch_info

Constructor Details

#initialize(env, argv) {|_self| ... } ⇒ Application

Returns a new instance of Application.

Yields:

  • (_self)

Yield Parameters:



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/elch_scan/application.rb', line 24

def initialize env, argv
  @env, @argv = env, argv
  @opts = {
    dispatch: :index,
    quiet: false,
    output_file: nil,
    formatter: "Plain",
    select_scripts: [],
  }
  yield(self)
end

Class Method Details

.dispatch(*a) ⇒ Object

Setup =



15
16
17
18
19
20
21
22
# File 'lib/elch_scan/application.rb', line 15

def self.dispatch *a
  new(*a) do |app|
    app.load_config "~/.elch_scan.yml"
    app.apply_config
    app.parse_params
    app.dispatch
  end
end

Instance Method Details

#apply_configObject



85
86
87
88
89
90
91
92
93
94
# File 'lib/elch_scan/application.rb', line 85

def apply_config
  logger.colorize = cfg(:application, :logger, :colorize)
  (cfg(:formatters) || []).each do |f|
    begin
      require File.expand_path(f)
    rescue LoadError
      abort "The custom formatter file wasn't found: " << c("#{f}", :magenta)
    end
  end
end

#ask(question) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/elch_scan/application.rb', line 117

def ask question
  logger.log_with_print(false) do
    log c("#{question} ", :blue)
    STDOUT.flush
    gets.chomp
  end
end

#c(str, color = :yellow) ⇒ Object

Shortcut for logger.colorize



113
114
115
# File 'lib/elch_scan/application.rb', line 113

def c str, color = :yellow
  logger.colorize? ? logger.colorize(str, color) : str
end

#cfg(*keys) ⇒ Object



96
97
98
99
# File 'lib/elch_scan/application.rb', line 96

def cfg *keys
  keys = keys.flatten.join(".").split(".")
  keys.inject(@config) {|cfg, skey| cfg.try(:[], skey) }
end

#load_config(file) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/elch_scan/application.rb', line 61

def load_config file
  @config_src = File.expand_path(file)
  @config = YAML.load_file(@config_src)
  raise "empty config" if !@config || @config.empty? || !@config.is_a?(Hash)
  @config = @config.with_indifferent_access
rescue Exception => e
  if e.message =~ /no such file or directory/i
    if @argv.include?("--generate-config")
      @config = { application: { logger: { colorize: true } } }.with_indifferent_access
    else
      log "Please create or generate a configuration file."
      log(
        c("Use ") << c("elch_scan --generate-config", :magenta) <<
        c(" or create ") << c("~/.elch_scan.yml", :magenta) << c(" manually.")
      )
      abort "No configuration file found.", 1
    end
  elsif e.message =~ //i
    abort "Configuration file is invalid.", 1
  else
    raise
  end
end

#loggerObject



108
109
110
# File 'lib/elch_scan/application.rb', line 108

def logger
  Thread.main.app_logger
end

#parse_paramsObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/elch_scan/application.rb', line 36

def parse_params
  @optparse = OptionParser.new do |opts|
    opts.banner = "Usage: elch_scan [options]"

    opts.on("--generate-config", "Generate sample configuration file in ~/.elch_scan.yml") { @opts[:dispatch] = :generate_config }
    opts.on("-h", "--help", "Shows this help") { @opts[:dispatch] = :help }
    opts.on("-v", "--version", "Shows version and other info") { @opts[:dispatch] = :info }
    opts.on("-f", "--formatter HTML", "Use formatter") {|f| @opts[:formatter] = f }
    opts.on("-o", "--output FILENAME", "Write formatted results to file") {|f| @opts[:output_file] = f }
    opts.on("-e", "--edit SELECT_SCRIPT", "Edit selector script") {|s| @opts[:dispatch] = :edit_script; @opts[:select_script] = s }
    opts.on("-s", "--select [WITH_SUBS,NO_NFO]", Array, "Filter movies with saved selector scripts") {|s| @opts[:select_scripts] = s }
    opts.on("-p", "--permute", "Open editor to write permutation code for collection") {|s| @opts[:permute] = true }
    opts.on("-q", "--quiet", "Don't ask to filter or save results") { @opts[:quiet] = true }
    opts.on("-c", "--console", "Start console to play around with the collection") {|f| @opts[:console] = true }
  end

  begin
    @optparse.parse!(@argv)
  rescue OptionParser::ParseError => e
    abort(e.message)
    dispatch(:help)
    exit 1
  end
end