Sha256: 5991c2d45d0ae52f20cc1537de40c3d652024cca9daf470a089b43b83ba2378b

Contents?: true

Size: 1.58 KB

Versions: 34

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

require "store_model/combine_errors_strategies/mark_invalid_error_strategy"
require "store_model/combine_errors_strategies/merge_error_strategy"
require "store_model/combine_errors_strategies/merge_array_error_strategy"

module StoreModel
  # Module with built-in strategies for combining errors.
  module CombineErrorsStrategies
    module_function

    # Finds a strategy based on +options+ and global config.
    #
    # @param options [Hash]
    #
    # @return [Object] strategy
    def configure(options)
      configured_strategy = options[:merge_errors] || StoreModel.config.merge_errors

      get_configured_strategy(
        configured_strategy,
        StoreModel::CombineErrorsStrategies::MergeErrorStrategy
      )
    end

    # Finds a array strategy based on +options+ and global config.
    #
    # @param options [Hash]
    #
    # @return [Object] strategy
    def configure_array(options)
      configured_strategy = options[:merge_array_errors] || StoreModel.config.merge_array_errors

      get_configured_strategy(
        configured_strategy,
        StoreModel::CombineErrorsStrategies::MergeArrayErrorStrategy
      )
    end

    def get_configured_strategy(configured_strategy, true_strategy_class)
      if configured_strategy.respond_to?(:call)
        configured_strategy
      elsif configured_strategy == true
        true_strategy_class.new
      elsif configured_strategy.nil?
        StoreModel::CombineErrorsStrategies::MarkInvalidErrorStrategy.new
      else
        const_get(configured_strategy.to_s.camelize).new
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
store_model-4.2.0 lib/store_model/combine_errors_strategies.rb
store_model-4.1.0 lib/store_model/combine_errors_strategies.rb
store_model-4.0.0 lib/store_model/combine_errors_strategies.rb
store_model-3.0.2 lib/store_model/combine_errors_strategies.rb
store_model-3.0.1 lib/store_model/combine_errors_strategies.rb
store_model-3.0.0 lib/store_model/combine_errors_strategies.rb
store_model-2.4.0 lib/store_model/combine_errors_strategies.rb
store_model-2.3.0 lib/store_model/combine_errors_strategies.rb
store_model-2.2.0 lib/store_model/combine_errors_strategies.rb
store_model-2.1.2 lib/store_model/combine_errors_strategies.rb
store_model-2.1.1 lib/store_model/combine_errors_strategies.rb
store_model-2.1.0 lib/store_model/combine_errors_strategies.rb
store_model-2.0.1 lib/store_model/combine_errors_strategies.rb
store_model-2.0.0 lib/store_model/combine_errors_strategies.rb
store_model-1.6.2 lib/store_model/combine_errors_strategies.rb
store_model-1.6.1 lib/store_model/combine_errors_strategies.rb
store_model-1.6.0 lib/store_model/combine_errors_strategies.rb
store_model-1.5.1 lib/store_model/combine_errors_strategies.rb
store_model-1.5.0 lib/store_model/combine_errors_strategies.rb
store_model-1.4.0 lib/store_model/combine_errors_strategies.rb