module PageMagic::Watchers

module Watchers - contains methods for adding watchers and checking them

Constants

ELEMENT_MISSING_MSG

Public Instance Methods

changed?(name) click to toggle source

@param [Symbol] name - the name of the watcher @return [Boolean] true if a change is detected

# 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(name, context: self, method: nil, &blk) click to toggle source

register a new watcher @overload watch(:price, context: object, method: :text)

@param [Symbol] name of the watcher/element
@param [Object] context the object that is being watched - defaults to self
@param [Symbol] method - the method on the watched element to check

@overload watch(:text)

@param [Symbol] method - the method on the watched element to check

@overload watch(:text, &blk)

@param [Symbol] name of the watcher/element
@yieldreturn [Object] the value that should be checked
@example
 watch(:something) do
 # more complicated code to get value
 end
# 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) click to toggle source

retrieve a watcher given its name @param [Symbol] name the name of the watcher @return [Watcher] watcher with the given name

# File lib/page_magic/watchers.rb, line 40
def watcher(name)
  watchers.find { |watcher| watcher.name == name }
end
watchers() click to toggle source

@return [Array] registered watchers

# File lib/page_magic/watchers.rb, line 45
def watchers
  @watchers ||= []
end

Private Instance Methods

watch_method(name, context:, method:) click to toggle source
# File lib/page_magic/watchers.rb, line 51
def watch_method(name, context:, method:)
  subject = method || name
  raise ElementMissingException, (ELEMENT_MISSING_MSG % subject) unless context.respond_to?(subject)

  Watcher.new(name, context: context) do
    public_send(subject)
  end
end