Module: Fini

Defined in:
lib/fini.rb,
lib/fini/version.rb

Defined Under Namespace

Classes: Ini, IniObject

Constant Summary collapse

NUMBER_REGEX =
/\A[+-]?[\d]+\.?\d*\Z/
VERSION =
"1.0.0"

Class Method Summary collapse

Class Method Details

.is_number?(string) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
27
# File 'lib/fini.rb', line 18

def self.is_number? string
    # Regex has the best performance overall, and it doesn't take longer with
    # long strings that are not numbers. In other words, getting a false is very fast,
    # and getting a true is only slightly slower than using Float()
    if NUMBER_REGEX === string
        true
    else
        false
    end
end

.parse(content) ⇒ Fini::IniObject

A static method for parsing a string with ini content.

Parameters:

  • content (String)

    Ini text to parse

Returns:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fini.rb', line 33

def self.parse content
    source = content
    data = IniObject.new
    section = nil
    source.lines do |line|
        line.strip!
        case line[0]
            when '['
                key = line.delete "[]"
                section = data.get_section key
            when ';'
                next
            else
                next if section.nil?
                pos = line.index '='
                unless pos.nil?
                    key = line[0, pos]
                    val = line[(pos + 1)..-1]
                    unless key.nil? || val.nil?
                        key.strip!
                        val.strip!
                        if Fini.is_number? val
                            if val.include?('.')
                                section[key] = Float(val)
                            else
                                section[key] = Integer(val)
                            end
                        elsif (val[0] == '"' && val[-1] == '"') or (val[0] == "'" && val[-1] == "'")
                            section[key] = val.chop.reverse.chop.reverse
                        elsif val == "true"
                            section[key] = true
                        elsif val == "false"
                            section[key] = false
                        else
                            section[key] = val
                        end
                    end
                end
        end
    end
    @data = data
end