Class: Flickrcaptionr::Config

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_path, user_options = {}) ⇒ Config

TODO: DRY this out.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/flickrcaptionr/config.rb', line 3

def initialize(config_path, user_options={})
  user_options.delete_if{|k,v|v == nil or v == ''}
  config_path = File.expand_path(config_path)
  @@defaults = {flickr_api_key:'', output_path: ''}
  config = YAML.load(open(config_path).read()) rescue nil
  if config
    @@config = config
    if user_options != {} and not user_options == nil
      user_options.each_pair do |k,v|
        @@config[k] = v
      end
      File.open(config_path, 'w'){|f|f<<YAML.dump(@@config)}
    end
  else
    begin
      if user_options != {} and not user_options == nil
        user_options.each_pair do |k,v|
          @@config[k] = v
        end
      end
      File.open(config_path, 'w'){|f|f<<YAML.dump(@@defaults)}
    rescue Exception => e
      raise ArgumentError, "Couldn't write a default config file! Check the config file path is writeable (#{e.inspect})."
    end
  end
  puts "Configuration: "+@@config.inspect
end

Class Method Details

.method_missing(method_name, *args, &block) ⇒ Object



30
31
32
# File 'lib/flickrcaptionr/config.rb', line 30

def self.method_missing(method_name, *args, &block)
  return @@config[method_name.to_sym] rescue nil
end