Class: PageMagic::Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/page_magic/watcher.rb

Overview

class WatchedElementDefinition - Contains the specification the for checking if an subject has changed

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, method = nil, &block) ⇒ Watcher

Returns a new instance of Watcher.

Examples:

Watcher.new(:text)
Watcher.new do
  session.url
end

Parameters:

  • name (Symbol)

    the of the subject being checked

  • method (Symbol) (defaults to: nil)

    the method that should be called on the subject being checked



13
14
15
16
17
# File 'lib/page_magic/watcher.rb', line 13

def initialize(name, method = nil, &block)
  @name = name
  @attribute = method
  @block = block
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



4
5
6
# File 'lib/page_magic/watcher.rb', line 4

def attribute
  @attribute
end

#blockObject (readonly)

Returns the value of attribute block.



4
5
6
# File 'lib/page_magic/watcher.rb', line 4

def block
  @block
end

#lastObject (readonly)

Returns the value of attribute last.



4
5
6
# File 'lib/page_magic/watcher.rb', line 4

def last
  @last
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/page_magic/watcher.rb', line 4

def name
  @name
end

Instance Method Details

#==(other) ⇒ Boolen

Returns true of the candiate is equal ot this one.

Parameters:

  • other (Object)

    candidate for equality check

Returns:

  • (Boolen)

    true of the candiate is equal ot this one.



34
35
36
37
38
39
# File 'lib/page_magic/watcher.rb', line 34

def ==(other)
  other.is_a?(Watcher) &&
    name == other.name &&
    attribute == other.attribute &&
    block == other.block
end

#check(subject = nil) ⇒ Object

check current value of watched element. The result of the check is stored against #last a block was specified then this will be executed.

Parameters:

  • subject (Object) (defaults to: nil)
    • subject to run watcher against


22
23
24
25
26
27
28
29
30
# File 'lib/page_magic/watcher.rb', line 22

def check(subject = nil)
  @last = if block
            block.call
          else
            object = subject.send(name)
            attribute ? object.send(attribute) : object
          end
  self
end