Class: Ini
- Inherits:
-
Object
- Object
- Ini
- 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
-
#filename ⇒ Object
Returns the value of attribute filename.
Class Method Summary collapse
Instance Method Summary collapse
- #cast(str) ⇒ Object
- #clear ⇒ Object
-
#initialize(text = "") ⇒ Ini
constructor
A new instance of Ini.
- #object ⇒ Object
- #parse(text) ⇒ Object
- #save(filename = nil) ⇒ Object
Constructor Details
#initialize(text = "") ⇒ Ini
Returns a new instance of Ini.
39 40 41 42 43 |
# File 'lib/ini.rb', line 39 def initialize(text = "") clear parse(text) @filename = nil end |
Instance Attribute Details
#filename ⇒ Object
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 |
# File 'lib/ini.rb', line 24 def self.load_file(file) 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
71 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 |
# File 'lib/ini.rb', line 71 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 |
#clear ⇒ Object
49 50 51 |
# File 'lib/ini.rb', line 49 def clear @data = { GLOBAL_SECTION => {} } end |
#object ⇒ Object
45 46 47 |
# File 'lib/ini.rb', line 45 def object @data end |
#parse(text) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/ini.rb', line 53 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
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/ini.rb', line 98 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| value = "\"#{value}\"" if value.kind_of?(String) fp.puts("#{key} #{DELIMITER} #{value}") end end end end |