Class: Rivendell::Import::ConfigLoader
- Inherits:
-
Object
- Object
- Rivendell::Import::ConfigLoader
- Defined in:
- lib/rivendell/import/config_loader.rb
Instance Attribute Summary collapse
-
#auto_reload ⇒ Object
(also: #auto_reload?)
Returns the value of attribute auto_reload.
-
#file ⇒ Object
Returns the value of attribute file.
Instance Method Summary collapse
- #absolute_path ⇒ Object
- #basename ⇒ Object
- #current_config ⇒ Object
- #directory ⇒ Object
-
#initialize(file, auto_reload = false) ⇒ ConfigLoader
constructor
A new instance of ConfigLoader.
- #listen_file ⇒ Object
- #listen_file_with_inotify ⇒ Object
- #load ⇒ Object
- #save(config) ⇒ Object
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_reload ⇒ Object 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 |
#file ⇒ Object
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_path ⇒ Object
53 54 55 |
# File 'lib/rivendell/import/config_loader.rb', line 53 def absolute_path ::File.(file) end |
#basename ⇒ Object
57 58 59 |
# File 'lib/rivendell/import/config_loader.rb', line 57 def basename ::File.basename(file) end |
#current_config ⇒ Object
16 17 18 |
# File 'lib/rivendell/import/config_loader.rb', line 16 def current_config ::File.read file end |
#directory ⇒ Object
61 62 63 |
# File 'lib/rivendell/import/config_loader.rb', line 61 def directory ::File.dirname(file) end |
#listen_file ⇒ Object
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_inotify ⇒ Object
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 |
#load ⇒ Object
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 |