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.



23
24
25
26
27
28
# File 'lib/transmission-rss/config.rb', line 23

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, watch: true) ⇒ Object

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/transmission-rss/config.rb', line 31

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

  check_deprecated
  check_warnings

  self
end

#merge_defaults!Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/transmission-rss/config.rb', line 47

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

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

Merge Config Hash with Hash from YAML file.



72
73
74
75
76
77
78
# File 'lib/transmission-rss/config.rb', line 72

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



80
81
82
83
# File 'lib/transmission-rss/config.rb', line 80

def reset!
  self.clear
  self.merge_defaults!
end

#watch_file(path) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/transmission-rss/config.rb', line 85

def watch_file(path)
  path = Pathname.new(path).realpath.to_s

  @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