Class: ModelTimeline::RSpec::Matchers::HaveTimelineEntryMetadata Private
- Inherits:
-
Object
- Object
- ModelTimeline::RSpec::Matchers::HaveTimelineEntryMetadata
- Defined in:
- lib/model_timeline/rspec/matchers.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
RSpec matcher to check if a model has timeline entries with specific metadata
Instance Method Summary collapse
-
#failure_message ⇒ String
private
Message displayed when the expectation fails.
-
#failure_message_when_negated ⇒ String
private
Message displayed when the negated expectation fails.
-
#initialize(expected_metadata, association_name) ⇒ HaveTimelineEntryMetadata
constructor
private
Initialize the matcher.
-
#matches?(subject) ⇒ Boolean
private
Check if the subject matches the expectations.
Constructor Details
#initialize(expected_metadata, association_name) ⇒ HaveTimelineEntryMetadata
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initialize the matcher
259 260 261 262 |
# File 'lib/model_timeline/rspec/matchers.rb', line 259 def initialize(, association_name) @expected_metadata = @association_name = association_name end |
Instance Method Details
#failure_message ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Message displayed when the expectation fails
285 286 287 |
# File 'lib/model_timeline/rspec/matchers.rb', line 285 def "expected #{@subject} to have timeline entries with metadata #{@expected_metadata.inspect}, but none was found" end |
#failure_message_when_negated ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Message displayed when the negated expectation fails
292 293 294 |
# File 'lib/model_timeline/rspec/matchers.rb', line 292 def "expected #{@subject} not to have timeline entries with metadata #{@expected_metadata.inspect}, but such entries were found" end |
#matches?(subject) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Check if the subject matches the expectations
268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/model_timeline/rspec/matchers.rb', line 268 def matches?(subject) @subject = subject # Build a query that checks for each key-value pair in the metadata entries = subject.public_send(@association_name) # Construct queries for each metadata key-value pair @expected_metadata.all? do |key, value| # Use a JSON containment query to check if the metadata contains the key-value pair # This syntax works with PostgreSQL's JSONB containment operator @> entries.where('metadata @> ?', { key.to_s => value }.to_json).exists? end end |