drawer 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2008 Michel Martens
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,34 @@
1
+ = Drawer
2
+
3
+ Ultra slim file-based cache.
4
+
5
+ == Usage
6
+
7
+ require 'drawer'
8
+
9
+ cache = Drawer.new('db/development.drawer')
10
+ cache.get('foo') #=> nil
11
+ cache.set('foo', 123)
12
+ cache.get('foo') #=> 123
13
+ cache.flush_all
14
+ cache.get('foo') #=> nil
15
+ cache.set('bar', 456)
16
+ cache.set('baz', 789)
17
+ cache.get_multi('bar', 'baz') #=> [456, 789]
18
+
19
+ An optional second parameter is a class to be used as the persistence layer.
20
+ By default, DrawerStore is used, but it can be replaced by any class that
21
+ provides load and save methods. For example:
22
+
23
+ cache = Drawer.new(some_file, MyStoreClass)
24
+
25
+ Now, MyStoreClass.load(some_file) and MyStoreClass.save(@cache, some_file) will
26
+ be used for retrieving and saving the cached contents.
27
+
28
+ == Installation
29
+
30
+ $ gem sources -a http://gems.github.com (you only have to do this once)
31
+ $ sudo gem install soveran-drawer
32
+
33
+ Copyright (c) 2008 Michel Martens.
34
+ Released under the MIT license.
data/Rakefile ADDED
@@ -0,0 +1,38 @@
1
+ require 'rake'
2
+ require 'rake/gempackagetask'
3
+ require 'rake/testtask'
4
+ require 'rake/clean'
5
+
6
+ gem_spec_file = 'drawer.gemspec'
7
+
8
+ gem_spec = eval(File.read(gem_spec_file)) rescue nil
9
+
10
+ Rake::GemPackageTask.new(gem_spec) do |pkg|
11
+ pkg.need_zip = false
12
+ pkg.need_tar = false
13
+ rm_f FileList['pkg/**/*.*']
14
+ end if gem_spec
15
+
16
+ task :default => :test
17
+
18
+ desc "Run tests"
19
+ Rake::TestTask.new do |t|
20
+ t.libs << "drawer"
21
+ t.test_files = FileList['test/*.rb']
22
+ t.verbose = true
23
+ end
24
+
25
+
26
+ desc "Generate the gemspec file."
27
+ task :gemspec do
28
+ require 'erb'
29
+
30
+ File.open(gem_spec_file, 'w') do |f|
31
+ f.write ERB.new(File.read("#{gem_spec_file}.erb")).result(binding)
32
+ end
33
+ end
34
+
35
+ desc "Builds and installs the gem."
36
+ task :install => :repackage do
37
+ `sudo gem install pkg/#{gem_spec.name}-#{gem_spec.version}.gem`
38
+ end
data/bin/drawer ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ unless file_name = ARGV.first
4
+ puts "Usage: drawer path_to_drawer_file"
5
+ exit 1
6
+ end
7
+
8
+ unless File.exists?(file_name)
9
+ puts "The file #{file_name} doesn't exist"
10
+ exit
11
+ end
12
+
13
+ ENV['DRAWER'] = file_name
14
+
15
+ require "drawer/console"
@@ -0,0 +1,2 @@
1
+ --- {}
2
+
@@ -0,0 +1,23 @@
1
+ require File.join(File.dirname(__FILE__), '../lib/drawer')
2
+
3
+ cache_file = File.join(File.dirname(__FILE__), 'db/development.yml')
4
+
5
+ cache = Drawer.new(cache_file)
6
+
7
+ puts "Store the value 123 in the 'foo' key."
8
+ puts "=> " + cache.set("foo", 123).inspect
9
+
10
+ puts "Store the value 456 in the 'bar' key."
11
+ puts "=> " + cache.set("bar", 456).inspect
12
+
13
+ puts "Retrieve the value for the key 'foo'."
14
+ puts "=> " + cache.get("foo").inspect
15
+
16
+ puts "Retrieve the value for the keys 'foo' and 'bar'."
17
+ puts "=> " + cache.get_multi("foo", "bar").inspect
18
+
19
+ puts "Flush the cache."
20
+ puts "=> " + cache.flush_all.inspect
21
+
22
+ puts "Try to get foo again."
23
+ puts "=> " + cache.get("foo").inspect
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'drawer'
3
+
4
+ cache_file = File.join(File.dirname(__FILE__), 'db/development.yml')
5
+
6
+ cache = Drawer.new(cache_file)
7
+
8
+ puts "Store the value 123 in the given key."
9
+ puts "=> " + cache.set("foo", 123).inspect
10
+
11
+ puts "Retrieve the value for the key foo."
12
+ puts "=> " + cache.get("foo").inspect
13
+
14
+ puts "Flush the cache."
15
+ puts "=> " + cache.flush_all.inspect
16
+
17
+ puts "Try to get foo again."
18
+ puts "=> " + cache.get("foo").inspect
data/lib/drawer.rb ADDED
@@ -0,0 +1,83 @@
1
+ require 'yaml'
2
+
3
+ class DrawerStore
4
+ def self.load(file)
5
+ YAML.load_file(File.expand_path(file))
6
+ end
7
+
8
+ def self.save(object, file)
9
+ File.open(File.expand_path(file), 'w') do |f|
10
+ YAML.dump(object, f)
11
+ end
12
+ end
13
+ end
14
+
15
+ class Drawer
16
+ attr :cache
17
+ attr :store
18
+
19
+ def initialize(file, store = DrawerStore)
20
+ @store = store
21
+ @cache = store.load(file) || {}
22
+
23
+ at_exit do
24
+ save(file)
25
+ end
26
+ end
27
+
28
+ def get(k)
29
+ cache[k] or (set(k, yield) if block_given?)
30
+ end
31
+
32
+ def get_multi(*ks)
33
+ ks.collect { |k| get(k) }
34
+ end
35
+
36
+ def set(k, v)
37
+ cache[k] = v
38
+ end
39
+
40
+ def add(k, v)
41
+ set(k, v) unless cache[k]
42
+ end
43
+
44
+ def remove(k)
45
+ cache.delete(k)
46
+ end
47
+
48
+ def flush_all
49
+ cache.clear
50
+ end
51
+
52
+ def inspect
53
+ "Drawer count: #{cache.size}. Type 'cache' to view the content."
54
+ end
55
+
56
+ def save(file)
57
+ store.save(@cache, file)
58
+ end
59
+
60
+ def self.open(file, &block)
61
+ file = File.expand_path(file)
62
+ drawer = Drawer.new(file)
63
+ yield drawer if block_given?
64
+ drawer
65
+ end
66
+
67
+ def self.open!(file)
68
+ file = File.expand_path(file)
69
+ create(file)
70
+ open(file)
71
+ end
72
+
73
+ def self.create(file)
74
+ unless File.exists?(file)
75
+ FileUtils.mkdir_p File.dirname(file)
76
+ FileUtils.touch(file)
77
+ end
78
+ end
79
+
80
+ def self.remove(file)
81
+ FileUtils.rm(file)
82
+ end
83
+ end
@@ -0,0 +1,2 @@
1
+ bootstrap = File.join(File.dirname(__FILE__), 'irb.rb')
2
+ system "irb --simple-prompt #{bootstrap}"
data/lib/drawer/irb.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'drawer'
3
+
4
+ IRB.conf[:IRB_RC] = lambda do
5
+ system 'clear'
6
+ end
7
+
8
+ irb Drawer.new(ENV['DRAWER'])
data/test/db/test.yml ADDED
@@ -0,0 +1,2 @@
1
+ --- {}
2
+
@@ -0,0 +1,112 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'shoulda'
4
+
5
+ require File.join(File.dirname(__FILE__), '../lib/drawer')
6
+
7
+ def cache_file
8
+ @cache_file ||= File.join(File.dirname(__FILE__), 'db/test.yml')
9
+ end
10
+
11
+ class TestDrawer < Test::Unit::TestCase
12
+ context "drawer" do
13
+ setup do
14
+ Drawer.create(cache_file)
15
+ @drawer = Drawer.new(cache_file)
16
+ @drawer.flush_all
17
+ end
18
+
19
+ should "return nil if a key is not found" do
20
+ assert_nil @drawer.get("foo")
21
+ end
22
+
23
+ should "set a key with a block on a cache miss" do
24
+ assert_equal 123, @drawer.get("foo") { 123 }
25
+ assert_nothing_raised { @drawer.get("foo") { raise 'Should not call block.' } }
26
+ end
27
+
28
+ should "successfully set a key" do
29
+ @drawer.set("foo", 123)
30
+ assert_equal 123, @drawer.get("foo")
31
+
32
+ assert_equal 124, @drawer.set("foo", 124)
33
+ end
34
+
35
+ should "clear an entry with remove" do
36
+ @drawer.set("foo", 123)
37
+ @drawer.remove("foo")
38
+ assert_nil @drawer.get("foo")
39
+ end
40
+
41
+ context "flush_all" do
42
+ setup do
43
+ @drawer.set("foo", 123)
44
+ @drawer.flush_all
45
+ end
46
+
47
+ should "clear all keys" do
48
+ assert_nil @drawer.get("foo")
49
+ end
50
+ end
51
+
52
+ context "get_multi" do
53
+ setup do
54
+ 10.times do |i|
55
+ @drawer.set(i, i * i)
56
+ end
57
+ end
58
+
59
+ should "return a list of values" do
60
+ assert_equal [9, 25, 49], @drawer.get_multi(3, 5, 7)
61
+ end
62
+ end
63
+
64
+ context "class methods" do
65
+ setup do
66
+ Drawer.remove(cache_file)
67
+ Drawer.create(cache_file)
68
+ end
69
+
70
+ context "open" do
71
+ should "return an instance of Drawer" do
72
+ assert_kind_of Drawer, Drawer.open(cache_file)
73
+ end
74
+ end
75
+
76
+ context "open with block" do
77
+ should "call the passed block" do
78
+ drawer = Drawer.open(cache_file) do |cache|
79
+ cache.set("foo", 123)
80
+ end
81
+ assert_equal 123, drawer.get('foo')
82
+ end
83
+ end
84
+ end
85
+
86
+ context "class methods with unexistent file" do
87
+ setup do
88
+ Drawer.remove(cache_file)
89
+ end
90
+
91
+ context "open" do
92
+ should "raise if the file doesn't exist" do
93
+ assert_raise Errno::ENOENT do
94
+ Drawer.open(cache_file)
95
+ end
96
+ end
97
+
98
+ should "not create the file" do
99
+ Drawer.open(cache_file) rescue Errno::ENOENT
100
+ assert_equal false, File.exists?(cache_file)
101
+ end
102
+ end
103
+
104
+ context "open!" do
105
+ should "create the file" do
106
+ Drawer.open!(cache_file)
107
+ assert_equal true, File.exists?(cache_file)
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: drawer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.7
5
+ platform: ruby
6
+ authors:
7
+ - Michel Martens
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-05 00:00:00 -02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: michel@soveran.com
18
+ executables:
19
+ - drawer
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/drawer/console.rb
26
+ - lib/drawer/irb.rb
27
+ - lib/drawer.rb
28
+ - README.rdoc
29
+ - LICENSE
30
+ - Rakefile
31
+ - example/db/development.yml
32
+ - example/drawer_sample.rb
33
+ - example/drawer_sample_gem.rb
34
+ - test/db/test.yml
35
+ - test/drawer_test.rb
36
+ has_rdoc: false
37
+ homepage: http://github.com/soveran/drawer
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.3.1
59
+ signing_key:
60
+ specification_version: 2
61
+ summary: Ultra light file-based cache.
62
+ test_files: []
63
+