Sha256: b7bff140b098ccca804890f3a74ef40301d16069c2023adbfff458c0954bce62

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

require 'page_magic/watcher'

module PageMagic
  # module Watchers - contains methods for adding watchers and checking them
  module Watchers
    ELEMENT_MISSING_MSG = 'Unable to defined watcher: Element or method with the name %s can not be found'

    # @param [Symbol] name - the name of the watcher
    # @return [Boolean] true if a change is detected
    def changed?(name)
      watched_element = watcher(name)
      watched_element.last != watched_element.check(self).last
    end

    # register a new watcher
    # @param [Object] name of the watcher/element
    # @param [Symbol] method - the method on the watched element to check
    # @yieldreturn [Object] the value that should be checked
    # @example
    #  watch(:price, :text)
    # @example
    #  watch(:something) do
    #   # more complicated code to get value
    # end
    def watch(name, method = nil, &block)
      fail ElementMissingException, (ELEMENT_MISSING_MSG % name) unless block || respond_to?(name)
      watched_element = block ? Watcher.new(name, &block) : Watcher.new(name, method)
      watchers << watched_element.check(self)
    end

    # retrieve a watcher given its name
    # @param [Symbol] name the name of the watcher
    # @return [Watcher] watcher with the given name
    def watcher(name)
      watchers.find { |watcher| watcher.name == name }
    end

    # @return [Array] registered watchers
    def watchers
      @watchers ||= []
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
page_magic-1.0.1 lib/page_magic/watchers.rb
page_magic-1.0.0 lib/page_magic/watchers.rb