Class: Hoaxdb::Db

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

Overview

p products.base #=> [“name”=>{“type”=>“String”,“default”=>“”,“price”=>“type”=>“Float”,“default”=>0“type”=>“Float”,“default”=>0.0,“quantity”=>“type”=>Integer“,”default“=>100, ”shipped“=>”type“=>”Date“,”default“=>nil]

Instance Method Summary collapse

Constructor Details

#initialize(a) ⇒ Db

Returns a new instance of Db.



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

def initialize(a)
    @file = a
    @a  = {}
    thr = Thread.new do 
        if File::exists? a
            begin
                Zlib::GzipReader.open(@file) do |gz|
                    x = JSON.parse(gz.read)
                    x.each{|k,v| @a.store(k,v)}
                end
            rescue Zlib::Error
                raise "We could not read any keys from #{a} the database may be corrupted"
            rescue JSON::ParserError    
                @a = {}
            end 
        else
            Zlib::GzipWriter.open(a).close  
        end
    end
    thr.join    
end

Instance Method Details

#>>(a) ⇒ Object

db >> “data.json”



147
148
149
150
151
# File 'lib/hoaxdb.rb', line 147

def >>(a)
    f = File.open(a,'w')
    f << @a.to_json
    f.close
end

#array_to_xml(k, a) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/hoaxdb.rb', line 226

def array_to_xml(k,a)
    sub = "<#{k}>"
    a.each{|o|
        if isarray(o)
            sub << array_to_xml("array",o);
        elsif ishash(o)
            sub << hash_to_xml("hash",o)
        else
            sub << "<s#{k}>#{o}</s#{k}>"
        end    
    }
    sub << "</#{k}>"
    sub    
end

#clearObject

Deletes all tables in the database



132
133
134
# File 'lib/hoaxdb.rb', line 132

def clear
    @a.clear
end

#commitObject

db.commit #saves the database



137
138
139
140
141
142
143
144
# File 'lib/hoaxdb.rb', line 137

def commit
    thr = Thread.new do
        Zlib::GzipWriter.open(@file) do |gz|
            gz.write(JSON.generate(@a))
        end
    end 
    thr.join    
end

#create_table(name, base) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/hoaxdb.rb', line 98

def create_table(name,base)
    if @a.has_key? name
        raise "Table #{name} already exists"
    end
    tbase = name + "_base"
    @a.store(tbase,{})
    x = Table.new(name,base)
    @a[tbase] = base
    @a.store(name,[])
end

#delObject

db.del



165
166
167
# File 'lib/hoaxdb.rb', line 165

def del
    File.delete(@file)
end

#drop_table(name) ⇒ Object



108
109
110
111
112
# File 'lib/hoaxdb.rb', line 108

def drop_table(name)
    tbase = name + "_base"
    @a.delete(name)
    @a.delete(tbase)
end

#get_json(a) ⇒ Object

db.get_json(“products”)



170
171
172
173
174
# File 'lib/hoaxdb.rb', line 170

def get_json(a)
    x = {}
    x.store(a,@a[a])
    JSON.generate(x)
end

#get_xml(a) ⇒ Object

db.get_xml(“products”)



177
178
179
180
181
182
183
184
185
186
187
# File 'lib/hoaxdb.rb', line 177

def get_xml(a)
    r = ""
    if ishash(@a[a])
        r = self.hash_to_xml(a,@a[a])
    elsif isarray(@a[a])
        r = self.array_to_xml(a,@a[a])
    else
       r = "<#{a}>#{@a[a]}</#{a}>"
    end
    r    
end

#hash_to_xml(x, k) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/hoaxdb.rb', line 240

def hash_to_xml(x,k)
    sub = "<#{x}>"
    k.each do |q,v|
        if isarray(v)
            sub << array_to_xml(q,v);
        elsif ishash(v)
            sub << "<#{q}>"
            v.each{|t,b|
                if isarray(b)
                    sub << array_to_xml(t,b)
                elsif ishash(b)
                    sub << hash_to_xml(t,b);
                else
                    sub << "<#{t}>#{b}</#{t}>"
                end
            }
            sub << "</#{q}>"
        else
           sub << "<#{q}>#{v}</#{q}>" 
        end    
    end
    sub << "</#{x}>"
    sub
end

#isarray(e) ⇒ Object



212
213
214
215
216
217
218
# File 'lib/hoaxdb.rb', line 212

def isarray(e)
    if e.class == Array
        return true
    else
        return false
    end    
end

#ishash(e) ⇒ Object



219
220
221
222
223
224
225
# File 'lib/hoaxdb.rb', line 219

def ishash(e)
    if e.class == Hash
        return true
    else
        return false
    end    
end

#offload(a) ⇒ Object

db.offload(“backup.db”)



190
191
192
193
194
# File 'lib/hoaxdb.rb', line 190

def offload(a)
    Zlib::GzipWriter.open(a) do |gz|
        gz.write(JSON.generate(@a))
    end
end

#pathObject

db.path



154
155
156
# File 'lib/hoaxdb.rb', line 154

def path
    @file
end

#reloadObject

This reloads data from the file and discards unsaved changes



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/hoaxdb.rb', line 196

def reload
    @a.clear
    if File::exists? @file
        begin
            Zlib::GzipReader.open(@file) do |gz|
                x = JSON.parse(gz.read)
                x.each{|k,v| @a.store(k,v)}
            end
        rescue Zlib::Error
            raise "We could not reload #{@file} it may be corrupted"
        rescue JSON::ParserError
        end    
    else
        Zlib::GzipWriter.open(a).close  
    end
end

#rename(a) ⇒ Object

db.rename(“hash.db”)



159
160
161
162
# File 'lib/hoaxdb.rb', line 159

def rename(a)
    File.rename(@file,a)
    @file = a
end

#table(name) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/hoaxdb.rb', line 113

def table(name)
    if @a.has_key? name and !name.match(/_base$/)
        tbase = name + "_base"
        x = Table.new(name,@a[tbase])
        x.inject(@a[tbase],@a[name])
        return x
    elsif name.match(/_base$/)
        raise "#{name} cannot be edited directly"
    else
        raise "Table #{name} does not exist"
    end
end

#tablesObject

Lists all the tables in the database



126
127
128
129
130
# File 'lib/hoaxdb.rb', line 126

def tables
    tables = []
    @a.keys.each{|e| if !e.match(/_base$/) then tables.push(e) end}
    tables
end