Sha256: c3cf4c1f534bd2ab87650ae92892f14bcd7696d864628dd24b7074260a2b7f1e

Contents?: true

Size: 963 Bytes

Versions: 3

Compression:

Stored size: 963 Bytes

Contents

# frozen_string_literal: true

module Mutant
  # Utility methods
  module Util
    # Error raised by `Util.one` if size is less than zero or greater than one
    SizeError = Class.new(IndexError)

    # Return only element in array if it contains exactly one member
    #
    # @param array [Array]
    #
    # @return [Object] first entry
    def self.one(array)
      case array
      in [value]
        value
      else
        fail SizeError, "expected size to be exactly 1 but size was #{array.size}"
      end
    end

    # Return only element in array if it contains max one member
    #
    # @param array [Array]
    #
    # @return [Object] first entry
    # @return [nil] if empty
    #
    # rubocop:disable Lint/EmptyInPattern
    def self.max_one(array)
      case array
      in []
      in [value]
        value
      else
        fail SizeError, "expected size to be max 1 but size was #{array.size}"
      end
    end
  end # Util
end # Mutant

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mutant-0.12.4 lib/mutant/util.rb
mutant-0.12.3 lib/mutant/util.rb
mutant-0.12.2 lib/mutant/util.rb