Class: Liquidscript::ICR::Context
- Inherits:
-
Object
- Object
- Liquidscript::ICR::Context
- 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
-
#allowed_variables ⇒ Array<Symbol>
readonly
The variables that are allowed to be used as a global scope, i.e.
-
#parents ⇒ Parent
The parent of the current context.
-
#undefined ⇒ Object
readonly
Returns the value of attribute undefined.
-
#variables ⇒ Hash<Symbol, Variable>
readonly
The variables that are a part of this context.
Instance Method Summary collapse
-
#allow(name) ⇒ void
Allows a specific variable to be used - but doesn’t define it in the current context.
- #class! ⇒ Object
-
#get(name, options = {}) ⇒ Object
(see #variable).
-
#initialize ⇒ Context
constructor
Initializes the context.
-
#parameters ⇒ Array<Variable>
All of the parameter variables.
-
#set(name, options = {}) ⇒ Object
(see #variable).
- #to_a ⇒ Object
-
#variable(name, type, options = {}) ⇒ Variable
Returns a variable reference.
Methods included from Representable
#to_a!, #to_ary, #to_sexp, #to_yaml
Constructor Details
#initialize ⇒ Context
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_variables ⇒ Array<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.
42 43 44 |
# File 'lib/liquidscript/icr/context.rb', line 42 def allowed_variables @allowed_variables end |
#parents ⇒ Parent
The parent of the current context.
30 31 32 |
# File 'lib/liquidscript/icr/context.rb', line 30 def parents @parents end |
#undefined ⇒ Object (readonly)
Returns the value of attribute undefined.
44 45 46 |
# File 'lib/liquidscript/icr/context.rb', line 44 def undefined @undefined end |
#variables ⇒ Hash<Symbol, Variable> (readonly)
The variables that are a part of this context.
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.
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, = {}) @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 |
#parameters ⇒ Array<Variable>
All of the parameter variables.
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, = {}) @variables.fetch(name) do @undefined.reject! { |(n, _)| n == name } @variables[name] = Variable.new(self, name, {:class => @class}.merge()) end end |
#to_a ⇒ Object
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.
66 67 68 69 70 |
# File 'lib/liquidscript/icr/context.rb', line 66 def variable(name, type, = {}) if [:get, :set].include?(type) send(type, name, = {}) end end |