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, :require ]
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
-
#allow(name) ⇒ void
Allows a specific variable to be used - but doesn’t define it in the current context.
-
#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
#to_a!, #to_ary, #to_sexp, #to_yaml
Constructor Details
#initialize ⇒ Context
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_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.
36 37 38 |
# File 'lib/liquidscript/icr/context.rb', line 36 def allowed_variables @allowed_variables end |
#parent ⇒ Parent
The parent of the current context.
24 25 26 |
# File 'lib/liquidscript/icr/context.rb', line 24 def parent @parent end |
#variables ⇒ Hash<Symbol, Variable> (readonly)
The variables that are a part of this context.
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.
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 |
#parameters ⇒ Array<Variable>
All of the parameter variables.
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_a ⇒ Object
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.
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 |