class UsefulMatchers::Matchers::Independent::Hashes::IncludeKeyAndValueMatcher

Public Class Methods

new(key) click to toggle source
# File lib/useful_matchers/matchers/independent/hashes/include_key_and_value.rb, line 10
def initialize(key)
  @key = key
  
  @value     = nil
  # Use boolean flag since we can't test whether @value is set by testing whether @value is nil
  # because we should be able to evaluate nil values.
  @value_set = false
  @subject   = nil
end

Public Instance Methods

failure_message() click to toggle source
# File lib/useful_matchers/matchers/independent/hashes/include_key_and_value.rb, line 36
def failure_message
  if @value_set
    "expected that the value for key #{format_key} in the hash is #{format_value}"
  else
    "expected that the hash contains #{format_key} as a key"
  end
end
Also aliased as: failure_message_for_should
failure_message_for_should()
Alias for: failure_message
failure_message_for_should_not()
failure_message_when_negated() click to toggle source
# File lib/useful_matchers/matchers/independent/hashes/include_key_and_value.rb, line 45
def failure_message_when_negated
  if @value_set
    "expected that the value for key #{format_key} in the hash isn't #{format_value}"
  else
    "expected that the hash doesn't contain #{format_key} as a key"
  end
end
matches?(subject) click to toggle source
# File lib/useful_matchers/matchers/independent/hashes/include_key_and_value.rb, line 20
def matches?(subject)
  @subject = subject
  
  if @value_set
    ensure_subject_has_target_key && ensure_target_key_corresponds_to_target_value
  else
    ensure_subject_has_target_key
  end
end
with_value(value) click to toggle source
# File lib/useful_matchers/matchers/independent/hashes/include_key_and_value.rb, line 30
def with_value(value)
  @value     = value
  @value_set = true
  self
end

Private Instance Methods

ensure_subject_has_target_key() click to toggle source
# File lib/useful_matchers/matchers/independent/hashes/include_key_and_value.rb, line 56
def ensure_subject_has_target_key
  @subject.keys.include?(@key)
end
ensure_target_key_corresponds_to_target_value() click to toggle source
# File lib/useful_matchers/matchers/independent/hashes/include_key_and_value.rb, line 60
def ensure_target_key_corresponds_to_target_value
  @subject[@key] == @value
end
format_key() click to toggle source
# File lib/useful_matchers/matchers/independent/hashes/include_key_and_value.rb, line 64
def format_key
  format_parameter(@key)
end
format_parameter(param) click to toggle source
# File lib/useful_matchers/matchers/independent/hashes/include_key_and_value.rb, line 72
def format_parameter(param)
  formatted_param = param
  
  case param
  when ''
    formatted_param = 'empty string'
  when nil
    formatted_param = 'nil'
  when Symbol
    formatted_param = ":#{param.to_s}"
  end
  
  formatted_param
end
format_value() click to toggle source
# File lib/useful_matchers/matchers/independent/hashes/include_key_and_value.rb, line 68
def format_value
  format_parameter(@value)
end