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.



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

#apply(*args) ⇒ Object

This applies only as many arguments as the block or function needs. This is just so that calling a method is easier, so the developer doesn’t have to worry about arity and such.



42
43
44
45
46
47
48
49
50
# File 'lib/liquidscript/compiler/base/callable.rb', line 42

def apply(*args)
  return call if arity == 0

  if block_given?
    call(*yield[0..arity])
  else
    call(*args[0..arity])
  end
end

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



57
58
59
60
61
62
63
# File 'lib/liquidscript/compiler/base/callable.rb', line 57

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.



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