Module: ElchScan::Application::Dispatch

Included in:
ElchScan::Application
Defined in:
lib/elch_scan/application/dispatch.rb

Instance Method Summary collapse

Instance Method Details

#_index_movies(directories) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/elch_scan/application/dispatch.rb', line 167

def _index_movies directories
  directories.each_with_object({}) do |dir, result|
    if FileTest.directory?(dir)
      Find.find(dir) do |path|
        # calculate depth
        depth = path.scan(?/).count - dir.scan(?/).count
        depth -= 1 unless File.directory?(path)

        if depth == 1 && File.directory?(path)
          result[File.basename(path)] = Movie.new(
            self,
            dir: dir,
            path: path,
          )
        end
      end
    else
      log(c("Directory unreadable, skipping ", :red) << c("#{dir}", :magenta))
    end
  end
end

#collection_size_changed(cold, cnew, reason = nil) ⇒ Object



156
157
158
159
160
161
162
163
164
165
# File 'lib/elch_scan/application/dispatch.rb', line 156

def collection_size_changed cold, cnew, reason = nil
  if cold != cnew
    log(
      "We have filtered " << c("#{cnew}", :magenta) <<
      c(" movies") << c(" (#{cnew - cold})", :red) <<
      c(" from originally ") << c("#{cold}", :magenta) <<
      (reason ? c(" (#{reason})", :blue) : "")
    )
  end
end

#dispatch(action = (@opts[:dispatch] || :help)) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/elch_scan/application/dispatch.rb', line 4

def dispatch action = (@opts[:dispatch] || :help)
  case action
    when :help then @optparse.to_s.split("\n").each(&method(:log))
    when :version, :info then dispatch_info
    else
      if respond_to?("dispatch_#{action}")
        send("dispatch_#{action}")
      else
        abort("unknown action #{action}", 1)
      end
  end
end

#dispatch_edit_scriptObject



77
78
79
# File 'lib/elch_scan/application/dispatch.rb', line 77

def dispatch_edit_script
  record_filter(filter_script(@opts[:select_script]))
end

#dispatch_generate_configObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/elch_scan/application/dispatch.rb', line 17

def dispatch_generate_config
  if File.exist?(@config_src)
    log "Configuration already exists, please delete it do generate another one."
  else
    FileUtils.cp("#{ROOT}/doc/config.yml", @config_src)
    log "Sample configuration created!"
    log "You will need to add at least 1 one movie directory."
    answer = ask("Do you want to open the configuration 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
  end
end

#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

#dispatch_infoObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/elch_scan/application/dispatch.rb', line 35

def dispatch_info
  logger.log_without_timestr do
    log ""
    log "     Your version: #{your_version = Gem::Version.new(ElchScan::VERSION)}"

    # get current version
    logger.log_with_print do
      log "  Current version: "
      if cfg("application.check_version")
        require "net/http"
        log c("checking...", :blue)

        begin
          current_version = Gem::Version.new Net::HTTP.get_response(URI.parse(ElchScan::UPDATE_URL)).body.strip

          if current_version > your_version
            status = c("#{current_version} (consider update)", :red)
          elsif current_version < your_version
            status = c("#{current_version} (ahead, beta)", :green)
          else
            status = c("#{current_version} (up2date)", :green)
          end
        rescue
          status = c("failed (#{$!.message})", :red)
        end

        logger.raw "#{"\b" * 11}#{" " * 11}#{"\b" * 11}", :print # reset cursor
        log status
      else
        log c("check disabled", :red)
      end
    end

    # more info
    log ""
    log "  ElchScan is brought to you by #{c "bmonkeys.net", :green}"
    log "  Contribute @ #{c "github.com/2called-chaos/elch_scan", :cyan}"
    log "  Eat bananas every day!"
    log ""
  end
end