Class: ToadSpawn::Base
- Inherits:
-
Object
- Object
- ToadSpawn::Base
- Defined in:
- lib/toad_spawn/base.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #any? ⇒ Boolean
- #cast(value, klass) ⇒ Object
- #clear ⇒ Object
- #delete(key) ⇒ Object
- #empty? ⇒ Boolean
- #flush ⇒ Object
- #format(key, value) ⇒ Object
- #has_key?(key) ⇒ Boolean
- #has_value?(value) ⇒ Boolean
-
#initialize(path) ⇒ Base
constructor
A new instance of Base.
- #key_file_path(key) ⇒ Object
- #keys ⇒ Object
- #klass(data) ⇒ Object
- #read_file(path) ⇒ Object
- #size ⇒ Object
- #to_hash ⇒ Object
- #value(data) ⇒ Object
- #values ⇒ Object
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
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 |
#clear ⇒ Object
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
70 71 72 |
# File 'lib/toad_spawn/base.rb', line 70 def empty? @cache.empty? end |
#flush ⇒ Object
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
79 80 81 |
# File 'lib/toad_spawn/base.rb', line 79 def has_key?(key) @cache.has_key?(key) end |
#has_value?(value) ⇒ 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 |
#keys ⇒ Object
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 |
#size ⇒ Object
95 96 97 |
# File 'lib/toad_spawn/base.rb', line 95 def size @cache.size end |
#to_hash ⇒ Object
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 |
#values ⇒ Object
91 92 93 |
# File 'lib/toad_spawn/base.rb', line 91 def values @cache.values end |