Sha256: f5219436ca77f5632c9459494cfeba8691d04eef282ebf8e3cb8fc55f6d9c122

Contents?: true

Size: 1005 Bytes

Versions: 1

Compression:

Stored size: 1005 Bytes

Contents

module IniReader
  class Base
    def initialize( string = '')
      @section_hash = {}
      parse(StringIO.new(string))
    end

    def self.parse(filename)
      reader = IniReader::Base.new
      reader.send(:parse, File.open(filename, "r"))
      return reader
    end

    #returns the hash for a section of the inifile
    def []( section_name )
      return @section_hash[section_name]
    end

    private
    def parse(io)
      current_section = nil
      io.each do |line|
        if line[0] == ';'
          next
        elsif ( matchdata = line.match(/^\[(.+)\]$/) ) != nil
          current_section = @section_hash[matchdata[1].to_sym] || {}
          @section_hash[matchdata[1].to_sym] = current_section
        elsif ( matchdata = line.match(/^([^=]*)=(.*)$/) )
          current_section[matchdata[1].to_sym] = matchdata[2].to_s
        elsif line.strip == ""
          next
        else
          raise Exception("Could not parse line: #{line}")
        end
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
inireader-0.0.1 lib/inireader/base.rb