Class: ToadSpawn::Base

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

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Base

Returns a new instance of Base.



3
4
5
6
7
8
9
# File 'lib/toad_spawn/base.rb', line 3

def initialize(path)
  raise "Directory does not exist" unless File.exist?(path)
  raise "Not a directory" unless File.directory?(path)
  raise "Not writable" unless File.writable?(path)
  @path = path
  flush
end

Instance Method Details

#[](key) ⇒ Object



50
51
52
# File 'lib/toad_spawn/base.rb', line 50

def [](key)
  @cache[key]
end

#[]=(key, value) ⇒ Object



38
39
40
41
42
43
# File 'lib/toad_spawn/base.rb', line 38

def []=(key, value)
  File.open(key_file_path(key), "w") do |f|
    f.write format(key, value)
  end
  @cache[key] = cast(value, value.class.to_s)
end

#any?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/toad_spawn/base.rb', line 66

def any?
  @cache.any?
end

#cast(value, klass) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/toad_spawn/base.rb', line 27

def cast(value, klass)
  case klass
  when 'Fixnum'
    value.to_i
  when 'Float'
    value.to_f
  else
    value.to_s
  end
end

#clearObject



99
100
101
102
103
104
# File 'lib/toad_spawn/base.rb', line 99

def clear
  Dir.glob("#{@path}/*.value").each do |path|
    File.delete(path)
  end
  @cache = {}
end

#delete(key) ⇒ Object



74
75
76
77
# File 'lib/toad_spawn/base.rb', line 74

def delete(key)
  File.delete key_file_path(key)
  @cache.delete(key)
end

#empty?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/toad_spawn/base.rb', line 70

def empty?
  @cache.empty?
end

#flushObject



54
55
56
57
58
59
60
# File 'lib/toad_spawn/base.rb', line 54

def flush
  @cache = {}
  Dir.glob("#{@path}/*.value").each do |path|
    key, value = read_file(path)
    @cache[key] = value
  end
end

#format(key, value) ⇒ Object



15
16
17
# File 'lib/toad_spawn/base.rb', line 15

def format(key, value)
  "#{value.class}\n#{value}"
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/toad_spawn/base.rb', line 79

def has_key?(key)
  @cache.has_key?(key)
end

#has_value?(value) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/toad_spawn/base.rb', line 83

def has_value?(value)
  @cache.has_value?(value)
end

#key_file_path(key) ⇒ Object



11
12
13
# File 'lib/toad_spawn/base.rb', line 11

def key_file_path(key)
  File.join(@path, key.to_s + ".value")
end

#keysObject



87
88
89
# File 'lib/toad_spawn/base.rb', line 87

def keys
  @cache.keys
end

#klass(data) ⇒ Object



19
20
21
# File 'lib/toad_spawn/base.rb', line 19

def klass(data)
  data[/^(.+)\n/,1]
end

#read_file(path) ⇒ Object



45
46
47
48
# File 'lib/toad_spawn/base.rb', line 45

def read_file(path)
  data = File.read(path)
  [File.basename(path, ".value").to_sym, cast(value(data), klass(data))]
end

#sizeObject



95
96
97
# File 'lib/toad_spawn/base.rb', line 95

def size
  @cache.size
end

#to_hashObject



62
63
64
# File 'lib/toad_spawn/base.rb', line 62

def to_hash
  @cache
end

#value(data) ⇒ Object



23
24
25
# File 'lib/toad_spawn/base.rb', line 23

def value(data)
  data.sub(/^.+\n/,'')
end

#valuesObject



91
92
93
# File 'lib/toad_spawn/base.rb', line 91

def values
  @cache.values
end