Class: Bathyscaphe::Config

Inherits:
Object show all
Defined in:
lib/bathyscaphe/config.rb

Constant Summary collapse

OPTIONS =
{
  :dry_run => false
}

Class Method Summary collapse

Class Method Details

.config_dirObject

Returns full path to the directory where config file is located.



101
102
103
# File 'lib/bathyscaphe/config.rb', line 101

def self.config_dir
  File.dirname( config_filename )
end

.config_filenameObject

Returns full path to config file.



94
95
96
# File 'lib/bathyscaphe/config.rb', line 94

def self.config_filename
  File.expand_path( CONFIG_FILENAME )
end

.loadObject

Loads configuration parameters.



58
59
60
61
62
63
# File 'lib/bathyscaphe/config.rb', line 58

def self.load
  @params = CONFIG_DEFAULTS
  if File.exists? config_filename 
    @params.merge! YAML::load_file( config_filename )
  end
end

.method_missing(name, *args) ⇒ Object

Returns a property with the given name.



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

def self.method_missing( name, *args )
  if m = /^(.+)=$/.match(name.to_s)
    # set
    @params[m[1].to_sym] = args[0]
  else
    # get
    @params[name.to_sym]
  end
end

.parse_optionsObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bathyscaphe/config.rb', line 22

def self.parse_options
  options = {}
  optparser = OptionParser.new do |opts|
    opts.set_summary_indent('  ')
    opts.banner = "Usage: #{File.basename($0)} [OPTIONS] TV_SHOW"
    # opts.on( "-s", "--source SOURCE", String, "Set source to download subtitles from","Current: #{self.source}" ) { |o| self.source = o ; self.save }
    opts.on( "-d", "--dry-run", "Parse filename but do not download anything") { |o| OPTIONS[:dry_run] = o}
    opts.on( "-v", "--version", "Show version") do
      options[:version] = true
      puts Bathyscaphe::VERSION
    end
    opts.on_tail( "-h", "--help", "Show usage") do
      puts opts
      exit
    end
  end
  # Parse command line
  begin
    optparser.parse!
    if ARGV.empty?
      puts optparser unless options[:version]
      exit
    else
      tv_show = ARGV[0]
    end
  rescue OptionParser::ParseError => e
    puts e
    puts optparser
    exit
  end
  return tv_show
end

.saveObject

Writes configuration parameters, creating config directory and file if they do not exist.



69
70
71
72
73
74
75
76
# File 'lib/bathyscaphe/config.rb', line 69

def self.save
  unless File.directory? config_dir
    system "mkdir -p #{config_dir}"
  end
  File.open( config_filename, "w" ) do |f|
    YAML::dump( @params, f )
  end
end