Sha256: 3f9d00d6875d36c896a23f96d28ccb6ba69e7e6bf84d8fe3c2250ef1a353fe81

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

module RailsSettings
  class Configuration
    def initialize(*args, &block)
      options = args.extract_options!
      klass = args.shift
      keys = args

      raise ArgumentError unless klass

      @klass = klass

      if options[:persistent]
        @klass.class_attribute :default_settings unless @klass.methods.include?(:default_settings)
      else
        @klass.class_attribute :default_settings
      end

      @klass.class_attribute :setting_object_class_name
      @klass.default_settings ||= {}
      @klass.setting_object_class_name = options[:class_name] || 'RailsSettings::SettingObject'

      if block_given?
        yield(self)
      else
        keys.each do |k|
          key(k)
        end
      end

      raise ArgumentError.new('has_settings: No keys defined') if @klass.default_settings.blank?
    end

    def key(name, options={})
      raise ArgumentError.new("has_settings: Symbol expected, but got a #{name.class}") unless name.is_a?(Symbol)
      raise ArgumentError.new("has_settings: Option :defaults expected, but got #{options.keys.join(', ')}") unless options.blank? || (options.keys == [:defaults])
      @klass.default_settings[name] = (options[:defaults] || {}).stringify_keys.freeze
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ledermann-rails-settings-2.5.0 lib/rails-settings/configuration.rb