Sha256: 1ef0cbcc40012d9120cf1fd2904e83a3c9046af758ab9904e3718ad2ed72ad80

Contents?: true

Size: 1.08 KB

Versions: 17

Compression:

Stored size: 1.08 KB

Contents

module JobNotifier
  module Identifier
    extend ActiveSupport::Concern

    included do
      def job_identifier
        identifier = if self.class.job_identifier_proc
                       self.class.job_identifier_proc.call(self).to_s
                     else
                       identifier_from_attrs
                     end

        Digest::MD5.hexdigest(identifier)
      end
    end

    def identifier_from_attrs
      keys = self.class.identifier_attrs
      raise JobNotifier::Error::MissingAttributes.new if keys.blank?

      keys.sort.map do |attribute|
        value = send(attribute)
        raise JobNotifier::Error::BlankAttribute.new(attribute) if value.blank?
        [attribute, value.to_s]
      end.flatten.join("::")
    end

    module ClassMethods
      def identify_job_through(*attrs, &block)
        if block
          @job_identifier_proc = block
        else
          @identifier_attrs = attrs
        end
      end

      def identifier_attrs
        @identifier_attrs
      end

      def job_identifier_proc
        @job_identifier_proc
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
job_notifier-3.0.0 lib/job_notifier/identifier.rb
job_notifier-2.0.0 lib/job_notifier/identifier.rb
job_notifier-1.5.0 lib/job_notifier/identifier.rb
job_notifier-1.4.0 lib/job_notifier/identifier.rb
job_notifier-1.3.0 lib/job_notifier/identifier.rb
job_notifier-1.2.4 lib/job_notifier/identifier.rb
job_notifier-1.2.3 lib/job_notifier/identifier.rb
job_notifier-1.2.2 lib/job_notifier/identifier.rb
job_notifier-1.2.1 lib/job_notifier/identifier.rb
job_notifier-1.2.0 lib/job_notifier/identifier.rb
job_notifier-1.1.1 lib/job_notifier/identifier.rb
job_notifier-1.1.0 lib/job_notifier/identifier.rb
job_notifier-1.0.0 lib/job_notifier/identifier.rb
job_notifier-0.2.1 lib/job_notifier/identifier.rb
job_notifier-0.2.0 lib/job_notifier/identifier.rb
job_notifier-0.1.1 lib/job_notifier/identifier.rb
job_notifier-0.1.0 lib/job_notifier/identifier.rb