Sha256: 62611bc8eec8cd98c970f59f6129d99a9bd006c4275706c50c02772561ca8898

Contents?: true

Size: 1.53 KB

Versions: 4

Compression:

Stored size: 1.53 KB

Contents

# This code is free software; you can redistribute it and/or modify it under
# the terms of the new BSD License.
#
# Copyright (c) 2010, Sebastian Staudt

module Rubikon

  module Config

    # A configuration provider loading configuration data from INI files
    #
    # @author Sebastian Staudt
    # @since 0.5.0
    class IniProvider

      # Loads a configuration Hash from a INI file
      #
      # This method is taken from code written by gdsx in #ruby-lang (see
      # http://snippets.dzone.com/posts/show/563).
      #
      # @param [String] file The path of the file to load
      # @return [Hash] The configuration data loaded from the file
      def self.load_config(file)
        content = File.new(file).readlines.map do |line|
          line.gsub(/(?:#|;).*/, '').strip
        end.join("\n")

        config = {}
        content = content.split(/\[([^\]]+)\]/)[1..-1]
        content.inject([]) do |temp, field|
          temp << field
          if temp.length == 2
            value = temp[1].sub(/^\s+/,'').sub(/\s+$/,'')
            if config[temp[0]].nil?
              config[temp[0]] = value
            else
              config[temp[0]] << "\n#{value}"
            end
            temp.clear
          end
          temp
        end

        config.dup.each do |key, value|
          value_list = value.split /[\r\n]+/
          config[key] = value_list.inject({}) do |hash, val|
            k, v = val.split /\s*=\s*/
            hash[k] = v
            hash
          end
        end

        config
      end

    end

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubikon-0.5.3 lib/rubikon/config/ini_provider.rb
rubikon-0.5.2 lib/rubikon/config/ini_provider.rb
rubikon-0.5.1 lib/rubikon/config/ini_provider.rb
rubikon-0.5.0 lib/rubikon/config/ini_provider.rb