Class: Shoulda::Callback::Matchers::ActiveModel::CallbackMatcher
- Inherits:
-
Object
- Object
- Shoulda::Callback::Matchers::ActiveModel::CallbackMatcher
- Includes:
- RailsVersionHelper
- Defined in:
- lib/shoulda/callback/matchers/active_model.rb
Overview
:nodoc:
Constant Summary collapse
- VALID_OPTIONAL_LIFECYCLES =
[:validation, :commit, :rollback].freeze
Instance Method Summary collapse
- #callback_method_exists?(object, callback) ⇒ Boolean
- #description ⇒ Object
- #failure_message ⇒ Object
- #failure_message_when_negated ⇒ Object
-
#initialize(method) ⇒ CallbackMatcher
constructor
A new instance of CallbackMatcher.
- #matches?(subject) ⇒ Boolean
- #negative_failure_message ⇒ Object
- #on(optional_lifecycle) ⇒ Object
Methods included from RailsVersionHelper
Constructor Details
#initialize(method) ⇒ CallbackMatcher
Returns a new instance of CallbackMatcher.
31 32 33 |
# File 'lib/shoulda/callback/matchers/active_model.rb', line 31 def initialize method @method = method end |
Instance Method Details
#callback_method_exists?(object, callback) ⇒ Boolean
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/shoulda/callback/matchers/active_model.rb', line 76 def callback_method_exists? object, callback if is_class_callback?(object, callback) && !callback_object(object, callback).respond_to?(:"#{@hook}_#{@lifecycle}", true) = "callback #{@method} is listed as a callback #{@hook} #{@lifecycle}#{optional_lifecycle_phrase}#{condition_phrase}, but the given object does not respond to #{@hook}_#{@lifecycle} (using respond_to?(:#{@hook}_#{@lifecycle}, true)" false elsif !is_class_callback?(object, callback) && !object.respond_to?(callback.filter, true) = "callback #{@method} is listed as a callback #{@hook} #{@lifecycle}#{optional_lifecycle_phrase}#{condition_phrase}, but the model does not respond to #{@method} (using respond_to?(:#{@method}, true)" false else true end end |
#description ⇒ Object
100 101 102 |
# File 'lib/shoulda/callback/matchers/active_model.rb', line 100 def description "callback #{@method} #{@hook} #{@lifecycle}#{optional_lifecycle_phrase}#{condition_phrase}" end |
#failure_message ⇒ Object
88 89 90 |
# File 'lib/shoulda/callback/matchers/active_model.rb', line 88 def || "expected #{@method} to be listed as a callback #{@hook} #{@lifecycle}#{optional_lifecycle_phrase}#{condition_phrase}, but was not" end |
#failure_message_when_negated ⇒ Object
92 93 94 |
# File 'lib/shoulda/callback/matchers/active_model.rb', line 92 def || "expected #{@method} not to be listed as a callback #{@hook} #{@lifecycle}#{optional_lifecycle_phrase}#{condition_phrase}, but was" end |
#matches?(subject) ⇒ Boolean
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/shoulda/callback/matchers/active_model.rb', line 63 def matches? subject check_preconditions! callbacks = subject.send :"_#{@lifecycle}_callbacks" callbacks.any? do |callback| has_callback?(subject, callback) && matches_hook?(callback) && matches_conditions?(callback) && matches_optional_lifecycle?(callback) && callback_method_exists?(subject, callback) end end |
#negative_failure_message ⇒ Object
96 97 98 |
# File 'lib/shoulda/callback/matchers/active_model.rb', line 96 def end |
#on(optional_lifecycle) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/shoulda/callback/matchers/active_model.rb', line 55 def on optional_lifecycle check_for_valid_optional_lifecycles! @optional_lifecycle = optional_lifecycle self end |