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)


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

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 temporarily clears any previously cached loaded state / load error(s), to ensure load validations are re-ran fully

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.



66
67
68
69
70
71
72
# File 'lib/site_prism/loadable.rb', line 66

def run_load_validations(&)
  previously_loaded = loaded

  self.loaded = false
  self.load_error = nil
  when_loaded(previously_loaded, &)
end

#when_loaded(previously_loaded = loaded) ⇒ Object

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

Parameters:

  • previously_loaded (Boolean, nil) (defaults to: loaded)

    Original loaded value (which may be nil), in case we are nested inside another ‘#when_loaded` block or called prior to the `#run_load_validations` invocation



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

def when_loaded(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
  # Restore the previous loaded state only if validations passed (i.e., loaded? was true).
  self.loaded = previously_loaded if loaded
end