Sha256: 7d7d7f4ee7fa3ff1f2dfc9acecdc6c0efa0e8e8851ee647e00f9e48be2609b0f

Contents?: true

Size: 756 Bytes

Versions: 3

Compression:

Stored size: 756 Bytes

Contents

module NxtSchema
  class Callable
    def initialize(callable, target = nil, *args)
      @callable = callable
      @target = target
      @args = args
    end

    def call
      return callable if value?
      return callable.call(*args_from_arity) if proc?

      target.send(callable, *args_from_arity)
    end

    def method?
      @method ||= callable.class.in?([Symbol, String]) && target.respond_to?(callable)
    end

    def proc?
      @proc ||= callable.respond_to?(:call)
    end

    def value?
      !method? && !proc?
    end

    private

    attr_reader :callable, :target, :args

    def arity
      proc? ? callable.arity : 0
    end

    def args_from_arity
      @args_from_arity ||= ([target] + args).take(arity)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nxt_schema-1.0.2 lib/nxt_schema/callable.rb
nxt_schema-1.0.1 lib/nxt_schema/callable.rb
nxt_schema-1.0.0 lib/nxt_schema/callable.rb