class Enumerize::Integrations::RSpec::Matcher

Attributes

expected_attr[RW]
expected_default[RW]
expected_i18n_scope[RW]
expected_multiple[RW]
expected_predicates[RW]
expected_scope[RW]
expected_values[RW]
subject[RW]

Public Class Methods

new(expected_attr) click to toggle source
# File lib/enumerize/integrations/rspec/matcher.rb, line 8
def initialize(expected_attr)
  self.expected_attr = expected_attr
end

Public Instance Methods

description() click to toggle source
# File lib/enumerize/integrations/rspec/matcher.rb, line 50
def description
  description  = "define enumerize :#{expected_attr}"
  description += " in: #{quote_values(expected_values)}" if expected_values
  description += " with #{expected_default.inspect} as default value" if expected_default
  description += " i18n_scope: #{expected_i18n_scope.inspect}" if expected_i18n_scope
  description += " predicates: #{expected_predicates.inspect}" if expected_predicates
  description += " multiple: #{expected_multiple.inspect}" if expected_multiple
  description += " scope: #{expected_scope.inspect}" if expected_scope

  description
end
failure_message() click to toggle source
# File lib/enumerize/integrations/rspec/matcher.rb, line 42
def failure_message
  "Expected #{expectation}"
end
failure_message_when_negated() click to toggle source
# File lib/enumerize/integrations/rspec/matcher.rb, line 46
def failure_message_when_negated
  "Did not expect #{expectation}"
end
in(*expected_values) click to toggle source
# File lib/enumerize/integrations/rspec/matcher.rb, line 12
def in(*expected_values)
  self.expected_values = expected_values.flatten
  self
end
matches?(subject) click to toggle source
# File lib/enumerize/integrations/rspec/matcher.rb, line 62
def matches?(subject)
  self.subject = subject
  matches      = true

  matches &= matches_attribute?
  matches &= matches_values? if expected_values
  matches &= matches_default_value? if expected_default
  matches &= matches_i18n_scope? if expected_i18n_scope
  matches &= matches_predicates? if expected_predicates
  matches &= matches_multiple? if expected_multiple
  matches &= matches_scope? if expected_scope

  matches
end
with_default(expected_default) click to toggle source
# File lib/enumerize/integrations/rspec/matcher.rb, line 17
def with_default(expected_default)
  self.expected_default = expected_default.to_s
  self
end
with_i18n_scope(expected_i18n_scope) click to toggle source
# File lib/enumerize/integrations/rspec/matcher.rb, line 22
def with_i18n_scope(expected_i18n_scope)
  self.expected_i18n_scope = expected_i18n_scope
  self
end
with_multiple(expected_multiple) click to toggle source
# File lib/enumerize/integrations/rspec/matcher.rb, line 32
def with_multiple(expected_multiple)
  self.expected_multiple = expected_multiple
  self
end
with_predicates(expected_predicates) click to toggle source
# File lib/enumerize/integrations/rspec/matcher.rb, line 27
def with_predicates(expected_predicates)
  self.expected_predicates = expected_predicates
  self
end
with_scope(expected_scope) click to toggle source
# File lib/enumerize/integrations/rspec/matcher.rb, line 37
def with_scope(expected_scope)
  self.expected_scope = expected_scope
  self
end

Private Instance Methods

attributes() click to toggle source
# File lib/enumerize/integrations/rspec/matcher.rb, line 150
def attributes
  subject_class.enumerized_attributes.attributes[expected_attr.to_s]
end
enumerized_default() click to toggle source
# File lib/enumerize/integrations/rspec/matcher.rb, line 142
def enumerized_default
  @enumerized_default ||= attributes.default_value
end
enumerized_value_hash() click to toggle source
# File lib/enumerize/integrations/rspec/matcher.rb, line 146
def enumerized_value_hash
  @enumerized_value_hash ||= attributes.instance_variable_get('@value_hash')
end
enumerized_values() click to toggle source
# File lib/enumerize/integrations/rspec/matcher.rb, line 138
def enumerized_values
  @enumerized_values ||= attributes.values.sort
end
expectation() click to toggle source
# File lib/enumerize/integrations/rspec/matcher.rb, line 82
def expectation
  "#{subject.class.name} to #{description}"
end
matches_array_values?() click to toggle source
# File lib/enumerize/integrations/rspec/matcher.rb, line 94
def matches_array_values?
  sorted_values == enumerized_values
end
matches_attribute?() click to toggle source
# File lib/enumerize/integrations/rspec/matcher.rb, line 86
def matches_attribute?
  attributes.present?
end
matches_default_value?() click to toggle source
# File lib/enumerize/integrations/rspec/matcher.rb, line 103
def matches_default_value?
  expected_default == enumerized_default
end
matches_hash_values?() click to toggle source
# File lib/enumerize/integrations/rspec/matcher.rb, line 98
def matches_hash_values?
  return unless expected_values.first.is_a?(Hash)
  expected_values.first.all? { |k, v| enumerized_value_hash[k.to_s] == v; }
end
matches_i18n_scope?() click to toggle source
# File lib/enumerize/integrations/rspec/matcher.rb, line 107
def matches_i18n_scope?
  attributes.i18n_scope == expected_i18n_scope
end
matches_multiple?() click to toggle source
# File lib/enumerize/integrations/rspec/matcher.rb, line 119
def matches_multiple?
  subject.public_send(expected_attr).is_a?(Enumerize::Set)
end
matches_predicates?() click to toggle source
# File lib/enumerize/integrations/rspec/matcher.rb, line 111
def matches_predicates?
  if expected_predicates.is_a?(TrueClass)
    subject.respond_to?("#{enumerized_values.first}?")
  else
    subject.respond_to?("#{expected_attr}_#{attributes.values.first}?")
  end
end
matches_scope?() click to toggle source
# File lib/enumerize/integrations/rspec/matcher.rb, line 123
def matches_scope?
  case expected_scope
  when TrueClass
    subject_class.respond_to?("with_#{expected_attr}")
  when :shallow
    enumerized_values.all? { |value| subject_class.respond_to?(value) }
  else
    subject_class.respond_to?(expected_scope[:scope])
  end
end
matches_values?() click to toggle source
# File lib/enumerize/integrations/rspec/matcher.rb, line 90
def matches_values?
  matches_array_values? || matches_hash_values?
end
quote_values(values) click to toggle source
# File lib/enumerize/integrations/rspec/matcher.rb, line 158
def quote_values(values)
  sorted_values.map(&:inspect).join(', ')
end
sorted_values() click to toggle source
# File lib/enumerize/integrations/rspec/matcher.rb, line 134
def sorted_values
  @sorted_values ||=expected_values.map(&:to_s).sort
end
subject_class() click to toggle source
# File lib/enumerize/integrations/rspec/matcher.rb, line 154
def subject_class
  @subject_class ||= subject.class
end