Sha256: 4491589e2368530e051d306d30b92d02419b74e57c85a26320a977f97f958f3e

Contents?: true

Size: 1.88 KB

Versions: 19

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

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

      SettingStruct = Struct.new(:type, :default)

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

      def self.setting(key, type, default = nil)
        settings[key] = SettingStruct.new(type, 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

19 entries across 19 versions & 1 rubygems

Version Path
pages_core-3.14.0 lib/pages_core/configuration/base.rb
pages_core-3.13.0 lib/pages_core/configuration/base.rb
pages_core-3.12.7 lib/pages_core/configuration/base.rb
pages_core-3.12.6 lib/pages_core/configuration/base.rb
pages_core-3.12.5 lib/pages_core/configuration/base.rb
pages_core-3.12.4 lib/pages_core/configuration/base.rb
pages_core-3.12.3 lib/pages_core/configuration/base.rb
pages_core-3.12.2 lib/pages_core/configuration/base.rb
pages_core-3.12.1 lib/pages_core/configuration/base.rb
pages_core-3.12.0 lib/pages_core/configuration/base.rb
pages_core-3.11.3 lib/pages_core/configuration/base.rb
pages_core-3.11.2 lib/pages_core/configuration/base.rb
pages_core-3.11.1 lib/pages_core/configuration/base.rb
pages_core-3.11.0 lib/pages_core/configuration/base.rb
pages_core-3.10.2 lib/pages_core/configuration/base.rb
pages_core-3.10.1 lib/pages_core/configuration/base.rb
pages_core-3.9.2 lib/pages_core/configuration/base.rb
pages_core-3.9.1 lib/pages_core/configuration/base.rb
pages_core-3.9.0 lib/pages_core/configuration/base.rb