module Testable::Ready
Attributes
If a ready validation fails, the message reported by that failure will be captured in the `ready_error` accessor.
If a ready validation fails, the message reported by that failure will be captured in the `ready_error` accessor.
Public Class Methods
# File lib/testable/ready.rb, line 39 def self.included(caller) caller.extend(ReadyAttributes) end
Public Instance Methods
# File lib/testable/ready.rb, line 70 def check_if_ready when_ready(true) end
The `ready?` method is used to check if the page has been loaded successfully, which means that none of the ready validations have indicated failure.
When `ready?` is called, the blocks that were stored in the above `ready_validations` array are instance evaluated against the current page instance.
# File lib/testable/ready.rb, line 81 def ready? self.ready_error = nil return true if ready ready_validations_pass? end
The `when_ready` method is called on an instance of an interface. This executes the provided validation block after the page has been loaded. The Ready
object instance is yielded into the block.
Calls to the `ready?` method use a poor-man's cache approach. The idea here being that when a page has confirmed that it is ready, meaning that no ready validations have failed, that information is stored so that any subsequent calls to `ready?` do not query the ready validations again.
# File lib/testable/ready.rb, line 55 def when_ready(simple_check = false, &_block) already_marked_ready = ready unless simple_check no_ready_check_possible unless block_given? end self.ready = ready? not_ready_validation(ready_error || 'NO REASON PROVIDED') unless ready yield self if block_given? ensure self.ready = already_marked_ready end
Private Instance Methods
This method checks if the ready validations that have been specified have passed. If any ready validation fails, no matter if others have succeeded, this method immediately returns false.
# File lib/testable/ready.rb, line 93 def ready_validations_pass? self.class.ready_validations.all? do |validation| passed, message = instance_eval(&validation) self.ready_error = message if message && !passed passed end end