Class: Ini

Inherits:
Object
  • Object
show all
Defined in:
lib/ini.rb

Overview

Copyright 2013 whiteleaf. All rights reserved.

Defined Under Namespace

Classes: NoFilenameError

Constant Summary collapse

DELIMITER =
"="
GLOBAL_SECTION =
"global"
COMMENT_SYMBOL =
";"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text = "") ⇒ Ini

Returns a new instance of Ini.



40
41
42
43
44
# File 'lib/ini.rb', line 40

def initialize(text = "")
  clear
  parse(text)
  @filename = nil
end

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



11
12
13
# File 'lib/ini.rb', line 11

def filename
  @filename
end

Class Method Details

.load(text) ⇒ Object



19
20
21
22
# File 'lib/ini.rb', line 19

def self.load(text)
  ini = new(text)
  ini.object
end

.load_file(file) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ini.rb', line 24

def self.load_file(file)
  text = ""
  case
  when file.kind_of?(String)
    text = open(file, "r:BOM|UTF-8") { |fp| fp.read }
    ini = new(text)
    ini.filename = file
    return ini.object
  when file.respond_to?(:read)
    text = file.read
    return load(text)
  else
    raise NoFilenameError
  end
end

Instance Method Details

#cast(str) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ini.rb', line 72

def cast(str)
  value = nil
  case str
  when /^[+-]?\d+$/
    value = str.to_i
  when /^[+-]?\d*\.\d+$/
    value = str.to_f
  when /^true$/i
    value = true
  when /^false$/i
    value = false
  when /^(?:nil|null)$/i
    value = nil
  else
    if str.length > 0
      if str =~ /^(["'])(.+)\1$/
        value = $2
      else
        value = str
      end
    else
      value = nil
    end
  end
  value
end

#clearObject



50
51
52
# File 'lib/ini.rb', line 50

def clear
  @data = { GLOBAL_SECTION => {} }
end

#objectObject



46
47
48
# File 'lib/ini.rb', line 46

def object
  @data
end

#parse(text) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ini.rb', line 54

def parse(text)
  section = GLOBAL_SECTION
  text.each_line do |line|
    next if line.strip == ""
    next if line[0] == COMMENT_SYMBOL
    key, value = line.split(DELIMITER, 2).map(&:strip)
    if key && value
      @data[section][key] = cast(value)
      next
    end
    if /^\[(.+?)\]/ =~ line
      section = $1.strip
      @data[section] ||= {}
      next
    end
  end
end

#save(filename = nil) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ini.rb', line 99

def save(filename = nil)
  filename ||= @filename
  unless filename
    raise NoFilenameError
  end
  open(@filename, "w") do |fp|
    @data.each do |section, values|
      if section != GLOBAL_SECTION
        fp.puts("[#{section}]")
      end
      values.each do |key, value|
        fp.puts(key + DELIMITER + value)
      end
    end
  end
end