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)


51
52
53
54
55
56
57
# File 'lib/site_prism/loadable.rb', line 51

def loaded?
  self.load_error = nil

  return true if loaded

  load_validations_pass?
end

#run_load_validationsObject

Executes the when_loaded check to determine if the page is loaded, but also clears any previous cache of the loaded state and load error

This is useful if you want to re-run the load validations irrespective of whether the page was previously loaded or not

The loadable object instance is yielded into the block.



65
66
67
68
69
# File 'lib/site_prism/loadable.rb', line 65

def run_load_validations
  self.loaded = false
  self.load_error = nil
  when_loaded
end

#when_loadedObject

Executes the given block after the page is loaded.

The loadable object instance is yielded into the block.

NB: This will only trigger load validations if the page is already not loaded. If you want to verbosely trigger the load validations irrespective, use #run_load_validations; which will clear any previous cache and then re-run the validations



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

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::Error::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