Sha256: e46bba37184cdeb8396e04b0c54c568a7aa71f0d01f5e2599e133bdf493b4cf5

Contents?: true

Size: 1.11 KB

Versions: 21

Compression:

Stored size: 1.11 KB

Contents

module Roger
  module Helpers
    # Helper to include the get_callbable method
    module GetCallable
      # Makes callable into a object that responds to call.
      #
      # @param [#call, Symbol, Class] callable If callable already responds to #call will
      #   just return callable, a Symbol will be searched for in the scope parameter, a class
      #   will be instantiated (and checked if it will respond to #call)
      # @param [Hash] map, Mapping to match symbol to a callable
      def get_callable(callable, map)
        return callable if callable.respond_to?(:call)

        callable = map[callable] if callable.is_a?(Symbol) && map.key?(callable)

        callable = callable.new if callable.is_a?(Class)

        if callable.respond_to?(:call)
          callable
        else
          error_message = "Could not resolve #{callable.inspect}. Callable must"
          error_message << "be an object that responds to #call or a symbol that resolve"
          error_message << "to such an object or a class with a #call instance method."
          fail ArgumentError, error_message
        end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
roger-1.7.0 lib/roger/helpers/get_callable.rb
roger-1.6.4 lib/roger/helpers/get_callable.rb
roger-1.6.3 lib/roger/helpers/get_callable.rb
roger-1.6.2 lib/roger/helpers/get_callable.rb
roger-1.6.1 lib/roger/helpers/get_callable.rb
roger-1.6.0 lib/roger/helpers/get_callable.rb
roger-1.5.0 lib/roger/helpers/get_callable.rb
roger-1.4.6 lib/roger/helpers/get_callable.rb
roger-1.4.5 lib/roger/helpers/get_callable.rb
roger-1.4.4 lib/roger/helpers/get_callable.rb
roger-1.4.3 lib/roger/helpers/get_callable.rb
roger-1.4.2 lib/roger/helpers/get_callable.rb
roger-1.3.5 lib/roger/helpers/get_callable.rb
roger-1.4.1 lib/roger/helpers/get_callable.rb
roger-1.4.0 lib/roger/helpers/get_callable.rb
roger-1.3.4 lib/roger/helpers/get_callable.rb
roger-1.3.3 lib/roger/helpers/get_callable.rb
roger-1.3.2 lib/roger/helpers/get_callable.rb
roger-1.3.0 lib/roger/helpers/get_callable.rb
roger-1.2.2 lib/roger/helpers/get_callable.rb