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`.

%w(
  window global exports console this arguments
  Infinity NaN undefined eval isFinite isNaN parseFloat
  parseInt decodeURI decodeURIComponent encodeURI encodeURIComponent
  Object Function Boolean Error EvalError InternalError RangeError
  ReferenceError SyntaxError TypeError URIError Number Math
  Date String  RegExp Array Float32Array Float64Array Int16Array
  Int32Array Int8Array Uint16Array Uint32Array Uint8Array
  Uint8ClampedArray ArrayBuffer DataView JSON Intl
).map(&:intern)

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.



49
50
51
52
53
54
# File 'lib/liquidscript/icr/context.rb', line 49

def initialize
  @undefined = []
  @variables = {}
  @allowed_variables = [DEFAULT_ALLOWED_VARIABLES].flatten
  @parents = []
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]


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

def allowed_variables
  @allowed_variables
end

#parentsParent

The parent of the current context.

Returns:

  • (Parent)


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

def parents
  @parents
end

#undefinedObject (readonly)

Returns the value of attribute undefined.



44
45
46
# File 'lib/liquidscript/icr/context.rb', line 44

def undefined
  @undefined
end

#variablesHash<Symbol, Variable> (readonly)

The variables that are a part of this context.

Returns:



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

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)


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

def allow(name)
  allowed_variables << name
end

#class!Object



118
119
120
121
# File 'lib/liquidscript/icr/context.rb', line 118

def class!
  @class = true
  self
end

#get(name, options = {}) ⇒ Object

(see #variable).

Passes ‘:get` as type.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/liquidscript/icr/context.rb', line 91

def get(name, options = {})
  @variables.fetch(name) do
    case true
    when allowed_variables.include?(name)
      Variable.new(self, name)
    when parents.any?
      get_parent(name)
    when @class
      add_undefined(name)
    else
      raise InvalidReferenceError.new(name)
    end
  end
end

#parametersArray<Variable>

All of the parameter variables.

Returns:



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

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

#set(name, options = {}) ⇒ Object

(see #variable).

Passes ‘:set` as type.



109
110
111
112
113
114
115
116
# File 'lib/liquidscript/icr/context.rb', line 109

def set(name, options = {})
  @variables.fetch(name) do
    @undefined.reject! { |(n, _)| n == name }
    @variables[name] =
      Variable.new(self, name,
                   {:class => @class}.merge(options))
  end
end

#to_aObject



123
124
125
# File 'lib/liquidscript/icr/context.rb', line 123

def to_a
  @variables.keys
end

#variable(name, type, options = {}) ⇒ 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:



66
67
68
69
70
# File 'lib/liquidscript/icr/context.rb', line 66

def variable(name, type, options = {})
  if [:get, :set].include?(type)
    send(type, name, options = {})
  end
end