Class: Liquidscript::ICR::Set

Inherits:
Code
  • Object
show all
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

Attributes inherited from Code

#action, #arguments

Instance Method Summary collapse

Methods included from Representable

#to_a!, #to_ary, #to_sexp, #to_yaml

Constructor Details

#initializeSet

Initialize the set.



18
19
20
21
22
# File 'lib/liquidscript/icr/set.rb', line 18

def initialize
   = {}
  @code = []
  @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.

Returns:

  • (Object)


149
150
151
# File 'lib/liquidscript/icr/set.rb', line 149

def method_missing(method, *args, &block)
  @code.public_send(method, *args, &block)
end

Instance Attribute Details

#metadataHash (readonly)

The metadata that is applied to the set.

Returns:

  • (Hash)


13
14
15
# File 'lib/liquidscript/icr/set.rb', line 13

def 
  
end

Instance Method Details

#<<(*v) ⇒ Object Also known as: push



46
47
48
49
50
# File 'lib/liquidscript/icr/set.rb', line 46

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.

Parameters:

  • key (Symbol, Numeric)

    the key.

Returns:

  • (Object)


113
114
115
116
117
118
119
# File 'lib/liquidscript/icr/set.rb', line 113

def [](key)
  if key.is_a? Numeric
    @code[key]
  else
    [key]
  end
end

#[]=(key, value) ⇒ Object

Sets something from the metadata. Unlike the accessor, it does not distinguish between Numeric and Symbol keys.

Parameters:

  • key (Object)

    the key.

  • value (Object)

    the value.

Returns:

  • (Object)


128
129
130
# File 'lib/liquidscript/icr/set.rb', line 128

def []=(key, value)
  [key] = value
end

#add(action, *arguments) ⇒ Object

Adds a code to the code list. This is just a convienince method.

Parameters:

  • action (Symbol)

    a symbol representing the action.

  • arguments

    the arguments for the code.



42
43
44
# File 'lib/liquidscript/icr/set.rb', line 42

def add(action, *arguments)
  @code << Code.new(action, arguments)
end

#codesArray<Code>

Outputs the codes in this set.

Returns:



102
103
104
# File 'lib/liquidscript/icr/set.rb', line 102

def codes
  @code
end

#contextObject



25
26
27
28
29
# File 'lib/liquidscript/icr/set.rb', line 25

def context
  .fetch(:context) do
    .fetch(:parent).context
  end
end

#context=(new_context) ⇒ Object



32
33
34
# File 'lib/liquidscript/icr/set.rb', line 32

def context=(new_context)
  [:context] = new_context
end

#localsArray<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.

Returns:



61
62
63
# File 'lib/liquidscript/icr/set.rb', line 61

def locals
  variables - parameters
end

#parametersArray<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.

Returns:



71
72
73
# File 'lib/liquidscript/icr/set.rb', line 71

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.

Parameters:

  • method (Symbol)

    the method to check.

  • include_private (Boolean) (defaults to: false)

    whether or not to include private methods.

Returns:

  • (Boolean)

    whether or not we respond to that method.



141
142
143
# File 'lib/liquidscript/icr/set.rb', line 141

def respond_to_missing?(method, include_private = false)
  @code.respond_to?(method, include_private)
end

#to_aArray

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.

Returns:



91
92
93
94
95
96
97
# File 'lib/liquidscript/icr/set.rb', line 91

def to_a
  [
    @action,
    *.to_a.map { |(m, i)| [:"_#{m}", i] },
    *@code
  ]
end

#variablesArray<Symbol>

A list of all variables in the current scope.

Returns:



79
80
81
# File 'lib/liquidscript/icr/set.rb', line 79

def variables
  context.variables.keys - context.allowed_variables
end