class Shoulda::Matchers::ActiveRecord::HaveSecureTokenMatcher

@private

def description

def description
  "have :#{token_attribute} as a secure token"
end

def failure_message

def failure_message
  return if !@errors
  "Expected #{@subject.class} to #{description} but the following " \
  "errors were found: #{@errors.join(', ')}"
end

def failure_message_when_negated

def failure_message_when_negated
  return if !@errors
  "Did not expect #{@subject.class} to have secure token " \
  ":#{token_attribute}"
end

def has_expected_db_column?

def has_expected_db_column?
  matcher = HaveDbColumnMatcher.new(token_attribute).of_type(:string)
  matcher.matches?(@subject)
end

def has_expected_db_index?

def has_expected_db_index?
  matcher = HaveDbIndexMatcher.new(token_attribute).unique(true)
  matcher.matches?(@subject)
end

def has_expected_instance_methods?

def has_expected_instance_methods?
  @subject.respond_to?(token_attribute.to_s) &&
    @subject.respond_to?("#{token_attribute}=") &&
    @subject.respond_to?("regenerate_#{token_attribute}") &&
    @subject.class.respond_to?(:generate_unique_secure_token)
end

def ignoring_check_for_db_index

def ignoring_check_for_db_index
  @options[:ignore_check_for_db_index] = true
  self
end

def initialize(token_attribute)

def initialize(token_attribute)
  @token_attribute = token_attribute
  @options = { ignore_check_for_db_index: false }
end

def matches?(subject)

def matches?(subject)
  @subject = subject
  @errors = run_checks
  @errors.empty?
end

def run_checks

def run_checks
  @errors = []
  if !has_expected_instance_methods?
    @errors << 'missing expected class and instance methods'
  end
  if !has_expected_db_column?
    @errors << "missing correct column #{token_attribute}:string"
  end
  if !@options[:ignore_check_for_db_index] && !has_expected_db_index?
    @errors << "missing unique index for #{table_and_column}"
  end
  @errors
end

def table_and_column

def table_and_column
  "#{table_name}.#{token_attribute}"
end

def table_name

def table_name
  @subject.class.table_name
end