Class: Syncr::ListenerSet

Inherits:
Object
  • Object
show all
Defined in:
lib/syncr/listener_set.rb

Overview

Class to handle sets of listener instances

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&initial_action) ⇒ ListenerSet

Sets action for each listener to perform and does some inital setup

Parameters:

  • initial_action (Proc)

    action for each listener to take (arguments are `from` and `to`)


15
16
17
18
19
20
# File 'lib/syncr/listener_set.rb', line 15

def initialize(&initial_action)
  @action = initial_action if initial_action

  @status = :stopped
  @listeners = Hash.new
end

Instance Attribute Details

#listenersObject (readonly)

Hash

Named listener set.


10
11
12
# File 'lib/syncr/listener_set.rb', line 10

def listeners
  @listeners
end

#statusObject (readonly)

Symbol

Status for listener set.


7
8
9
# File 'lib/syncr/listener_set.rb', line 7

def status
  @status
end

Instance Method Details

#action(&replacement_action) ⇒ Proc

Gets or sets the listener action. When this changes, does not update already-created listeners' actions.

Parameters:

  • replacement_action (Proc)

    replaces the initial_action set in #initialize

Returns:

  • (Proc)

    listener action


26
27
28
29
30
31
32
# File 'lib/syncr/listener_set.rb', line 26

def action(&replacement_action)
  if replacement_action
    @action = replacement_action
  else
    @action
  end
end

#add_listener(name, from, to) ⇒ Object

Adds listener to set

Parameters:

  • name (Symbol, String)

    name to set the listener to

  • from (String)

    origin path to listen on

  • to (String)

    destination path to act upton


39
40
41
42
43
44
45
46
# File 'lib/syncr/listener_set.rb', line 39

def add_listener(name, from, to)
  @listeners[name] = Listen.to(from) do |modified, added, removed|
    log "modified absolute path: #{modified}"
    log "added absolute path: #{added}"
    log "removed absolute path: #{removed}"
    @action.call(from, to)
  end
end

#remove_listener(name) ⇒ Object

Remove listener from set

Parameters:

  • name (Symbol, String)

    name of listener to remove


50
51
52
# File 'lib/syncr/listener_set.rb', line 50

def remove_listener(name)
  @listeners.delete name
end

#sizeInteger Also known as: length

Number of listeners in set

Returns:

  • (Integer)

    size of listeners hash


87
88
89
# File 'lib/syncr/listener_set.rb', line 87

def size
  listeners.size
end

#startSymbol

Starts all listeners

Returns:

  • (Symbol)

    current status


57
58
59
60
# File 'lib/syncr/listener_set.rb', line 57

def start
  on_each_listener(:start)
  @status = :started
end

#started?Boolean

Are listeners started?

Returns:

  • (Boolean)

    true if started, false if not


73
74
75
# File 'lib/syncr/listener_set.rb', line 73

def started?
  @status == :started
end

#stopSymbol

Stops all listeners

Returns:

  • (Symbol)

    current status


65
66
67
68
# File 'lib/syncr/listener_set.rb', line 65

def stop
  on_each_listener(:stop)
  @status = :stopped
end

#stopped?Boolean

Are listeners stopped?

Returns:

  • (Boolean)

    true if stopped, false if not


80
81
82
# File 'lib/syncr/listener_set.rb', line 80

def stopped?
  @status == :stopped
end