Sha256: e738685a5f0a2992df5eb065c886c166bcf430582b4f58a99c97c2ab3edf742f

Contents?: true

Size: 1.45 KB

Versions: 31

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true
module Blacklight
  class Configuration
    class Context
      attr_reader :context

      def initialize(context)
        @context = context
      end

      ##
      # Evaluate conditionals for a configuration with if/unless attributes
      #
      # @param [#if,#unless] config an object that responds to if/unless
      # @return [Boolean]
      def evaluate_if_unless_configuration(config, *args)
        return config if config == true or config == false

        if_value = !config.respond_to?(:if) ||
                        config.if.nil? ||
                        evaluate_configuration_conditional(config.if, config, *args)

        unless_value = !config.respond_to?(:unless) ||
                          config.unless.nil? ||
                          !evaluate_configuration_conditional(config.unless, config, *args)

        if_value && unless_value
      end

      def evaluate_configuration_conditional(proc_helper_or_boolean, *args_for_procs_and_methods)
        case proc_helper_or_boolean
        when Symbol
          arity = context.method(proc_helper_or_boolean).arity

          if arity.zero?
            context.send(proc_helper_or_boolean)
          else
            context.send(proc_helper_or_boolean, *args_for_procs_and_methods)
          end
        when Proc
          proc_helper_or_boolean.call context, *args_for_procs_and_methods
        else
          proc_helper_or_boolean
        end
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
blacklight-6.25.0 lib/blacklight/configuration/context.rb
blacklight-6.24.0 lib/blacklight/configuration/context.rb
blacklight-6.23.0 lib/blacklight/configuration/context.rb
blacklight-6.22.0 lib/blacklight/configuration/context.rb
blacklight-6.21.0 lib/blacklight/configuration/context.rb
blacklight-6.20.0 lib/blacklight/configuration/context.rb
blacklight-6.19.2 lib/blacklight/configuration/context.rb
blacklight-6.19.1 lib/blacklight/configuration/context.rb
blacklight-6.19.0 lib/blacklight/configuration/context.rb
blacklight-6.18.0 lib/blacklight/configuration/context.rb
blacklight-6.17.0 lib/blacklight/configuration/context.rb
blacklight-6.16.0 lib/blacklight/configuration/context.rb
blacklight-6.15.0 lib/blacklight/configuration/context.rb
blacklight-6.14.1 lib/blacklight/configuration/context.rb
blacklight-6.14.0 lib/blacklight/configuration/context.rb
blacklight-6.13.0 lib/blacklight/configuration/context.rb
blacklight-6.12.0 lib/blacklight/configuration/context.rb
blacklight-6.11.2 lib/blacklight/configuration/context.rb
blacklight-6.11.1 lib/blacklight/configuration/context.rb
blacklight-6.11.0 lib/blacklight/configuration/context.rb