Sha256: db5aadc414bf273db1b7a6ccf3b051ba7366c0d6079b559f85b764580cd20407

Contents?: true

Size: 1.65 KB

Versions: 28

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

module PagesCore
  module Templates
    class ConfigurationHandler
      class << self
        def handle_blocks
          @handle_blocks ||= {}
        end

        def handle(method_name, &handle_block)
          @handle_blocks ||= {}
          @handle_blocks[method_name] = handle_block
        end
      end

      attr_reader :configuration

      def initialize
        @configuration = {}
      end

      def method_missing(method_name, *args, &block)
        if self.class.handle_blocks.key?(method_name)
          proxy = PagesCore::Templates::ConfigurationProxy.new(
            self.class.handle_blocks[method_name],
            self
          )
          yield(proxy) if block
          proxy
        else
          super
        end
      end

      def proxy(proxy_block = nil, &callback)
        proxy_object = PagesCore::Templates::ConfigurationProxy.new(callback)
        proxy_block&.call(proxy_object)
        proxy_object
      end

      def respond_to_missing?(method_name)
        self.class.handle_blocks.key?(method_name)
      end

      def set(stack, value)
        @configuration ||= {}
        value = true  if value == :enabled
        value = false if value == :disabled
        stack = [stack] unless stack.is_a?(Enumerable)
        partial_hash = stack.reverse.inject(value) do |hash, key|
          { key => hash }
        end
        @configuration = @configuration.deep_merge(partial_hash)
        value
      end

      def get(*path)
        @configuration ||= {}
        path.inject(@configuration) do |value, key|
          value.is_a?(Hash) && value&.key?(key) ? value[key] : nil
        end
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
pages_core-3.15.5 lib/pages_core/templates/configuration_handler.rb
pages_core-3.15.4 lib/pages_core/templates/configuration_handler.rb
pages_core-3.15.3 lib/pages_core/templates/configuration_handler.rb
pages_core-3.15.2 lib/pages_core/templates/configuration_handler.rb
pages_core-3.15.1 lib/pages_core/templates/configuration_handler.rb
pages_core-3.14.0 lib/pages_core/templates/configuration_handler.rb
pages_core-3.13.0 lib/pages_core/templates/configuration_handler.rb
pages_core-3.12.7 lib/pages_core/templates/configuration_handler.rb
pages_core-3.12.6 lib/pages_core/templates/configuration_handler.rb
pages_core-3.12.5 lib/pages_core/templates/configuration_handler.rb
pages_core-3.12.4 lib/pages_core/templates/configuration_handler.rb
pages_core-3.12.3 lib/pages_core/templates/configuration_handler.rb
pages_core-3.12.2 lib/pages_core/templates/configuration_handler.rb
pages_core-3.12.1 lib/pages_core/templates/configuration_handler.rb
pages_core-3.12.0 lib/pages_core/templates/configuration_handler.rb
pages_core-3.11.3 lib/pages_core/templates/configuration_handler.rb
pages_core-3.11.2 lib/pages_core/templates/configuration_handler.rb
pages_core-3.11.1 lib/pages_core/templates/configuration_handler.rb
pages_core-3.11.0 lib/pages_core/templates/configuration_handler.rb
pages_core-3.10.2 lib/pages_core/templates/configuration_handler.rb