Class: Shoulda::Matchers::ActiveRecord::EncryptMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/shoulda/matchers/active_record/encrypt_matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute) ⇒ EncryptMatcher

Returns a new instance of EncryptMatcher.



79
80
81
82
# File 'lib/shoulda/matchers/active_record/encrypt_matcher.rb', line 79

def initialize(attribute)
  @attribute = attribute.to_sym
  @options = {}
end

Instance Attribute Details

#failure_messageObject (readonly)

Returns the value of attribute failure_message.



84
85
86
# File 'lib/shoulda/matchers/active_record/encrypt_matcher.rb', line 84

def failure_message
  @failure_message
end

#failure_message_when_negatedObject (readonly)

Returns the value of attribute failure_message_when_negated.



84
85
86
# File 'lib/shoulda/matchers/active_record/encrypt_matcher.rb', line 84

def failure_message_when_negated
  @failure_message_when_negated
end

Instance Method Details

#descriptionObject



125
126
127
# File 'lib/shoulda/matchers/active_record/encrypt_matcher.rb', line 125

def description
  "encrypt :#{@attribute}"
end

#deterministic(deterministic) ⇒ Object



86
87
88
# File 'lib/shoulda/matchers/active_record/encrypt_matcher.rb', line 86

def deterministic(deterministic)
  with_option(:deterministic, deterministic)
end

#downcase(downcase) ⇒ Object



90
91
92
# File 'lib/shoulda/matchers/active_record/encrypt_matcher.rb', line 90

def downcase(downcase)
  with_option(:downcase, downcase)
end

#ignore_case(ignore_case) ⇒ Object



94
95
96
# 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) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# 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