Class: KeyDB

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ KeyDB

Returns a new instance of KeyDB.



4
5
6
7
8
9
10
11
# File 'lib/keydb.rb', line 4

def initialize filename
  @filename = filename.split(".")
  @filename = "#{@filename[0]}.db"
  unless Dir.entries('.').detect {|f| f.match "#{@filename}"}
    @db_file = File.new(@filename, "w")
    @db_file.close
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(keyname, *value) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/keydb.rb', line 84

def method_missing(keyname, *value)
  keyname = keyname.to_s
  if keyname =~ /=/
    keyname.gsub!("=", "")
    value = value[0]
    if keyname =~ /\/\/\/|\(---\)|-\*\*\*-/
      raise "Invalid Keyname!"
      return
    end
    File.open(@filename, "r").each_line do |line|
      if Marshal.load(line).split("///")[0] == keyname
        raise "Keynames must be unique"
        return
      end
    end
    @db_file = File.open(@filename, "a")
    @to_write = (Marshal.dump("#{keyname}///#{value}"))
    @db_file.write(@to_write)
    @db_file.write("\n")
    @db_file.close
  else
    File.open(@filename, "r").each_line do |line|
      if Marshal.load(line).split("(---)")[1] == keyname
        return "Key has been deleted"
      elsif Marshal.load(line).split("-***-")[1] == keyname
        return Marshal.load(line).split("-***-")[2]
      end
    end
    File.open(@filename, "r").each_line do |line|
      line = Marshal.load(line)
      if line.split("///")[0] == keyname
        return line.split("///")[1]
      end
    end
  end 
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



2
3
4
# File 'lib/keydb.rb', line 2

def filename
  @filename
end

#versionObject (readonly)

Returns the value of attribute version.



2
3
4
# File 'lib/keydb.rb', line 2

def version
  @version
end

Instance Method Details

#change_key(keyname, value) ⇒ Object



34
35
36
37
38
39
# File 'lib/keydb.rb', line 34

def change_key keyname, value
  @db_file = File.open(@filename, "a")
  @db_file.write(Marshal.dump("CK-***-#{keyname}-***-#{value}"))
  @db_file.write("\n")
  @db_file.close
end

#delete_dbObject



75
76
77
# File 'lib/keydb.rb', line 75

def delete_db
  system("rm #{@filename}")
end

#delete_key(keyname) ⇒ Object



41
42
43
44
45
46
# File 'lib/keydb.rb', line 41

def delete_key keyname
  @db_file = File.open(@filename, "a")
  @db_file.write(Marshal.dump("DK(---)#{keyname}"))
  @db_file.write("\n")
  @db_file.close
end

#keysObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/keydb.rb', line 52

def keys
  to_ignore = Array.new
  File.open(@filename).each_line do |line|
    if Marshal.load(line).split("(---)")[1] != nil
      to_ignore.push(Marshal.load(line).split("(---)")[1])
    elsif Marshal.load(line).split("-***-")[1] != nil
      to_ignore.push(Marshal.load(line).split("-***-")[1])
    end
  end
  puts to_ignore
  File.open(@filename).each_line do |line|
    line = Marshal.load(line)
    @keys_and_vals = line.split('///')
    if line.split("(---)").length == 2 or to_ignore.include? line.split("///")[0]
      print
    elsif to_ignore.include? line.split("-***-")[1] 
      print  
    else
      puts @keys_and_vals[0]
    end
  end
end

#new_key(keyname, value) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/keydb.rb', line 13

def new_key keyname, value
  if keyname =~ /\/\/\/|\(---\)|-\*\*\*-/
    raise "Invalid Keyname!"
    return
  end
  File.open(@filename, "r").each_line do |line|
    if Marshal.load(line).split("///")[0] == keyname
      raise "Keynames must be unique"
      return
    end
  end
  if keyname.class != String
    warn "WARNING: Keyname should be a string"
  end
  @db_file = File.open(@filename, "a")
  @to_write = (Marshal.dump("#{keyname}///#{value}"))
  @db_file.write(@to_write)
  @db_file.write("\n")
  @db_file.close
end

#to_sObject



48
49
50
# File 'lib/keydb.rb', line 48

def to_s
  return "KeyDB-#{@filename}"
end

#wipe_dbObject



79
80
81
82
# File 'lib/keydb.rb', line 79

def wipe_db
  delete_db
  File.new(@filename, "w")
end