Class: Bathyscaphe::Config

Inherits:
Object
  • 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.



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

def self.config_dir
  File.dirname( config_filename )
end

.config_filenameObject

Returns full path to config file.



86
87
88
# File 'lib/bathyscaphe/config.rb', line 86

def self.config_filename
  File.expand_path( CONFIG_FILENAME )
end

.loadObject

Loads configuration parameters.



50
51
52
53
54
55
# File 'lib/bathyscaphe/config.rb', line 50

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.



73
74
75
76
77
78
79
80
81
# File 'lib/bathyscaphe/config.rb', line 73

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



18
19
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/bathyscaphe/config.rb', line 18

def self.parse_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_tail( "-h", "--help", "Show usage") do
      puts opts
      exit
    end
  end
  # Parse command line
  begin
    optparser.parse!
    if ARGV.empty?
      puts optparser
      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.



61
62
63
64
65
66
67
68
# File 'lib/bathyscaphe/config.rb', line 61

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