Sha256: 37d9a313f03f73374e46320d458930740481b65571811dd8cd9362234d842b20

Contents?: true

Size: 1.08 KB

Versions: 7

Compression:

Stored size: 1.08 KB

Contents

module Pageflow
  module Panorama
    class JobStatusAttributes < Struct.new(:record, :stage_name)
      def self.handle(record, options = {}, &block)
        new(record, options[:stage]).call(&block)
      end

      def self.reset(record, options = {})
        new(record, options[:stage]).reset
      end

      def call(&block)
        update_progress(0)

        with_error_message_handling do
          block.call do |percent|
            update_progress(percent)
          end
        end
      end

      def reset
        update_progress(0)
      end

      private

      def update_progress(percent)
        record.update(stage_attribute_name(:progress) => percent,
                      stage_attribute_name(:error_message) => nil)
      end

      def with_error_message_handling
        yield
      rescue StandardError => e
        if e.respond_to?(:message_i18n_key)
          record[stage_attribute_name(:error_message)] = e.message_i18n_key
        end

        raise
      end

      def stage_attribute_name(suffix)
        [stage_name, suffix].compact.join('_')
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
pageflow-panorama-2.4.0 lib/pageflow/panorama/job_status_attributes.rb
pageflow-panorama-2.3.0 lib/pageflow/panorama/job_status_attributes.rb
pageflow-panorama-2.2.0 lib/pageflow/panorama/job_status_attributes.rb
pageflow-panorama-2.1.0 lib/pageflow/panorama/job_status_attributes.rb
pageflow-panorama-2.0.0 lib/pageflow/panorama/job_status_attributes.rb
pageflow-panorama-1.0.1 lib/pageflow/panorama/job_status_attributes.rb
pageflow-panorama-1.0.0 lib/pageflow/panorama/job_status_attributes.rb