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
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Representable

#to_a!, #to_ary, #to_yaml

Constructor Details

#initializeContext

Initializes the context.



38
39
40
41
# File 'lib/liquidscript/icr/context.rb', line 38

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]


33
34
35
# File 'lib/liquidscript/icr/context.rb', line 33

def allowed_variables
  @allowed_variables
end

#parentParent

The parent of the current context.

Returns:

  • (Parent)


21
22
23
# File 'lib/liquidscript/icr/context.rb', line 21

def parent
  @parent
end

#variablesHash<Symbol, Variable> (readonly)

The variables that are a part of this context.

Returns:



26
27
28
# File 'lib/liquidscript/icr/context.rb', line 26

def variables
  @variables
end

Instance Method Details

#get(name) ⇒ Object

(see #variable).

Passes ‘:get` as type.



77
78
79
# File 'lib/liquidscript/icr/context.rb', line 77

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

#parametersArray<Variable>

All of the parameter variables.

Returns:



70
71
72
# File 'lib/liquidscript/icr/context.rb', line 70

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

#set(name) ⇒ Object

(see #variable).

Passes ‘:set` as type.



84
85
86
# File 'lib/liquidscript/icr/context.rb', line 84

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

#to_aObject



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

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:



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/liquidscript/icr/context.rb', line 53

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