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.



49
50
51
# File 'lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb', line 49

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

Instance Attribute Details

#failure_messageObject (readonly)

Returns the value of attribute failure_message.



53
54
55
# File 'lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb', line 53

def failure_message
  @failure_message
end

#failure_message_when_negatedObject (readonly)

Returns the value of attribute failure_message_when_negated.



53
54
55
# File 'lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb', line 53

def failure_message_when_negated
  @failure_message_when_negated
end

Instance Method Details

#descriptionObject



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

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

#failure_reasonObject



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

def failure_reason
  @failure_message
end

#matches?(subject) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb', line 59

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