Class: FileMonitor::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/file_monitor/core.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Core

Returns a new instance of Core.



5
6
7
8
9
10
# File 'lib/file_monitor/core.rb', line 5

def initialize *args
  args.flatten!
  options = args.last.is_a?(Hash) ? args.pop : {}
  @hooks  = []
  @config = FileMonitor::Configuration.new(:config_path => options[:config_path], :watch_dir => options[:watch_dir])
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



4
5
6
# File 'lib/file_monitor/core.rb', line 4

def config
  @config
end

#hooksObject (readonly)

Returns the value of attribute hooks.



3
4
5
# File 'lib/file_monitor/core.rb', line 3

def hooks
  @hooks
end

Instance Method Details

#add_hook(klass, call_method = :run) ⇒ Object



12
13
14
15
# File 'lib/file_monitor/core.rb', line 12

def add_hook klass, call_method=:run
  @hooks.push [klass, call_method]
  self
end

#hook!(event, *args) ⇒ Object



31
32
33
34
35
36
# File 'lib/file_monitor/core.rb', line 31

def hook! event, *args
  if hooks.empty?
    hooks.push default_hook
  end
  hooks.map{|klass, call_method| FileMonitor::Hooks.const_get(klass).send(call_method, *[event, args]) }
end

#remove_hook(klass) ⇒ Object



17
18
19
20
# File 'lib/file_monitor/core.rb', line 17

def remove_hook klass
  @hooks.delete_if{|k,m| k.to_s == klass.to_s }
  self
end

#runObject



22
23
24
25
26
27
28
29
# File 'lib/file_monitor/core.rb', line 22

def run
  _self = self
  FSSM.monitor(config.data["monitor"]["path"]) do
    update{|base, relative| _self.hook!(:update, base, relative) }
    delete{|base, relative| _self.hook!(:delete, base, relative) }
    create{|base, relative| _self.hook!(:create, base, relative) }
  end
end