class Shoulda::Matchers::ActiveRecord::EncryptMatcher

@private

Attributes

failure_message[R]
failure_message_when_negated[R]

Public Class Methods

new(attribute) click to toggle source
# File lib/shoulda/matchers/active_record/encrypt_matcher.rb, line 79
def initialize(attribute)
  @attribute = attribute.to_sym
  @options = {}
end

Public Instance Methods

description() click to toggle source
# File lib/shoulda/matchers/active_record/encrypt_matcher.rb, line 125
def description
  "encrypt :#{@attribute}"
end
deterministic(deterministic) click to toggle source
# File lib/shoulda/matchers/active_record/encrypt_matcher.rb, line 86
def deterministic(deterministic)
  with_option(:deterministic, deterministic)
end
downcase(downcase) click to toggle source
# File lib/shoulda/matchers/active_record/encrypt_matcher.rb, line 90
def downcase(downcase)
  with_option(:downcase, downcase)
end
ignore_case(ignore_case) click to toggle source
# File lib/shoulda/matchers/active_record/encrypt_matcher.rb, line 94
def ignore_case(ignore_case)
  with_option(:ignore_case, ignore_case)
end
matches?(subject) click to toggle source
# File lib/shoulda/matchers/active_record/encrypt_matcher.rb, line 98
        def matches?(subject)
          @subject = subject
          result = encrypted_attributes_included? &&
                   options_correct?(
                     :deterministic,
                     :downcase,
                     :ignore_case,
                   )

          if result
            @failure_message_when_negated = "Did not expect to #{description} of #{class_name}"
            if @options.present?
              @failure_message_when_negated += "
using "
              @failure_message_when_negated += @options.map { |opt, expected|
                ":#{opt} option as ‹#{expected}›"
              }.join(' and
')
            end

            @failure_message_when_negated += ",
but it did"
          end

          result
        end

Private Instance Methods

class_name() click to toggle source
# File lib/shoulda/matchers/active_record/encrypt_matcher.rb, line 168
def class_name
  @subject.class.name
end
encrypted_attribute_scheme() click to toggle source
# File lib/shoulda/matchers/active_record/encrypt_matcher.rb, line 164
def encrypted_attribute_scheme
  @subject.class.type_for_attribute(@attribute).scheme
end
encrypted_attributes() click to toggle source
# File lib/shoulda/matchers/active_record/encrypt_matcher.rb, line 160
def encrypted_attributes
  @_encrypted_attributes ||= @subject.class.encrypted_attributes || []
end
encrypted_attributes_included?() click to toggle source
# File lib/shoulda/matchers/active_record/encrypt_matcher.rb, line 131
def encrypted_attributes_included?
  if encrypted_attributes.include?(@attribute)
    true
  else
    @failure_message = "Expected to #{description} of #{class_name}, but it did not"
    false
  end
end
options_correct?(*opts) click to toggle source
# File lib/shoulda/matchers/active_record/encrypt_matcher.rb, line 145
        def options_correct?(*opts)
          opts.all? do |opt|
            next true unless @options.key?(opt)

            expected = @options[opt]
            actual = encrypted_attribute_scheme.send("#{opt}?")
            next true if expected == actual

            @failure_message = "Expected to #{description} of #{class_name} using :#{opt} option
as ‹#{expected}›, but got ‹#{actual}›"

            false
          end
        end
with_option(option_name, value) click to toggle source
# File lib/shoulda/matchers/active_record/encrypt_matcher.rb, line 140
def with_option(option_name, value)
  @options[option_name] = value
  self
end