Sha256: ef8bd720a6769445b3d790e9bdbcc00ebb5528f649dfe401bd343d072282b5d7

Contents?: true

Size: 1.15 KB

Versions: 3

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

module Fear
  # @private
  module Utils
    EMPTY_STRING = ""
    public_constant :EMPTY_STRING

    UNDEFINED = Object.new.freeze
    public_constant :UNDEFINED

    EMPTY_HASH = {}.freeze
    public_constant :EMPTY_HASH

    EMPTY_ARRAY = [].freeze
    public_constant :EMPTY_ARRAY

    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.()
        else
          value
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fear-3.0.0 lib/fear/utils.rb
fear-2.0.1 lib/fear/utils.rb
fear-2.0.0 lib/fear/utils.rb