Class: TransmissionRSS::Config

Inherits:
Hash show all
Extended by:
Callback
Includes:
Singleton
Defined in:
lib/transmission-rss/config.rb

Overview

Class handles configuration parameters.

Instance Method Summary collapse

Methods included from Callback

callback

Methods inherited from Hash

#method_missing

Constructor Details

#initialize(file = nil) ⇒ Config

Returns a new instance of Config.



19
20
21
22
23
24
# File 'lib/transmission-rss/config.rb', line 19

def initialize(file = nil)
  self.merge_defaults!
  self.load(file) unless file.nil?

  @log = Log.instance
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Hash

Instance Method Details

#load(config) ⇒ Object

Merges a Hash or YAML file (containing a Hash) with itself.



27
28
29
30
31
32
33
34
35
36
# File 'lib/transmission-rss/config.rb', line 27

def load(config)
  case config.class.to_s
  when 'Hash'
    self.merge!(config)
  when 'String'
    self.merge_yaml!(config)
  else
    raise ArgumentError.new('Could not load config.')
  end
end

#merge_defaults!Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/transmission-rss/config.rb', line 38

def merge_defaults!
  self.merge!({
    'feeds' => [],
    'update_interval' => 600,
    'add_paused' => false,
    'server' => {
      'host' => 'localhost',
      'port' => 9091,
      'rpc_path' => '/transmission/rpc'
    },
    'login' => nil,
    'log_target' => $stderr,
    'fork' => false,
    'pid_file' => false,
    'privileges' => {},
    'seen_file' => nil
  })
end

#merge_yaml!(path, watch = true) ⇒ Object

Merge Config Hash with Hash from YAML file.



58
59
60
61
62
63
64
# File 'lib/transmission-rss/config.rb', line 58

def merge_yaml!(path, watch = true)
  self.merge!(YAML.load_file(path))
rescue TypeError
  # If YAML loading fails, .load_file returns `false`.
else
  watch_file(path) if watch && linux?
end

#reset!Object



66
67
68
69
# File 'lib/transmission-rss/config.rb', line 66

def reset!
  self.clear
  self.merge_defaults!
end

#watch_file(path) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/transmission-rss/config.rb', line 71

def watch_file(path)
  path = Pathname.new(path).realpath.to_s
  @log.debug('watch_file ' + path)

  @notifier ||= INotify::Notifier.new
  @notifier.watch(path, :close_write) do |e|
    self.reset!
    self.merge_yaml!(path, false)

    @log.debug('reloaded config file ' + path)
    @log.debug(self)

    on_change
  end

  @notifier_thread ||= Thread.start do
    @notifier.run
  end
end