Sha256: 5d4a47e8258341c6d2ae096343b599435c74ed3120360328b4247f4d3f003146

Contents?: true

Size: 1.45 KB

Versions: 58

Compression:

Stored size: 1.45 KB

Contents

module Hyrax
  module Workflow
    # Responsible for writing the database records for the given :action and :method list.
    class MethodGenerator
      # @api public
      #
      # @param action [Sipity::WorkflowAction]
      # @param list [Array<String>]
      def self.call(action:, list:)
        new(action: action, list: list).call
      end

      # @param action [Sipity::WorkflowAction]
      # @param list [Array<String>]
      def initialize(action:, list:)
        @action = action
        @list = list
      end

      attr_reader :action, :list

      def call
        if list.size < action.triggered_methods.count
          replace_list
        else
          update_list
        end
      end

      private

        def update_list
          update_from = list.dup
          nodes_to_update = action.triggered_methods.order(:weight)
          nodes_to_update.each do |node|
            node.update!(service_name: update_from.shift)
          end

          count = nodes_to_update.count
          # If there are more new values than old values, add them.
          until update_from.empty?
            action.triggered_methods.create!(service_name: update_from.shift, weight: count)
            count += 1
          end
        end

        def replace_list
          action.triggered_methods.destroy_all
          list.each_with_index do |name, i|
            action.triggered_methods.create!(service_name: name, weight: i)
          end
        end
    end
  end
end

Version data entries

58 entries across 58 versions & 2 rubygems

Version Path
hyrax-2.9.6 app/services/hyrax/workflow/method_generator.rb
hyrax-2.9.5 app/services/hyrax/workflow/method_generator.rb
hyrax-2.9.4 app/services/hyrax/workflow/method_generator.rb
hyrax-2.9.3 app/services/hyrax/workflow/method_generator.rb
hyrax-2.9.2 app/services/hyrax/workflow/method_generator.rb
hyrax-2.9.1 app/services/hyrax/workflow/method_generator.rb
hyrax-2.9.0 app/services/hyrax/workflow/method_generator.rb
hyrax-2.8.0 app/services/hyrax/workflow/method_generator.rb
hyrax-2.7.2 app/services/hyrax/workflow/method_generator.rb
hyrax-2.7.1 app/services/hyrax/workflow/method_generator.rb
hyrax-2.7.0 app/services/hyrax/workflow/method_generator.rb
hyrax-2.6.0 app/services/hyrax/workflow/method_generator.rb
hyrax-3.0.0.pre.rc1 app/services/hyrax/workflow/method_generator.rb
hyrax-3.0.0.pre.beta3 app/services/hyrax/workflow/method_generator.rb
hyrax-2.5.1 app/services/hyrax/workflow/method_generator.rb
hyrax-2.5.0 app/services/hyrax/workflow/method_generator.rb
hyrax-3.0.0.pre.beta2 app/services/hyrax/workflow/method_generator.rb
hyrax-2.4.1 app/services/hyrax/workflow/method_generator.rb
hyrax-3.0.0.pre.beta1 app/services/hyrax/workflow/method_generator.rb
hyrax-2.4.0 app/services/hyrax/workflow/method_generator.rb