Sha256: a0127b92a82c239f4d28dd8dab87469f70cdf40a92c1c6e93dabfb960b6de271

Contents?: true

Size: 1.07 KB

Versions: 5

Compression:

Stored size: 1.07 KB

Contents

require_relative 'validation'
require_relative 'settings'

module UltraConfig
  class Config
    include Validation

    attr_reader :value

    def initialize(name, parents, options = {}, &block)
      @name = name
      @parents = parents
      @config_block = block

      @value = options[:default].nil? ? nil : options[:default]
      @sanitize = options[:sanitize] || false
      @error_msg = options[:error_msg]
    end

    def value=(value)
      @intermediate_value = value
      self.instance_eval(&@config_block) if @config_block
      type_safety(Settings.type_safety) unless @type_safety_checked
      @value = @intermediate_value
    rescue UltraConfig::Validation::ValidationError
      raise UltraConfig::Validation::ValidationError.new(@error_msg, @parents + [@name], sanitize? ? '*****' : @intermediate_value)
    ensure
      @type_safety_checked = false
      @intermediate_value = nil
    end

    def set(&block)
      @intermediate_value = yield(@intermediate_value)
    end

    def sanitize?
      @sanitize
    end

    def to_s
      "\"#{@value.to_s}\""
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ultra_config-0.15.2 lib/ultra_config/config.rb
ultra_config-0.15.1 lib/ultra_config/config.rb
ultra_config-0.15.0 lib/ultra_config/config.rb
ultra_config-0.14.0 lib/ultra_config/config.rb
ultra_config-0.13.0 lib/ultra_config/config.rb