Class: Liquidscript::ICR::Code
- Inherits:
-
Object
- Object
- Liquidscript::ICR::Code
- Includes:
- Representable
- Defined in:
- lib/liquidscript/icr/code.rb
Overview
An individual code point. This is normally in a set. A code will have an action, and arguments that accompany that action. The arguments list can be however long.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#action ⇒ Symbol
(also: #type)
readonly
The action that this code is associated with.
-
#arguments ⇒ Array
readonly
The arguments that are used for this action.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Access either the metadata or the codes.
-
#[]=(key, value) ⇒ Object
Sets something from the metadata.
-
#initialize(action, *arguments) ⇒ Code
constructor
Initializes the code.
-
#method_missing(method, *args, &block) ⇒ Object
Send the method to @arguments if it doesn’t exist here.
-
#respond_to_missing?(method, include_private = false) ⇒ Boolean
If we don’t respond to it, the @arguments array might.
-
#to_a ⇒ Array
Turns the code into an array, containing the action and the arguments.
-
#value? ⇒ Boolean
If this code respresents something with a definite value.
Methods included from Representable
#to_a!, #to_ary, #to_sexp, #to_yaml
Constructor Details
#initialize(action, *arguments) ⇒ Code
Initializes the code. It takes an action and an argument as its arguments. The action should not change from this point forward.
34 35 36 37 38 |
# File 'lib/liquidscript/icr/code.rb', line 34 def initialize(action, *arguments) @action = action @arguments = arguments @metadata = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Send the method to @arguments if it doesn’t exist here.
109 110 111 |
# File 'lib/liquidscript/icr/code.rb', line 109 def method_missing(method, *args, &block) @arguments.public_send(method, *args, &block) end |
Instance Attribute Details
#action ⇒ Symbol (readonly) Also known as: type
The action that this code is associated with. This should be a symbol.
14 15 16 |
# File 'lib/liquidscript/icr/code.rb', line 14 def action @action end |
#arguments ⇒ Array (readonly)
The arguments that are used for this action. This is an array.
20 21 22 |
# File 'lib/liquidscript/icr/code.rb', line 20 def arguments @arguments end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
22 23 24 |
# File 'lib/liquidscript/icr/code.rb', line 22 def @metadata end |
Instance Method Details
#[](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.
70 71 72 73 74 75 76 |
# File 'lib/liquidscript/icr/code.rb', line 70 def [](key) if argument_key?(key) super 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.
85 86 87 88 89 90 91 |
# File 'lib/liquidscript/icr/code.rb', line 85 def []=(key, value) if argument_key?(key) super else @metadata[key] = value end end |
#respond_to_missing?(method, include_private = false) ⇒ Boolean
If we don’t respond to it, the @arguments array might. Ask them if they do, and if they don’t, respond accordingly.
101 102 103 |
# File 'lib/liquidscript/icr/code.rb', line 101 def respond_to_missing?(method, include_private = false) @arguments.respond_to?(method) end |
#to_a ⇒ Array
Turns the code into an array, containing the action and the arguments. Note that changing this array will not change the code.
45 46 47 48 49 |
# File 'lib/liquidscript/icr/code.rb', line 45 def to_a part = [@action] part.concat(@arguments) part end |
#value? ⇒ Boolean
If this code respresents something with a definite value.
55 56 57 58 59 60 61 |
# File 'lib/liquidscript/icr/code.rb', line 55 def value? @_value ||= ![ :class, :module, :if, :elseif, :unless, :else, :try, :catch, :finally, :while, :for_in, :for_seg, :return, :exec ].include?(@action) end |