Class: Liquidscript::ICR::Context

Inherits:
Object
  • Object
show all
Includes:
Representable
Defined in:
lib/liquidscript/icr/context.rb

Overview

Handles variables within blocks. Each variable will get a reference in each context. When retrieving the value of a variable, if the variable was not introduced in the scope, it will look to its parent for the value of the variable. When setting the value of a variable, a new variable is forcibly created.

Constant Summary collapse

DEFAULT_ALLOWED_VARIABLES =

The variables that are allowed to be used as a global scope, i.e. used in a ‘get` context without a previous `set`.

[
  :window, :global, :exports, :console, :this, :arguments,
  :require
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Representable

#to_a!, #to_ary, #to_sexp, #to_yaml

Constructor Details

#initializeContext

Initializes the context.



41
42
43
44
# File 'lib/liquidscript/icr/context.rb', line 41

def initialize
  @variables = {}
  @allowed_variables = [DEFAULT_ALLOWED_VARIABLES].flatten
end

Instance Attribute Details

#allowed_variablesArray<Symbol> (readonly)

The variables that are allowed to be used as a global scope, i.e. used in a ‘get` context without a previous set.

Returns:

See Also:

  • [DEFAULT_ALLOWED_VARIABLES]


36
37
38
# File 'lib/liquidscript/icr/context.rb', line 36

def allowed_variables
  @allowed_variables
end

#parentParent

The parent of the current context.

Returns:

  • (Parent)


24
25
26
# File 'lib/liquidscript/icr/context.rb', line 24

def parent
  @parent
end

#variablesHash<Symbol, Variable> (readonly)

The variables that are a part of this context.

Returns:



29
30
31
# File 'lib/liquidscript/icr/context.rb', line 29

def variables
  @variables
end

Instance Method Details

#allow(name) ⇒ void

This method returns an undefined value.

Allows a specific variable to be used - but doesn’t define it in the current context.

Parameters:

  • name (Symbol)


75
76
77
# File 'lib/liquidscript/icr/context.rb', line 75

def allow(name)
  allowed_variables << name
end

#get(name) ⇒ Object

(see #variable).

Passes ‘:get` as type.



89
90
91
# File 'lib/liquidscript/icr/context.rb', line 89

def get(name)
  variable(name, :get)
end

#parametersArray<Variable>

All of the parameter variables.

Returns:



82
83
84
# File 'lib/liquidscript/icr/context.rb', line 82

def parameters
  @variables.values.select(&:parameter?)
end

#set(name) ⇒ Object

(see #variable).

Passes ‘:set` as type.



96
97
98
# File 'lib/liquidscript/icr/context.rb', line 96

def set(name)
  variable(name, :set)
end

#to_aObject



100
101
102
# File 'lib/liquidscript/icr/context.rb', line 100

def to_a
  @variables.keys
end

#variable(name, type) ⇒ Variable

Returns a variable reference. If checks the local variables first; if it doesn’t exist there, then if the type is ‘:get`, checks the parent; otherwise, sets the value of the variable. If there is no parent and the type is `:get`, it raises an Liquidscript::InvalidReferenceError.

Parameters:

  • name (Symbol)

    the name of the variable.

  • type (Symbol)

    the type of use. Should be ‘:get` or `:set`.

Returns:



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/liquidscript/icr/context.rb', line 56

def variable(name, type)
  @variables.fetch(name) do
    if type == :set
      @variables[name] = Variable.new(self, name)
    elsif allowed_variables.include?(name)
      Variable.new(self, name)
    elsif type == :get && parent
      parent.get(name)
    else
      raise InvalidReferenceError.new(name)
    end
  end
end