Sha256: 8eadcd3121bb7c72a9536cbbe3172e3dcfaf9ee072b0d41d382eabb6ef2fe514

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

module Trailblazer
  class Finder
    module Activity
      # Adapters Activity
      class PrepareAdapters < Trailblazer::Activity::Railway
        def check_for_adapters(ctx, **)
          adapters = ctx.dig(:config, :adapters)
          return true if adapters.empty?

          adapters.each do |adapter|
            return true if Finder::Adapters.constants.include?(adapter.to_sym)
          end
          false
        end

        ## Only one ORM can be accepted
        def validate_adapters(ctx, **)
          adapters = ctx.dig(:config, :adapters)
          return true if adapters.empty?

          adapters.each do |adapter|
            if ORM_ADAPTERS.include?(adapter)
              return false if (adapters & (ORM_ADAPTERS - [adapter])).any?
            end
          end
          true
        end

        def invalid_adapters_error((ctx, flow_options), **_circuit_options)
          (ctx[:errors] ||= []) << {adapters: "One or more of the specified adapters are invalid"}
        end

        def multiple_orm_error((ctx, flow_options), **_circuit_options)
          (ctx[:errors] ||= []) << {adapters: "More then one ORM adapter specified"}
        end

        def set_adapters(ctx, **)
          adapters = ctx.dig(:config, :adapters)
          ctx[:adapters] = (ORM_ADAPTERS & adapters).any? ? adapters : ["Basic"] + adapters
        end

        step :check_for_adapters
        fail :invalid_adapters_error, Output(:success) => End(:failure)
        step :validate_adapters
        fail :multiple_orm_error
        step :set_adapters
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
trailblazer-finder-0.10.0 lib/trailblazer/finder/activity/prepare_adapters.rb