Module: PageMagic::Watchers

Included in:
Element, InstanceMethods
Defined in:
lib/page_magic/watchers.rb

Overview

module Watchers - contains methods for adding watchers and checking them

Constant Summary collapse

ELEMENT_MISSING_MSG =
'Unable to defined watcher: Element or method with the name %s can not be found'

Instance Method Summary collapse

Instance Method Details

#changed?(name) ⇒ Boolean

Returns true if a change is detected.

Parameters:

  • name (Symbol)
    • the name of the watcher

Returns:

  • (Boolean)

    true if a change is detected



12
13
14
15
# File 'lib/page_magic/watchers.rb', line 12

def changed?(name)
  watched_element = watcher(name)
  watched_element.observed_value != watched_element.check.observed_value
end

#watch(: price, context: object, method: :text) ⇒ Object #watch(: text) ⇒ Object #watch(: text, &blk) ⇒ Object

register a new watcher

Overloads:

  • #watch(: price, context: object, method: :text) ⇒ Object

    Parameters:

    • name (Symbol)

      of the watcher/element

    • context (Object) (defaults to: object)

      the object that is being watched - defaults to self

    • method (Symbol) (defaults to: :text)
      • the method on the watched element to check
  • #watch(: text) ⇒ Object

    Parameters:

    • method (Symbol)
      • the method on the watched element to check
  • #watch(: text, &blk) ⇒ Object

    Examples:

    watch(:something) do
    # more complicated code to get value
    end

    Parameters:

    • name (Symbol)

      of the watcher/element

    Yield Returns:

    • (Object)

      the value that should be checked



31
32
33
34
35
# File 'lib/page_magic/watchers.rb', line 31

def watch(name, context: self, method: nil, &blk)
  watcher = blk ? Watcher.new(name, context: context, &blk) : watch_method(name, context: context, method: method)
  watchers.delete_if { |w| w.name == name }
  watchers << watcher.check
end

#watcher(name) ⇒ Watcher

retrieve a watcher given its name

Parameters:

  • name (Symbol)

    the name of the watcher

Returns:

  • (Watcher)

    watcher with the given name



40
41
42
# File 'lib/page_magic/watchers.rb', line 40

def watcher(name)
  watchers.find { |watcher| watcher.name == name }
end

#watchersArray

Returns registered watchers.

Returns:

  • (Array)

    registered watchers



45
46
47
# File 'lib/page_magic/watchers.rb', line 45

def watchers
  @watchers ||= []
end