Sha256: 01e4822348b88e33f9b1d19b5ede1bb69c85579243063c54ed346cf54b57349e

Contents?: true

Size: 1.49 KB

Versions: 34

Compression:

Stored size: 1.49 KB

Contents

module Liquidscript
  module Compiler
    class Base
      class Callable

        # Initialize the callable.
        #
        # @param bind [Object] the object that holds the method (if
        #   this represents a method call).
        # @param block [Symbol, Block] if it's a Symbol, it represents
        #   a method on bind; otherwise, it's a pure block.
        def initialize(bind, block)
          @bind = bind
          @block = if block.is_a? Symbol
            :"compile_#{block}"
          else
            block
          end
        end

        # 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.
        #
        # @param args [Object] passed to the call.
        # @return [Object] the result of the call.
        def call(*args)
          if @block.is_a? Symbol
            @bind.public_send(@block, *args)
          elsif @block.respond_to?(:call)
            @block.call(*args)
          end
        end

        # 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.
        #
        # @return [Numeric] the number of arguments it can take.
        def arity
          if @block.is_a? Symbol
            @bind.method(@block).arity
          else
            @block.arity
          end
        end

      end
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
liquidscript-0.7.12 lib/liquidscript/compiler/base/callable.rb
liquidscript-0.7.11 lib/liquidscript/compiler/base/callable.rb
liquidscript-0.7.10 lib/liquidscript/compiler/base/callable.rb
liquidscript-0.7.9 lib/liquidscript/compiler/base/callable.rb
liquidscript-0.7.8 lib/liquidscript/compiler/base/callable.rb
liquidscript-0.7.7 lib/liquidscript/compiler/base/callable.rb
liquidscript-0.7.6 lib/liquidscript/compiler/base/callable.rb
liquidscript-0.7.5 lib/liquidscript/compiler/base/callable.rb
liquidscript-0.7.4 lib/liquidscript/compiler/base/callable.rb
liquidscript-0.7.3 lib/liquidscript/compiler/base/callable.rb
liquidscript-0.7.2 lib/liquidscript/compiler/base/callable.rb
liquidscript-0.7.1 lib/liquidscript/compiler/base/callable.rb
liquidscript-0.7.0 lib/liquidscript/compiler/base/callable.rb
liquidscript-0.6.5 lib/liquidscript/compiler/base/callable.rb
liquidscript-0.6.4 lib/liquidscript/compiler/base/callable.rb
liquidscript-0.6.3 lib/liquidscript/compiler/base/callable.rb
liquidscript-0.6.2 lib/liquidscript/compiler/base/callable.rb
liquidscript-0.6.1 lib/liquidscript/compiler/base/callable.rb
liquidscript-0.6.0 lib/liquidscript/compiler/base/callable.rb
liquidscript-0.5.1 lib/liquidscript/compiler/base/callable.rb