module SitePrism::Loadable

SitePrism::Loadable

SitePrism Loadable's are defined as checks or waiters which “must” pass before the rest of the loading logic can be performed on a class or section.

Loadable's are primarily used with the `#load` method which will auto-execute them all in their defined order. But they can be used dynamically wherever desired.

Attributes

load_error[RW]

In certain circumstances, we cache that the page has already been confirmed to be loaded so that actions which call `loaded?` a second time do not need to perform the load_validation queries against the page a second time.

loaded[RW]

In certain circumstances, we cache that the page has already been confirmed to be loaded so that actions which call `loaded?` a second time do not need to perform the load_validation queries against the page a second time.

Public Class Methods

included(base) click to toggle source
# File lib/site_prism/loadable.rb, line 51
def self.included(base)
  base.extend(ClassMethods)
end

Public Instance Methods

loaded?() click to toggle source

Check if the page is loaded.

On failure, if an error was reported by a failing validation, it will be available via the `load_error` accessor.

@return [Boolean] True if the page loaded successfully; otherwise false.

# File lib/site_prism/loadable.rb, line 89
def loaded?
  self.load_error = nil

  return true if loaded

  load_validations_pass?
end
when_loaded() { |self| ... } click to toggle source

Executes the given block after the page is loaded.

The loadable object instance is yielded into the block.

# File lib/site_prism/loadable.rb, line 64
def when_loaded
  # Get original loaded value, in case we are nested
  # inside another when_loaded block.
  previously_loaded = loaded

  # Within the block, check (and cache) loaded?, to see whether the
  # page has indeed loaded according to the rules defined by the user.
  self.loaded = loaded?

  # If the page hasn't loaded. Then crash and return the error message.
  # If one isn't defined, just return the Error code.
  raise SitePrism::FailedLoadValidationError, load_error unless loaded

  # Return the yield value of the block if one was supplied.
  yield self if block_given?
ensure
  self.loaded = previously_loaded
end

Private Instance Methods

load_validations_pass?() click to toggle source

If any load validations from page subclasses returns false, immediately return false.

# File lib/site_prism/loadable.rb, line 101
def load_validations_pass?
  self.class.load_validations.all? do |validation|
    passed, message = instance_eval(&validation)

    self.load_error = message if message && !passed
    passed
  end
end