Sha256: 0dc80bc1d86c5354cd8c5bbc038ab20ef4e527d13f8e964f7c3fcf673fbe82d5

Contents?: true

Size: 882 Bytes

Versions: 2

Compression:

Stored size: 882 Bytes

Contents

# coding: utf-8

module Confuse
  module Converter
    class << self
      def converters
        @converters ||= { }
      end

      def register(key, &block)
        converters[key] = block
      end

      def [](key)
        converters[key]
      end
    end

    register(String) { |item| item.to_s }

    register Fixnum do |item|
      raise Errors::Invalid unless /^\d*$/.match item.to_s
      item.to_i
    end

    register Float do |item|
      raise Errors::Invalid unless /^\d*(\.\d*)?$/.match item.to_s
      item.to_f
    end

    register :bool do |item|
      raise Errors::Invalid unless %w(true false).include? item.to_s
      item.to_s == 'true'
    end

    register Array do |item|
      if item.respond_to? :to_ary
        item.to_ary
      elsif item.include? ','
        item.split(',')
      else
        raise Errors::Invalid
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
confuse-1.1.1 lib/confuse/converter.rb
confuse-1.1.0 lib/confuse/converter.rb