Skip to content

Instantly share code, notes, and snippets.

@Peeja
Created October 9, 2008 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Peeja/15799 to your computer and use it in GitHub Desktop.
Save Peeja/15799 to your computer and use it in GitHub Desktop.
module HaveAMatcher
class HaveA
def method_missing(attribute)
@attribute ||= attribute
self
end
def matches?(target)
@target = target
raise ArgumentError, "No attribute specified for have(:a)" unless @attribute
@value = @target.__send__(@attribute)
!(@value.nil? || @value.empty?)
end
def failure_message
"expected #{@target} to have #{an} #{@attribute}, but its #{@attribute} was #{@value.inspect}."
end
def negative_failure_message
"expected #{@target} not to have #{an} #{@attribute}, but its #{@attribute} was #{@value.inspect}."
end
def an
vowels = [?a, ?e, ?i, ?o, ?u]
vowels += [?A, ?E, ?I, ?O, ?U]
if vowels.include? @attribute.to_s[0]
"an"
else
"a"
end
end
end
def have(n=nil)
if n.nil?
HaveA.new
else
super
end
end
alias :have_a :have
alias :have_an :have
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment