Class: PathObserver::Observer

Inherits:
Object
  • Object
show all
Defined in:
lib/path_observer/observer.rb

Overview

PathObserver::Observer observe a Pathname Object.

SYNOPSYS

observe_manager = PathObserver::ObserveManager.new
observe_manager.add_observer(PathObserver::Observer.new('hoge.txt') do |path, result|
  puts "#{path} was updated on #{result}"
end

SEE ALSO

PathObserver::ObserveManager, Observable, Pathname

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, &event_action) ⇒ Observer

event_action will be called by update in actually. path is a target for observing.



17
18
19
20
21
22
23
24
# File 'lib/path_observer/observer.rb', line 17

def initialize path, &event_action
  @path = Pathname.new path
  unless @path.exist?
    raise ArgumentError
  end

  @proc = event_action
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



26
27
28
# File 'lib/path_observer/observer.rb', line 26

def path
  @path
end

Instance Method Details

#update(path, result) ⇒ Object

update is called by ObserveManager#notify_observers



29
30
31
32
33
# File 'lib/path_observer/observer.rb', line 29

def update path, result
  if @path.to_s == path.to_s
    @proc.call @path.to_s, result
  end
end