Sha256: 65b48fcb0460fd83b893b9782a65c3dca52e55936c90b7fc58c9568413576d00

Contents?: true

Size: 1.23 KB

Versions: 6

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

class Serega
  module SeregaPlugins
    module If
      #
      # Validator for attribute :if option
      #
      class CheckOptIf
        class << self
          #
          # Checks attribute :if option that must be [nil, Symbol, Proc, #call]
          #
          # @param opts [Hash] Attribute options
          #
          # @raise [SeregaError] Attribute validation error
          #
          # @return [void]
          #
          def call(opts)
            return unless opts.key?(:if)

            check_type(opts[:if])
          end

          private

          def check_type(value)
            return if value.is_a?(Symbol)
            raise SeregaError, must_be_callable unless value.respond_to?(:call)

            SeregaValidations::Utils::CheckExtraKeywordArg.call(value, ":if option")
            params_count = SeregaUtils::ParamsCount.call(value, max_count: 2)

            if params_count > 2
              raise SeregaError, "Option :if value should have up to 2 parameters (object, context)"
            end
          end

          def must_be_callable
            "Invalid attribute option :if. It must be a Symbol, a Proc or respond to :call"
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
serega-0.21.0 lib/serega/plugins/if/validations/check_opt_if.rb
serega-0.20.1 lib/serega/plugins/if/validations/check_opt_if.rb
serega-0.20.0 lib/serega/plugins/if/validations/check_opt_if.rb
serega-0.19.0 lib/serega/plugins/if/validations/check_opt_if.rb
serega-0.18.0 lib/serega/plugins/if/validations/check_opt_if.rb
serega-0.17.0 lib/serega/plugins/if/validations/check_opt_if.rb