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`.
[ :window, :global, :exports, :console, :this, :arguments ]
Instance Attribute Summary collapse
-
#allowed_variables ⇒ Array<Symbol>
readonly
The variables that are allowed to be used as a global scope, i.e.
-
#parent ⇒ Parent
The parent of the current context.
-
#variables ⇒ Hash<Symbol, Variable>
readonly
The variables that are a part of this context.
Instance Method Summary collapse
-
#get(name) ⇒ Object
(see #variable).
-
#initialize ⇒ Context
constructor
Initializes the context.
-
#parameters ⇒ Array<Variable>
All of the parameter variables.
-
#set(name) ⇒ Object
(see #variable).
- #to_a ⇒ Object
-
#variable(name, type) ⇒ Variable
Returns a variable reference.
Methods included from Representable
Constructor Details
#initialize ⇒ Context
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_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.
33 34 35 |
# File 'lib/liquidscript/icr/context.rb', line 33 def allowed_variables @allowed_variables end |
#parent ⇒ Parent
The parent of the current context.
21 22 23 |
# File 'lib/liquidscript/icr/context.rb', line 21 def parent @parent end |
#variables ⇒ Hash<Symbol, Variable> (readonly)
The variables that are a part of this context.
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 |
#parameters ⇒ Array<Variable>
All of the parameter variables.
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_a ⇒ Object
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.
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 |