module Tapestry::Ready

Attributes

ready[RW]

If a ready validation fails, the message reported by that failure will be captured in the `ready_error` accessor.

ready_error[RW]

If a ready validation fails, the message reported by that failure will be captured in the `ready_error` accessor.

Public Class Methods

included(caller) click to toggle source
# File lib/tapestry/ready.rb, line 39
def self.included(caller)
  caller.extend(ReadyAttributes)
end

Public Instance Methods

check_if_ready() click to toggle source
# File lib/tapestry/ready.rb, line 70
def check_if_ready
  when_ready(true)
end
ready?() click to toggle source

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/tapestry/ready.rb, line 81
def ready?
  self.ready_error = nil
  return true if ready
  ready_validations_pass?
end
when_ready(simple_check = false) { |self| ... } click to toggle source

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/tapestry/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

ready_validations_pass?() click to toggle source

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/tapestry/ready.rb, line 92
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