Sha256: 3d28bc04020fd645f9914f58daf72fc3977980fd2eb7f8aff9381874f92368a5

Contents?: true

Size: 1.89 KB

Versions: 14

Compression:

Stored size: 1.89 KB

Contents

module EasySettings
  class Mapper

    def initialize
      @all_keys = {}
    end

    # Be careful for double definition of the same key
    # Last definition wins
    def key(name, **options, &block)
      name = name.to_s

      if block
        key_dsl = EasySettings::KeyDSL.new
        key_dsl.instance_eval(&block)
        options = options.merge(key_dsl.options)
      end

      EasySetting.mapper_clear_caches
      @all_keys[name] = EasySettings::Key.init(name, **options)
    end
    alias_method :add_key, :key

    def keys(*names, **options, &block)
      names.each do |name|
        key(name, **options, &block)
      end
    end
    alias_method :add_keys, :keys

    # Shortcust due to frequent usage
    def boolean_keys(*names)
      names.each do |name|
        key(name, type: 'boolean')
      end
    end

    def default_values
      values = {}
      @all_keys.each do |name, key|
        values[name] = key.default
      end
      values
    end

    def validate(easy_setting)
      if @all_keys.has_key?(easy_setting.name)
        @all_keys[easy_setting.name].validate(easy_setting)
      else
        true
      end
    end

    def after_save(easy_setting)
      if @all_keys.has_key?(easy_setting.name)
        @all_keys[easy_setting.name].after_save(easy_setting)
      else
        true
      end
    end

    def from_params(easy_setting, value)
      if @all_keys.has_key?(easy_setting.name)
        @all_keys[easy_setting.name].from_params(easy_setting, value)
      else
        value
      end
    end

    def disabled_from_params?(easy_setting)
      if @all_keys.has_key?(easy_setting.name)
        @all_keys[easy_setting.name].disabled_from_params?
      else
        false
      end
    end

    def skip_blank_params?(easy_setting)
      if @all_keys.has_key?(easy_setting.name)
        @all_keys[easy_setting.name].skip_blank_params?
      else
        false
      end
    end

  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
redmine_extensions-1.2.0 lib/redmine_extensions/easy_settings/mapper.rb
redmine_extensions-1.1.2 lib/redmine_extensions/easy_settings/mapper.rb
redmine_extensions-1.1.1 lib/redmine_extensions/easy_settings/mapper.rb
redmine_extensions-1.1.0 lib/redmine_extensions/easy_settings/mapper.rb
redmine_extensions-1.0.0 lib/redmine_extensions/easy_settings/mapper.rb
redmine_extensions-0.6.3 lib/redmine_extensions/easy_settings/mapper.rb
redmine_extensions-0.6.2 lib/redmine_extensions/easy_settings/mapper.rb
redmine_extensions-0.6.1 lib/redmine_extensions/easy_settings/mapper.rb
redmine_extensions-0.6.0 lib/redmine_extensions/easy_settings/mapper.rb
redmine_extensions-0.4.5 lib/redmine_extensions/easy_settings/mapper.rb
redmine_extensions-0.5.1 lib/redmine_extensions/easy_settings/mapper.rb
redmine_extensions-0.4.4 lib/redmine_extensions/easy_settings/mapper.rb
redmine_extensions-0.4.3 lib/redmine_extensions/easy_settings/mapper.rb
redmine_extensions-0.4.1 lib/redmine_extensions/easy_settings/mapper.rb