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
-
#changed?(name) ⇒ Boolean
True if a change is detected.
-
#watch(name, context: self, method: nil, &blk) ⇒ Object
register a new watcher.
-
#watcher(name) ⇒ Watcher
retrieve a watcher given its name.
-
#watchers ⇒ Array
Registered watchers.
Instance Method Details
#changed?(name) ⇒ Boolean
Returns 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
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
40 41 42 |
# File 'lib/page_magic/watchers.rb', line 40 def watcher(name) watchers.find { |watcher| watcher.name == name } end |
#watchers ⇒ Array
Returns registered watchers.
45 46 47 |
# File 'lib/page_magic/watchers.rb', line 45 def watchers @watchers ||= [] end |