Module: MakeWatchable::Watcher

Extended by:
ActiveSupport::Concern
Defined in:
lib/make_watchable/watcher.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#unwatch(watchable) ⇒ Object

Unwatch a watchable, but don’t raise an error if the watcher is not watching the watchable. Instead returns false. Raises an InvalidWatchableError if the watchable is not a valid watchable.



61
62
63
64
65
66
67
68
69
# File 'lib/make_watchable/watcher.rb', line 61

def unwatch(watchable)
  begin
    unwatch!(watchable)
    success = true
  rescue Exceptions::NotWatchingError
    success = false
  end
  success
end

#unwatch!(watchable) ⇒ Object

Unwatch a watchable. Raises an +NotWatchingError if the watcher is not watching the watchable. Raises an InvalidWatchableError if the watchable is not a valid watchable.



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/make_watchable/watcher.rb', line 46

def unwatch!(watchable)
  check_watchable(watchable)

  watching = fetch_watching(watchable)

  raise Exceptions::NotWatchingError unless watching

  watching.destroy

  true
end

#watch(watchable) ⇒ Object

Watch a watchable, but don’t raise an error if the watcher is already watching the watchable. Instead simply return false then and ignore the watch request. Raises an InvalidWatchableError if the watchable is not a valid watchable.



33
34
35
36
37
38
39
40
41
# File 'lib/make_watchable/watcher.rb', line 33

def watch(watchable)
  begin
    watch!(watchable)
    success = true
  rescue Exceptions::AlreadyWatchingError
    success = false
  end
  success
end

#watch!(watchable) ⇒ Object

Watch a watchable. Raises an AlreadyWatchingError if the watcher already is watching the watchable. Raises an InvalidWatchableError if the watchable is not a valid watchable.



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/make_watchable/watcher.rb', line 18

def watch!(watchable)
  check_watchable(watchable)

  if watches?(watchable)
    raise Exceptions::AlreadyWatchingError.new
  end

  Watching.create(:watchable => watchable, :watcher => self)

  true
end

#watches?(watchable) ⇒ Boolean

Check if the watcher watches a watchable.

Returns:

  • (Boolean)


72
73
74
75
76
# File 'lib/make_watchable/watcher.rb', line 72

def watches?(watchable)
  check_watchable(watchable)

  fetch_watching(watchable) ? true : false
end