Class: Liquidscript::Compiler::Base::Callable

Inherits:
Object
  • Object
show all
Defined in:
lib/liquidscript/compiler/base/callable.rb

Instance Method Summary collapse

Constructor Details

#initialize(bind, block) ⇒ Callable

Initialize the callable.

Parameters:

  • bind (Object)

    the object that holds the method (if this represents a method call).

  • block (Symbol, Block)

    if it’s a Symbol, it represents a method on bind; otherwise, it’s a pure block.



12
13
14
15
16
17
18
19
# File 'lib/liquidscript/compiler/base/callable.rb', line 12

def initialize(bind, block)
  @bind = bind
  @block = if block.is_a? Symbol
    :"compile_#{block}"
  else
    block
  end
end

Instance Method Details

#arityNumeric

How many arguments the call can take. If this represents a block that has tricks enabled, then this isn’t an issue; if it’s a method call, however, it becomes important.

Returns:

  • (Numeric)

    the number of arguments it can take.



40
41
42
43
44
45
46
# File 'lib/liquidscript/compiler/base/callable.rb', line 40

def arity
  if @block.is_a? Symbol
    @bind.method(@block).arity
  else
    @block.arity
  end
end

#call(*args) ⇒ Object

Calls the thing that this represents. If this represents a method call, it calls the method with the given arguments; otherwise, it calls #call on the block.

Parameters:

  • args (Object)

    passed to the call.

Returns:

  • (Object)

    the result of the call.



27
28
29
30
31
32
33
# File 'lib/liquidscript/compiler/base/callable.rb', line 27

def call(*args)
  if @block.is_a? Symbol
    @bind.public_send(@block, *args)
  elsif @block.respond_to?(:call)
    @block.call(*args)
  end
end