Module: SitePrism::Loadable

Included in:
Page, Section
Defined in:
lib/site_prism/loadable.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#load_errorObject

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.



51
52
53
# File 'lib/site_prism/loadable.rb', line 51

def load_error
  @load_error
end

#loadedObject

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.



51
52
53
# File 'lib/site_prism/loadable.rb', line 51

def loaded
  @loaded
end

Class Method Details

.included(base) ⇒ Object



43
44
45
# File 'lib/site_prism/loadable.rb', line 43

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#loaded?Boolean

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.

Returns:

  • (Boolean)

    True if the page loaded successfully; otherwise false.



81
82
83
84
85
86
87
# File 'lib/site_prism/loadable.rb', line 81

def loaded?
  self.load_error = nil

  return true if loaded

  load_validations_pass?
end

#when_loadedObject

Executes the given block after the page is loaded.

The loadable object instance is yielded into the block.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/site_prism/loadable.rb', line 56

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