Class: Liquidscript::ICR::Set
- Includes:
- Representable
- Defined in:
- lib/liquidscript/icr/set.rb
Overview
Represents a set of instruction codes. Can contain metadata about the set, like whether or not it can be inlined, if a new context needs to be applied, etc.
Instance Attribute Summary collapse
-
#metadata ⇒ Hash
readonly
The metadata that is applied to the set.
Attributes inherited from Code
Instance Method Summary collapse
- #<<(*v) ⇒ Object (also: #push)
-
#[](key) ⇒ Object
Access either the metadata or the codes.
-
#[]=(key, value) ⇒ Object
Sets something from the metadata.
-
#add(action, *arguments) ⇒ Object
Adds a code to the code list.
-
#codes ⇒ Array<Code>
Outputs the codes in this set.
- #context ⇒ Object
- #context=(new_context) ⇒ Object
- #contexts ⇒ Object
-
#initialize ⇒ Set
constructor
Initialize the set.
-
#locals ⇒ Array<Symbol>
A list of all the local variables in the current scope.
-
#method_missing(method, *args, &block) ⇒ Object
For methods that we don’t respond to, send them to the interal array.
-
#parameters ⇒ Array<Symbol>
A list of components (or arguments) that are in the current scope.
-
#respond_to_missing?(method, include_private = false) ⇒ Boolean
Tells ruby that we respond to some methods.
-
#to_a ⇒ Array
Turns the set into an array.
-
#variables ⇒ Array<Symbol>
A list of all variables in the current scope.
Methods included from Representable
#to_a!, #to_ary, #to_sexp, #to_yaml
Constructor Details
#initialize ⇒ Set
Initialize the set.
18 19 20 21 22 23 |
# File 'lib/liquidscript/icr/set.rb', line 18 def initialize @metadata = {} @code = [] @contexts = [] @action = :exec end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
For methods that we don’t respond to, send them to the interal array.
152 153 154 |
# File 'lib/liquidscript/icr/set.rb', line 152 def method_missing(method, *args, &block) @code.public_send(method, *args, &block) end |
Instance Attribute Details
#metadata ⇒ Hash (readonly)
The metadata that is applied to the set.
13 14 15 |
# File 'lib/liquidscript/icr/set.rb', line 13 def @metadata end |
Instance Method Details
#<<(*v) ⇒ Object Also known as: push
49 50 51 52 53 |
# File 'lib/liquidscript/icr/set.rb', line 49 def <<(*v) v.select { |p| p }.each do |part| @code << part end end |
#[](key) ⇒ Object
Access either the metadata or the codes. If the accessor is a Symbol, it access the metadata; if it the accessor is a Numeric, it access the codes.
116 117 118 119 120 121 122 |
# File 'lib/liquidscript/icr/set.rb', line 116 def [](key) if key.is_a? Numeric @code[key] else @metadata[key] end end |
#[]=(key, value) ⇒ Object
Sets something from the metadata. Unlike the accessor, it does not distinguish between Numeric and Symbol keys.
131 132 133 |
# File 'lib/liquidscript/icr/set.rb', line 131 def []=(key, value) @metadata[key] = value end |
#add(action, *arguments) ⇒ Object
Adds a code to the code list. This is just a convienince method.
45 46 47 |
# File 'lib/liquidscript/icr/set.rb', line 45 def add(action, *arguments) @code << Code.new(action, arguments) end |
#codes ⇒ Array<Code>
Outputs the codes in this set.
105 106 107 |
# File 'lib/liquidscript/icr/set.rb', line 105 def codes @code end |
#context ⇒ Object
26 27 28 |
# File 'lib/liquidscript/icr/set.rb', line 26 def context contexts.last || @metadata.fetch(:parent).context end |
#context=(new_context) ⇒ Object
35 36 37 |
# File 'lib/liquidscript/icr/set.rb', line 35 def context=(new_context) contexts << new_context end |
#contexts ⇒ Object
30 31 32 |
# File 'lib/liquidscript/icr/set.rb', line 30 def contexts @contexts end |
#locals ⇒ Array<Symbol>
A list of all the local variables in the current scope. Local variables are defined as variables that were a) not passed in by function execution as arguments and b) are set within the current context.
64 65 66 |
# File 'lib/liquidscript/icr/set.rb', line 64 def locals variables - parameters end |
#parameters ⇒ Array<Symbol>
A list of components (or arguments) that are in the current scope. Defined as variables that were passed in by function execution as arguments.
74 75 76 |
# File 'lib/liquidscript/icr/set.rb', line 74 def parameters context.parameters.map(&:name) end |
#respond_to_missing?(method, include_private = false) ⇒ Boolean
Tells ruby that we respond to some methods. Passes the method name to the internal array, asking if it responds to it.
144 145 146 |
# File 'lib/liquidscript/icr/set.rb', line 144 def respond_to_missing?(method, include_private = false) @code.respond_to?(method, include_private) end |
#to_a ⇒ Array
Turns the set into an array. Includes the metadata information and the actual internal array. Note that this is not the array used in #method_missing - that actually operates on the internal array.
94 95 96 97 98 99 100 |
# File 'lib/liquidscript/icr/set.rb', line 94 def to_a part = [@action] part << [:_context, context] if contexts.any? part.concat(@metadata.to_a.map { |(m, i)| [:"_#{m}", i] }) part.concat(@code) part end |
#variables ⇒ Array<Symbol>
A list of all variables in the current scope.
82 83 84 |
# File 'lib/liquidscript/icr/set.rb', line 82 def variables context.variables.keys - context.allowed_variables end |