Sha256: da5857daddbe2975a9dfd3279231e67661e2f4f855099822b8f1ce7e16532484

Contents?: true

Size: 941 Bytes

Versions: 2

Compression:

Stored size: 941 Bytes

Contents

module Fear
  # @private
  module Utils
    UNDEFINED = Object.new.freeze

    class << self
      def assert_arg_or_block!(method_name, *args)
        unless block_given? ^ !args.empty?
          raise ArgumentError, "##{method_name} accepts either one argument or block"
        end
      end

      def with_block_or_argument(method_name, arg = UNDEFINED, block = nil)
        if block.nil? ^ arg.equal?(UNDEFINED)
          yield(block || arg)
        else
          raise ArgumentError, "#{method_name} accepts either block or partial function"
        end
      end

      def assert_type!(value, *types)
        if types.none? { |type| value.is_a?(type) }
          raise TypeError, "expected `#{value.inspect}` to be of #{types.join(', ')} class"
        end
      end

      def return_or_call_proc(value)
        if value.respond_to?(:call)
          value.call
        else
          value
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fear-1.0.0 lib/fear/utils.rb
fear-0.11.0 lib/fear/utils.rb