Method: Liquidscript::ICR::Context#get

Defined in:
lib/liquidscript/icr/context.rb

#get(name, options = {}) ⇒ Variable, Boolean

Retrieves a reference to a variable. If the local context doesn’t have a reference, then it will try a few things; first, it will check to see if that variable is one of our allowed variables; second, it will check if the parent has a reference; otherwise, it will add an undefined reference if this context is associated with a class.

Parameters:

  • name (Symbol)

    the variable to reference.

  • options (Hash) (defaults to: {})

    Extra options.

Options Hash (options):

  • :dry_run (Boolean)

    if this is a dry run. In that case, it won’t add an undefined reference.

Returns:

Raises:

See Also:

  • Liquidscript::ICR::Context.{{#parent}
  • Liquidscript::ICR::Context.{{#allowed_variables}
  • Liquidscript::ICR::Context.{{#add_undefined}
  • Liquidscript::ICR::Context.{{#variables}


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/liquidscript/icr/context.rb', line 108

def get(name, options = {})
  variables.fetch(name) do
    case true
    # If the asking variable is an allowed variable, we'll
    # allow it, and just return a variable instance.
    when allowed_variables.include?(name)
      Variable.new(self, name, :allowed => true)
    # If we have a parent, we can ask the parent for the
    # variable.  This takes precedence over the class
    # so we can get a proper reference to the correct
    # variable.
    when !!parent
      parent_get(name, options)
    # If this context is associated with a class, and
    # we're not doing a dry run, then we'll add an
    # undefined.
    when @class && !options[:dry_run]
      add_undefined(name)
    # If we are doing a dry run, however, then just let
    # the caller know that it would have been successful.
    when @class && options[:dry_run]
      true
    # If none of those options fit, raise an error.
    else
      raise InvalidReferenceError.new(name)
    end
  end
end