module EmailSpectacular::Matchers

Module containing helper methods for matching expectations against emails

@author Aleck Greenham

Constants

MATCHERS

Public Class Methods

included(base) click to toggle source
# File lib/email_spectacular/concerns/matchers.rb, line 45
def self.included(base) # rubocop:disable Metrics/MethodLength
  base.class_eval do
    def matching_emails(emails, scopes)
      emails.each_with_object(sent: [], enqueued: []) do |email, memo|
        matches_scopes = scopes.all? do |attribute, expected|
          email_matches?(email, MATCHERS[attribute], expected)
        end

        if matches_scopes
          if email.instance_variable_get(:@enqueued)
            memo[:enqueued] << email
          else
            memo[:sent] << email
          end
        end
      end
    end

    def email_matches?(email, assertion, expected)
      case assertion
      when :to
        !(expected & email.send(assertion)).empty?
      when String, Symbol
        email.send(assertion).include?(expected)
      when Hash
        assertion[:match].call(email, parsed_email_parts(email), expected)
      else
        raise "Unsupported assertion mapping '#{assertion}' of type #{assertion.class.name}"
      end
    end
  end
end

Public Instance Methods

email_matches?(email, assertion, expected) click to toggle source
# File lib/email_spectacular/concerns/matchers.rb, line 63
def email_matches?(email, assertion, expected)
  case assertion
  when :to
    !(expected & email.send(assertion)).empty?
  when String, Symbol
    email.send(assertion).include?(expected)
  when Hash
    assertion[:match].call(email, parsed_email_parts(email), expected)
  else
    raise "Unsupported assertion mapping '#{assertion}' of type #{assertion.class.name}"
  end
end
matching_emails(emails, scopes) click to toggle source
# File lib/email_spectacular/concerns/matchers.rb, line 47
def matching_emails(emails, scopes)
  emails.each_with_object(sent: [], enqueued: []) do |email, memo|
    matches_scopes = scopes.all? do |attribute, expected|
      email_matches?(email, MATCHERS[attribute], expected)
    end

    if matches_scopes
      if email.instance_variable_get(:@enqueued)
        memo[:enqueued] << email
      else
        memo[:sent] << email
      end
    end
  end
end