Sha256: ef6b64816e2e0981266245290d29b5afd10a39ef13dac84b00e218807a01d85b

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

# encoding: utf-8

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.keys.include?(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) if proxy_block
        proxy_object
      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|
          Hash[key => hash]
        end
        @configuration = @configuration.deep_merge(partial_hash)
        value
      end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pages_core-3.5.1 lib/pages_core/templates/configuration_handler.rb