Method: ElchScan::Application::Dispatch#dispatch_index

Defined in:
lib/elch_scan/application/dispatch.rb

#dispatch_indexObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/elch_scan/application/dispatch.rb', line 81

def dispatch_index
  if cfg(:movies).empty?
    log "You will need at least 1 one movie directory defined in your configuration."
    answer = ask("Do you want to open the file now? [Yes/no]")
    if ["", "y", "yes"].include?(answer.downcase)
      if RUBY_PLATFORM.include?("darwin")
        exec("open #{@config_src}")
      else
        system "#{cfg :application, :editor} #{@config_src}"
      end
    end
  else
    movies = _index_movies(cfg(:movies))
    old_count = movies.count
    return log("No movies found :(") if old_count.zero?
    log(
      "We have found " << c("#{movies.count}", :magenta) <<
      c(" movies in ") << c("#{cfg(:movies).count}", :magenta) << c(" directories")
    )

    if @opts[:console]
      log "You have access to the collection with " << c("movies", :magenta)
      log "Apply existent select script with " << c("apply_filter(movies, 'filter_name')", :magenta)
      log "Type " << c("exit", :magenta) << c(" to leave the console.")
      binding.pry(quiet: true)
    else
      # ask to filter
      if !@opts[:quiet] && @opts[:select_scripts].empty?
        answer = ask("Do you want to filter the results? [yes/No]")
        if ["y", "yes"].include?(answer.downcase)
          movies = apply_filter(movies, record_filter)
          old_count = movies.count
          collection_size_changed old_count, movies.count, "custom filter"
        end
      end

      # filter
      @opts[:select_scripts].each do |filter|
        movies = apply_filter(movies, filter_script(filter))
        collection_size_changed old_count, movies.count, "filter: #{filter}"
        old_count = movies.count
      end

      # permute
      permute_script(movies) if @opts[:permute]

      # ask to save
      if !@opts[:quiet] && !@opts[:output_file]
        answer = ask("Enter filename to save output or leave blank to print to STDOUT:")
        if !answer.empty?
          @opts[:output_file] = answer
        end
      end

      # format results
      begin
        formatter = "ElchScan::Formatter::#{@opts[:formatter]}".constantize.new(self)
      rescue LoadError
        warn "Unknown formatter " << c("#{@opts[:formatter]}", :magenta) << c(", using Plain...", :red)
        formatter = "ElchScan::Formatter::Plain".constantize.new(self)
      end
      results = formatter.format(movies)

      # save
      if @opts[:output_file]
        File.open(@opts[:output_file], "w+") {|f| f.write(results.join("\n")) }
      else
        logger.log_without_timestr do
          results.each {|line| log line }
        end
      end
    end
  end
end