Class: Drawer
- Inherits:
-
Object
- Object
- Drawer
- Defined in:
- lib/drawer.rb
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#store ⇒ Object
readonly
Returns the value of attribute store.
Class Method Summary collapse
- .create(file) ⇒ Object
- .open(file) {|drawer| ... } ⇒ Object
- .open!(file) ⇒ Object
- .remove(file) ⇒ Object
Instance Method Summary collapse
- #add(k, v) ⇒ Object
- #flush_all ⇒ Object
- #get(k) ⇒ Object
- #get_multi(*ks) ⇒ Object
-
#initialize(file, store = DrawerStore) ⇒ Drawer
constructor
A new instance of Drawer.
- #inspect ⇒ Object
- #remove(k) ⇒ Object
- #save(file) ⇒ Object
- #set(k, v) ⇒ Object
Constructor Details
#initialize(file, store = DrawerStore) ⇒ Drawer
Returns a new instance of Drawer.
19 20 21 22 23 24 25 26 |
# File 'lib/drawer.rb', line 19 def initialize(file, store = DrawerStore) @store = store @cache = store.load(file) || {} at_exit do save(file) end end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
16 17 18 |
# File 'lib/drawer.rb', line 16 def cache @cache end |
#store ⇒ Object (readonly)
Returns the value of attribute store.
17 18 19 |
# File 'lib/drawer.rb', line 17 def store @store end |
Class Method Details
.create(file) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/drawer.rb', line 73 def self.create(file) unless File.exists?(file) FileUtils.mkdir_p File.dirname(file) FileUtils.touch(file) end end |
.open(file) {|drawer| ... } ⇒ Object
60 61 62 63 64 65 |
# File 'lib/drawer.rb', line 60 def self.open(file, &block) file = File.(file) drawer = Drawer.new(file) yield drawer if block_given? drawer end |
.open!(file) ⇒ Object
67 68 69 70 71 |
# File 'lib/drawer.rb', line 67 def self.open!(file) file = File.(file) create(file) open(file) end |
.remove(file) ⇒ Object
80 81 82 |
# File 'lib/drawer.rb', line 80 def self.remove(file) FileUtils.rm(file) end |
Instance Method Details
#add(k, v) ⇒ Object
40 41 42 |
# File 'lib/drawer.rb', line 40 def add(k, v) set(k, v) unless cache[k] end |
#flush_all ⇒ Object
48 49 50 |
# File 'lib/drawer.rb', line 48 def flush_all cache.clear end |
#get(k) ⇒ Object
28 29 30 |
# File 'lib/drawer.rb', line 28 def get(k) cache[k] or (set(k, yield) if block_given?) end |
#get_multi(*ks) ⇒ Object
32 33 34 |
# File 'lib/drawer.rb', line 32 def get_multi(*ks) ks.collect { |k| get(k) } end |
#inspect ⇒ Object
52 53 54 |
# File 'lib/drawer.rb', line 52 def inspect "Drawer count: #{cache.size}. Type 'cache' to view the content." end |
#remove(k) ⇒ Object
44 45 46 |
# File 'lib/drawer.rb', line 44 def remove(k) cache.delete(k) end |
#save(file) ⇒ Object
56 57 58 |
# File 'lib/drawer.rb', line 56 def save(file) store.save(@cache, file) end |
#set(k, v) ⇒ Object
36 37 38 |
# File 'lib/drawer.rb', line 36 def set(k, v) cache[k] = v end |