Class: Liquidscript::ICR::Code

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

Set

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • action (Symbol)
  • arguments (Array)


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.

Returns:

  • (Object)


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

#actionSymbol (readonly) Also known as: type

The action that this code is associated with. This should be a symbol.

Returns:

  • (Symbol)


14
15
16
# File 'lib/liquidscript/icr/code.rb', line 14

def action
  @action
end

#argumentsArray (readonly)

The arguments that are used for this action. This is an array.

Returns:



20
21
22
# File 'lib/liquidscript/icr/code.rb', line 20

def arguments
  @arguments
end

#metadataObject (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.

Parameters:

  • key (Symbol, Numeric)

    the key.

Returns:

  • (Object)


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.

Parameters:

  • key (Object)

    the key.

  • value (Object)

    the value.

Returns:

  • (Object)


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.

Parameters:

  • method (Symbol)

    the method to check.

  • include_private (Boolean) (defaults to: false)

    whether or not to include private methods.

Returns:

  • (Boolean)


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_aArray

Turns the code into an array, containing the action and the arguments. Note that changing this array will not change the code.

Returns:



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.

Returns:

  • (Boolean)


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