Sha256: 06233c694744c1c02c8c82abd340f753a379e71c58ad00d39ce36cc620550a4e

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

# encoding: utf-8

module PagesCore
  module Configuration
    class Base
      class InvalidConfigurationKey < StandardError; end

      def self.settings
        @settings ||= {}
      end

      def self.setting(key, type, default = nil)
        settings[key] = OpenStruct.new(type: type, default: default)

        define_method key do |*args|
          args.any? ? set(key, *args) : get(key)
        end

        define_method "#{key}=" do |value|
          set(key, value)
        end

        define_method "#{key}?" do
          get(key) ? true : false
        end
      end

      def get(key)
        raise InvalidConfigurationKey unless setting?(key)
        if configuration.key?(key)
          configuration[key]
        else
          self.class.settings[key].default
        end
      end

      def set(key, value)
        raise InvalidConfigurationKey unless setting?(key)
        value = parse_value(key, value)
        unless valid_type?(key, value)
          raise(
            ArgumentError,
            "expected #{self.class.settings[key].type}, got #{value.class}"
          )
        end
        configuration[key] = value
      end

      protected

      def configuration
        @configuration ||= {}
      end

      def setting?(key)
        self.class.settings.key?(key)
      end

      def type_for(key)
        self.class.settings[key].type
      end

      def valid_type?(key, value)
        return true if value.nil?
        if type_for(key) == :boolean
          value.is_a?(TrueClass) || value.is_a?(FalseClass)
        else
          value.is_a?(type_for(key).to_s.camelize.constantize)
        end
      end

      def parse_value(key, value)
        if type_for(key) == :boolean
          value = true  if value == :enabled
          value = false if value == :disabled
        end
        value
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pages_core-3.5.1 lib/pages_core/configuration/base.rb