lib/page_magic/watchers.rb in page_magic-1.2.9 vs lib/page_magic/watchers.rb in page_magic-2.0.0.alpha1

- old
+ new

@@ -1,11 +1,13 @@ +# frozen_string_literal: true + 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'.freeze + 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) @@ -22,9 +24,10 @@ # watch(:something) do # # more complicated code to get value # end def watch(name, method = nil, &block) raise ElementMissingException, (ELEMENT_MISSING_MSG % name) unless block || respond_to?(name) + watched_element = block ? Watcher.new(name, &block) : Watcher.new(name, method) watchers.delete_if { |w| w.name == name } watchers << watched_element.check(self) end