Class: Datastorage

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

Class Method Summary collapse

Class Method Details

.delete(sadkey) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/datastore.rb', line 75

def self::delete sadkey
    if $datafile == "" || !File.exist?($datafile)
        return false
    end
    # Verify that the key exists in the file.
    File.open($datafile, "r") do |f|
        f.each_line do |line|
            key, value = line.split(" | ")
            if key.to_s == sadkey.to_s
                $existence = true
            end
        end
        if $existence == false
            return false
        end
    end
    hashmap = Hash.new
    File.open($datafile, "r") do |f|
        f.each_line do |line|
            key, value = line.split(" | ")
            hashmap[key] = value
        end
    end
    
    File.open($datafile, "w+") do |f|
        hashmap.each do |key, value|
            if key != sadkey
                f.puts "#{key} | #{value}"
            end
        end
    end
end

.open(file) ⇒ Object



4
5
6
7
# File 'lib/datastore.rb', line 4

def self::open file
    $datafile = file
    return true
end

.parseObject



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/datastore.rb', line 9

def self::parse
    if $datafile == "" || !File.exist?($datafile)
        return false
    end
    hashmap = Hash.new
    File.open($datafile, "r") do |f|
        f.each_line do |line|
            key, value = line.split(" | ")
            hashmap[key] = value
        end
    end
    return hashmap
end

.update(coolkey, newvalue) ⇒ Object



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
# File 'lib/datastore.rb', line 40

def self::update coolkey, newvalue
    if $datafile == "" || !File.exist?($datafile)
        return false
    end
    File.open($datafile, "r") do |f|
        f.each_line do |line|
            key, value = line.split(" | ")
            if key.to_s == coolkey.to_s
                $existence = true
            end
        end
        if $existence == false
            return false
        end
    end

    hashmap = Hash.new
    File.open($datafile, "r") do |f|
        f.each_line do |line|
            key, value = line.split(" | ")
            hashmap[key] = value
        end
    end

    File.open($datafile, "w+") do |f|
        hashmap.each do |key, value|
            if key == coolkey
                f.puts "#{key} | #{newvalue}"
            else
                f.puts "#{key} | #{value}"
            end
        end
    end
end

.write(wkey, value) ⇒ Object



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

def self::write wkey, value
    if $datafile == "" || !File.exist?($datafile)
        return false
    end
    File.open($datafile, "r") do |f|
        f.each_line do |line|
            key, value = line.split(" | ")
            if key == wkey
                return false # Key already exists.
            end
        end
    end
    File.open($datafile, "r+") do |f|
        f.puts "#{wkey} | #{value}"
    end
end