Class: Shoulda::Matchers::ActiveRecord::HaveReadonlyAttributeMatcher

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute) ⇒ HaveReadonlyAttributeMatcher

Returns a new instance of HaveReadonlyAttributeMatcher.



29
30
31
# File 'lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb', line 29

def initialize(attribute)
  @attribute = attribute.to_s
end

Instance Attribute Details

#failure_messageObject (readonly)

Returns the value of attribute failure_message.



33
34
35
# File 'lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb', line 33

def failure_message
  @failure_message
end

#failure_message_when_negatedObject (readonly)

Returns the value of attribute failure_message_when_negated.



33
34
35
# File 'lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb', line 33

def failure_message_when_negated
  @failure_message_when_negated
end

Instance Method Details

#descriptionObject



55
56
57
# File 'lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb', line 55

def description
  "make #{@attribute} read-only"
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb', line 35

def matches?(subject)
  @subject = subject
  if readonly_attributes.include?(@attribute)
    @failure_message_when_negated = "Did not expect #{@attribute}"\
    ' to be read-only'
    true
  else
    @failure_message =
      if readonly_attributes.empty?
        "#{class_name} attribute #{@attribute} " <<
          'is not read-only'
      else
        "#{class_name} is making " <<
          "#{readonly_attributes.to_a.to_sentence} " <<
          "read-only, but not #{@attribute}."
      end
    false
  end
end