Class: Rivendell::Import::ConfigLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/rivendell/import/config_loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, auto_reload = false) ⇒ ConfigLoader

Returns a new instance of ConfigLoader.



7
8
9
10
# File 'lib/rivendell/import/config_loader.rb', line 7

def initialize(file, auto_reload = false)
  self.file = file
  self.auto_reload = auto_reload
end

Instance Attribute Details

#auto_reloadObject Also known as: auto_reload?

Returns the value of attribute auto_reload.



4
5
6
# File 'lib/rivendell/import/config_loader.rb', line 4

def auto_reload
  @auto_reload
end

#fileObject

Returns the value of attribute file.



4
5
6
# File 'lib/rivendell/import/config_loader.rb', line 4

def file
  @file
end

Instance Method Details

#absolute_pathObject



53
54
55
# File 'lib/rivendell/import/config_loader.rb', line 53

def absolute_path
  ::File.expand_path(file)
end

#basenameObject



57
58
59
# File 'lib/rivendell/import/config_loader.rb', line 57

def basename
  ::File.basename(file)
end

#current_configObject



16
17
18
# File 'lib/rivendell/import/config_loader.rb', line 16

def current_config
  ::File.read file
end

#directoryObject



61
62
63
# File 'lib/rivendell/import/config_loader.rb', line 61

def directory
  ::File.dirname(file)
end

#listen_fileObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rivendell/import/config_loader.rb', line 24

def listen_file
  return unless auto_reload?

  callback = Proc.new do |modified, added, removed|
    Rivendell::Import.logger.debug "Configuration changed ? #{[modified, added, removed].inspect}"
    if modified.include? absolute_path
      Rivendell::Import.logger.info "Configuration changed, reload it"
      load
    end
  end

  Rivendell::Import.logger.info "Listen to config file changes (#{file})"
  Listen.to(directory).filter(/^#{basename}$/).change(&callback).start
end

#listen_file_with_inotifyObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rivendell/import/config_loader.rb', line 39

def listen_file_with_inotify
  require 'rb-inotify'

  notifier = INotify::Notifier.new.watch(file, :modify) do
    Rivendell::Import.logger.info "Configuration changed, reload it"
    load
  end

  Thread.new do
    Rivendell::Import.logger.info "Listen to config modification (#{file})"
    notifier.run
  end
end

#loadObject



12
13
14
# File 'lib/rivendell/import/config_loader.rb', line 12

def load
  Kernel.load file
end

#save(config) ⇒ Object



20
21
22
# File 'lib/rivendell/import/config_loader.rb', line 20

def save(config)
  ::File.write absolute_path, config
end