Sha256: a0ed60fdea976db9550fd7e6be3638321ef2a7daae95aa9e41f9c093fcd7d04a

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

require "king_konf/duration_decoder"

module KingKonf
  module Decoder
    extend self

    def boolean(value, true_values: ["true", "1"], false_values: ["false", "0"])
      if true_values.include?(value)
        true
      elsif false_values.include?(value)
        false
      else
        values = true_values + false_values
        raise ConfigError, "#{value.inspect} is not a boolean: must be one of #{values.join(', ')}"
      end
    end

    def string(value, **)
      value
    end

    def symbol(value, **)
      value.to_sym
    end

    def list(value, sep: ",", items: :string)
      value.split(sep).map {|s| public_send(items, s) }
    end

    def integer(value, **)
      Integer(value)
    rescue ArgumentError
      raise ConfigError, "#{value.inspect} is not an integer"
    end

    def float(value, **)
      Float(value)
    end

    def duration(value, **)
      case value
      when ""
        nil
      when/^\d*$/
        value.to_i
      else
        DurationDecoder.decode(value)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
king_konf-0.3.1 lib/king_konf/decoder.rb
king_konf-0.3.0 lib/king_konf/decoder.rb