Module: SitePrism::Loadable

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

Overview

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.

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 or section has already been “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.



18
19
20
# File 'lib/site_prism/loadable.rb', line 18

def load_error
  @load_error
end

#loadedObject

In certain circumstances, we cache that the page or section has already been “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.



18
19
20
# File 'lib/site_prism/loadable.rb', line 18

def loaded
  @loaded
end

Class Method Details

.included(base) ⇒ Object



12
13
14
# File 'lib/site_prism/loadable.rb', line 12

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

It will return true if the page has been loaded successfully; otherwise it returns false

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/site_prism/loadable.rb', line 48

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.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/site_prism/loadable.rb', line 23

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