Class: Episode

Inherits:
Object
  • Object
show all
Defined in:
lib/episode.rb,
lib/episode/config.rb

Defined Under Namespace

Classes: CommandError, Config, NoCommandError, NotLocal

Constant Summary collapse

VERSION =
2
VERSION_PATCH =
0
DEFAULT_CFG =
{
  dir: '.',
  viewer: 'xdg-open',
  index_from: 1,
  pointer: '*',
  formats: %w[mkv mp4 avi]
}

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Episode



13
14
15
16
17
18
# File 'lib/episode.rb', line 13

def initialize(opts = {}) 
  @is_name = opts[:name] || false
  @is_update = opts[:update] != false
  @is_global = opts[:global] || false
  @viewer = opts[:viewer]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object (private)



161
162
163
164
165
166
167
# File 'lib/episode.rb', line 161

def method_missing(*args)
  if args.size == 1
    play args.first.to_s
  else
    raise NoCommandError, args.first
  end
end

Instance Method Details

#cfgObject Also known as: c



105
106
107
108
109
110
111
112
113
114
# File 'lib/episode.rb', line 105

def cfg
  cfg_h = 
    if global? 
      config.global.to_h
    else
      config.to_h
    end

  cfg_h.each { |param, val| puts "#{param}: #{val}" }
end

#lastObject Also known as: l



75
76
77
# File 'lib/episode.rb', line 75

def last
  play_last 
end

#lsObject



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
# File 'lib/episode.rb', line 20

def ls
  if episodes.empty?
    raise CommandError, "      No episodes found in the directory.\n      Currently \#{PROGRAM_NAME} is looking for the following formats: \#{config.formats.join ', '}.\n      To change this run:\n        `ep set formats fmt1,fmt2,fmt3` -- to set list of formats for the current directory\n        `ep set formats fmt1,fmt2,fmt3 -g` -- to make it global\n    EOS\n  end\n\n  largest_id = episodes.size - 1 + config.index_from\n  id_length = Math.log10([largest_id, 1].max).floor + 1\n\n  episodes.each_with_index do |filename, id|\n    id_fixed = id + config.index_from\n    id_formatted = id_fixed.to_s.rjust id_length, '0'\n    separator = \n      if config.last && id_fixed == last_id\n        config.pointer\n      else\n        '|'\n      end\n    puts \"\#{id_formatted} \#{separator} \#{filename}\"\n  end\nend\n" 

#nextObject Also known as: n



81
82
83
84
85
86
87
88
89
# File 'lib/episode.rb', line 81

def next
  config.last = 
    if config.last 
      episode_by_id(last_id + 1) 
    else 
      episodes.first
    end
  play_last
end

#play(ref) ⇒ Object



100
101
102
103
# File 'lib/episode.rb', line 100

def play(ref)
  config.last = parse_episode_ref ref
  play_last
end

#prevObject Also known as: p



93
94
95
96
# File 'lib/episode.rb', line 93

def prev
  config.last = episode_by_id(last_id - 1)
  play_last
end

#reset(param = nil) ⇒ Object Also known as: r



144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/episode.rb', line 144

def reset(param = nil)
  cfg_path = global? ? CFG_GLOBAL_PATH : CFG_FILENAME  

  if param.nil?
    $stderr.puts "Reset all config parameters (delete #{cfg_path})? (y|N)"
    if 'y' == $stdin.getch
      safe_rm cfg_path 
    end
  else
    set param, nil
  end
end

#set(param, value) ⇒ Object



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
# File 'lib/episode.rb', line 118

def set(param, value)
  unless config.respond_to? param
    raise CommandError, "      Unknown config parameter '\#{param}'.\n      Run `\#{PROGRAM_NAME} cfg` to see the list of available parameters.\n    EOS\n  end\n\n  cfg, cfg_path =\n    if global? \n      [config.global, CFG_GLOBAL_PATH]\n    else\n      [config, CFG_FILENAME]\n    end\n\n  cfg.send \"\#{param}=\", parse_config_value(param, value)\n\n  if param == 'last'\n    cfg.last_played_at = nil\n  end\n\n  config_save cfg_path, cfg\nrescue NotLocal\n  raise CommandError, \"Parameter '\#{param}' is not global\"\nend\n"

#statusObject Also known as: s



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
# File 'lib/episode.rb', line 47

def status
  puts "#{last_id} #{config.pointer} #{config.last}"

  unless config.last_played_at
    puts 'Time unknown'
    return
  end

  seconds_ago = (Time.now - config.last_played_at).floor
  days_ago = seconds_ago / (3600 * 24)
  hours_ago = (seconds_ago % (3600 * 24)) / 3600
  minutes_ago = (seconds_ago % 3600) / 60
  time_passed = 
    if seconds_ago < 60
      pluralize seconds_ago, 'second'
    else
      [
        days_ago > 0 ? pluralize(days_ago, 'day') : nil,
        hours_ago > 0 ? pluralize(hours_ago, 'hour') : nil,
        minutes_ago > 0 ? pluralize(minutes_ago, 'minute') : nil
      ].compact.join ' '
    end
    
  puts "#{config.last_played_at} (#{time_passed} ago)" 
end