Sha256: 0f93863a2826e6bd9da5eeb95157005e76e938445af722e6da3d733eb6bec207

Contents?: true

Size: 1.78 KB

Versions: 6

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

class Serega
  module SeregaPlugins
    module Metadata
      class MetaAttribute
        #
        # Validator for meta_attribute options
        #
        class CheckOpts
          class << self
            #
            # Validates meta_attribute options
            # Checks used options are allowed and then checks options values.
            #
            # @param opts [Hash] Attribute options
            # @param block [Proc] Attribute block
            # @param allowed_keys [Array<Symbol>] Allowed options keys
            #
            # @raise [SeregaError] when attribute has invalid options
            #
            # @return [void]
            #
            def call(opts, block, allowed_keys)
              check_allowed_options_keys(opts, allowed_keys)
              check_each_opt(opts, block)
              check_any_value_provided(opts, block)
            end

            private

            def check_allowed_options_keys(opts, allowed_keys)
              opts.each_key do |key|
                next if allowed_keys.include?(key.to_sym)

                allowed = allowed_keys.map(&:inspect).join(", ")
                raise SeregaError, "Invalid option #{key.inspect}. Allowed options are: #{allowed}"
              end
            end

            def check_each_opt(opts, block)
              CheckOptHideEmpty.call(opts)
              CheckOptHideNil.call(opts)
              CheckOptValue.call(opts, block)
              CheckOptConst.call(opts, block)
            end

            def check_any_value_provided(opts, block)
              return if opts.key?(:const) || opts.key?(:value) || block

              raise SeregaError, "Please provide block argument or add :value or :const option"
            end
          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/metadata/validations/check_opts.rb
serega-0.20.1 lib/serega/plugins/metadata/validations/check_opts.rb
serega-0.20.0 lib/serega/plugins/metadata/validations/check_opts.rb
serega-0.19.0 lib/serega/plugins/metadata/validations/check_opts.rb
serega-0.18.0 lib/serega/plugins/metadata/validations/check_opts.rb
serega-0.17.0 lib/serega/plugins/metadata/validations/check_opts.rb