Class: ModelTimeline::RSpec::Matchers::HaveTimelineEntryMetadata Private

Inherits:
Object
  • Object
show all
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

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

Parameters:

  • expected_metadata (Hash)

    The metadata key-value pairs to check for

  • association_name (Symbol)

    The name of the timeline association



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_messageString

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

Returns:

  • (String)

    A descriptive failure message



285
286
287
# File 'lib/model_timeline/rspec/matchers.rb', line 285

def failure_message
  "expected #{@subject} to have timeline entries with metadata #{@expected_metadata.inspect}, but none was found"
end

#failure_message_when_negatedString

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

Returns:

  • (String)

    A descriptive failure message for negated expectations



292
293
294
# File 'lib/model_timeline/rspec/matchers.rb', line 292

def failure_message_when_negated
  "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

Parameters:

  • subject (Object)

    The model to check for timeline entries

Returns:

  • (Boolean)

    True if the model has timeline entries with the specified metadata



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