soveran-drawer 0.0.2

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,38 @@
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
+ == Contributors
34
+
35
+ Michel Martens
36
+
37
+ Copyright (c) 2008 Michel Martens.
38
+ 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
@@ -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/lib/drawer.rb ADDED
@@ -0,0 +1,53 @@
1
+ require 'yaml'
2
+
3
+ class DrawerStore
4
+ def self.load(file)
5
+ YAML.load_file(file)
6
+ end
7
+
8
+ def self.save(object, file)
9
+ File.open(file, 'w') do |f|
10
+ YAML.dump(object, f)
11
+ end
12
+ end
13
+ end
14
+
15
+ class Drawer
16
+ attr :cache
17
+
18
+ def initialize(file, store = DrawerStore)
19
+ @cache = store.load(file)
20
+
21
+ at_exit do
22
+ store.save(@cache, file)
23
+ end
24
+ end
25
+
26
+ def get(k)
27
+ cache[k]
28
+ end
29
+
30
+ def get_multi(*ks)
31
+ ks.collect { |k| get(k) }
32
+ end
33
+
34
+ def set(k, v)
35
+ cache[k] = v
36
+ end
37
+
38
+ def add(k, v)
39
+ set(k, v) unless cache[k]
40
+ end
41
+
42
+ def remove(k)
43
+ cache.delete(k)
44
+ end
45
+
46
+ def flush_all
47
+ cache.clear
48
+ end
49
+
50
+ def inspect
51
+ "Drawer count: #{cache.size}. Type 'cache' to view the content."
52
+ end
53
+ end
data/test/db/test.yml ADDED
@@ -0,0 +1,2 @@
1
+ --- {}
2
+
@@ -0,0 +1,56 @@
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 = Drawer.new(cache_file)
15
+ @drawer.flush_all
16
+ end
17
+
18
+ should "return nil if a key is not found" do
19
+ assert_nil @drawer.get("foo")
20
+ end
21
+
22
+ should "successfully set a key" do
23
+ @drawer.set("foo", 123)
24
+ assert_equal 123, @drawer.get("foo")
25
+ end
26
+
27
+ should "clear an entry with remove" do
28
+ @drawer.set("foo", 123)
29
+ @drawer.remove("foo")
30
+ assert_nil @drawer.get("foo")
31
+ end
32
+
33
+ context "flush_all" do
34
+ setup do
35
+ @drawer.set("foo", 123)
36
+ @drawer.flush_all
37
+ end
38
+
39
+ should "clear all keys" do
40
+ assert_nil @drawer.get("foo")
41
+ end
42
+ end
43
+
44
+ context "get_multi" do
45
+ setup do
46
+ 10.times do |i|
47
+ @drawer.set(i, i * i)
48
+ end
49
+ end
50
+
51
+ should "return a list of values" do
52
+ assert_equal [9, 25, 49], @drawer.get_multi(3, 5, 7)
53
+ end
54
+ end
55
+ end
56
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: soveran-drawer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
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 -08: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
+ - README.rdoc
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: true
37
+ homepage: http://github.com/soveran/drawer
38
+ post_install_message:
39
+ rdoc_options:
40
+ - --line-numbers
41
+ - --inline-source
42
+ - --title
43
+ - drawer
44
+ - --main
45
+ - README.rdoc
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.2.0
64
+ signing_key:
65
+ specification_version: 2
66
+ summary: Ultra light cache.
67
+ test_files: []
68
+