class HaveUnique

Matcher to verify that a hash's key is unique in a collection of other hashes a full class is required to implement the in method Use like: expect(hash).to have_unique(:id).in(hashes)

Public Class Methods

new(key) click to toggle source
# File lib/storexplore/testing/matchers/have_unique_matcher.rb, line 27
def initialize(key)
  @key = key
end

Public Instance Methods

description() click to toggle source
# File lib/storexplore/testing/matchers/have_unique_matcher.rb, line 49
def description
  "expected an hash or object with a unique #{@key} in #{@collection}"
end
failure_message_for_should() click to toggle source
# File lib/storexplore/testing/matchers/have_unique_matcher.rb, line 45
def failure_message_for_should
  "expected #{value_expression(actual)} (=#{value(actual)}) to be unique in #{@collection}"
end
in(collection) click to toggle source
# File lib/storexplore/testing/matchers/have_unique_matcher.rb, line 31
def in(collection)
  @collection = collection
  @index = Hash.new(0)
  collection.each do |item|
    @index[value(item)] += 1
  end
  self
end
matches?(actual) click to toggle source
# File lib/storexplore/testing/matchers/have_unique_matcher.rb, line 40
def matches?(actual)
  @actual = actual
  @index[value(actual)] == 1
end

Private Instance Methods

value(actual) click to toggle source
# File lib/storexplore/testing/matchers/have_unique_matcher.rb, line 54
def value(actual)
  if actual.instance_of?(Hash)
    actual[@key]
  else
    actual.send(@key)
  end
end
value_expression(actual) click to toggle source
# File lib/storexplore/testing/matchers/have_unique_matcher.rb, line 61
def value_expression(actual)
  if actual.instance_of?(Hash)
    "#{actual}[#{@key}]"
  else
    "#{actual}.#{@key}"
  end
end