key_value 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source :rubygems
2
+
3
+ gem 'activerecord'
4
+
5
+ group :dev do # not development <-> would add unneeded development dependencies in gemspec
6
+ gem 'sqlite3'
7
+ gem 'rake'
8
+ gem 'rspec', '~>2'
9
+ gem 'jeweler'
10
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,43 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.0.7)
5
+ activesupport (= 3.0.7)
6
+ builder (~> 2.1.2)
7
+ i18n (~> 0.5.0)
8
+ activerecord (3.0.7)
9
+ activemodel (= 3.0.7)
10
+ activesupport (= 3.0.7)
11
+ arel (~> 2.0.2)
12
+ tzinfo (~> 0.3.23)
13
+ activesupport (3.0.7)
14
+ arel (2.0.9)
15
+ builder (2.1.2)
16
+ diff-lcs (1.1.2)
17
+ git (1.2.5)
18
+ i18n (0.5.0)
19
+ jeweler (1.6.0)
20
+ bundler (~> 1.0.0)
21
+ git (>= 1.2.5)
22
+ rake
23
+ rake (0.8.7)
24
+ rspec (2.5.0)
25
+ rspec-core (~> 2.5.0)
26
+ rspec-expectations (~> 2.5.0)
27
+ rspec-mocks (~> 2.5.0)
28
+ rspec-core (2.5.1)
29
+ rspec-expectations (2.5.0)
30
+ diff-lcs (~> 1.1.2)
31
+ rspec-mocks (2.5.0)
32
+ sqlite3 (1.3.3)
33
+ tzinfo (0.3.27)
34
+
35
+ PLATFORMS
36
+ ruby
37
+
38
+ DEPENDENCIES
39
+ activerecord
40
+ jeweler
41
+ rake
42
+ rspec (~> 2)
43
+ sqlite3
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ task :default do
2
+ sh "rspec spec/"
3
+ end
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |gem|
8
+ gem.name = 'key_value'
9
+ gem.summary = "Abuse Sql database as Key-Value store"
10
+ gem.email = "michael@grosser.it"
11
+ gem.homepage = "http://github.com/grosser/#{gem.name}"
12
+ gem.authors = ["Roman Heinrich","Michael Grosser"]
13
+ end
14
+
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler, or one of its dependencies, is not available. Install it with: gem install jeweler"
18
+ end
data/Readme.md ADDED
@@ -0,0 +1,35 @@
1
+ Abuse Sql database as Key-Value Store
2
+
3
+ Install
4
+ =======
5
+ sudo gem install key_value
6
+
7
+ Migration
8
+ =========
9
+ `rails g migration create_key_value`
10
+
11
+ class CreateKeyValue < ActiveRecord::Migration
12
+ def self.up
13
+ create_table :key_values do |t|
14
+ t.string :key, :null => false
15
+ t.text :value, :null => false
16
+ end
17
+ add_index :key_values, :key, :unique => true
18
+ end
19
+
20
+ def self.down
21
+ drop_table :key_values
22
+ end
23
+ end
24
+
25
+ Usage
26
+ =====
27
+ KeyValue.set('xxx', {:baz=>'foo'})
28
+ KeyValue.get('xxx') -> {:baz=>'foo'}
29
+ KeyValue.del('xxx')
30
+
31
+ Authors
32
+ =======
33
+ [Roman Heinrich](https://github.com/mindreframer)<br/>
34
+ [Michael Grosser](http://grosser.it)<br/>
35
+ Hereby placed under public domain, do what you want, just do not hold anyone accountable...
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/key_value.gemspec ADDED
@@ -0,0 +1,42 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{key_value}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Roman Heinrich", "Michael Grosser"]
12
+ s.date = %q{2011-05-03}
13
+ s.email = %q{michael@grosser.it}
14
+ s.files = [
15
+ "Gemfile",
16
+ "Gemfile.lock",
17
+ "Rakefile",
18
+ "Readme.md",
19
+ "VERSION",
20
+ "key_value.gemspec",
21
+ "lib/key_value.rb",
22
+ "spec/key_value_spec.rb",
23
+ "spec/spec_helper.rb"
24
+ ]
25
+ s.homepage = %q{http://github.com/grosser/key_value}
26
+ s.require_paths = ["lib"]
27
+ s.rubygems_version = %q{1.6.2}
28
+ s.summary = %q{Abuse Sql database as Key-Value store}
29
+
30
+ if s.respond_to? :specification_version then
31
+ s.specification_version = 3
32
+
33
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
34
+ s.add_runtime_dependency(%q<activerecord>, [">= 0"])
35
+ else
36
+ s.add_dependency(%q<activerecord>, [">= 0"])
37
+ end
38
+ else
39
+ s.add_dependency(%q<activerecord>, [">= 0"])
40
+ end
41
+ end
42
+
data/lib/key_value.rb ADDED
@@ -0,0 +1,26 @@
1
+ require 'active_record'
2
+
3
+ class KeyValue < ActiveRecord::Base
4
+ VERSION = File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip
5
+ validates_presence_of :key, :value
6
+
7
+ serialize :value
8
+
9
+ def self.get(key)
10
+ KeyValue.find_by_key(key).try(:value)
11
+ end
12
+
13
+ def self.set(key, value)
14
+ if value
15
+ record = KeyValue.find_by_key(key) || KeyValue.new(:key => key)
16
+ record.value = value
17
+ record.save!
18
+ else
19
+ KeyValue.delete_all(:key => key)
20
+ end
21
+ end
22
+
23
+ def self.del(key)
24
+ set(key, nil)
25
+ end
26
+ end
@@ -0,0 +1,43 @@
1
+ require File.expand_path('spec/spec_helper')
2
+
3
+ describe KeyValue do
4
+ before do
5
+ KeyValue.delete_all
6
+ end
7
+
8
+ it "has a VERSION" do
9
+ KeyValue::VERSION.should =~ /^\d+\.\d+\.\d+$/
10
+ end
11
+
12
+ it "can get empty" do
13
+ KeyValue.get('xxx').should == nil
14
+ end
15
+
16
+ it "can set & get" do
17
+ KeyValue.set('xxx', 1)
18
+ KeyValue.get('xxx').should == 1
19
+ end
20
+
21
+ it "overwrites on set" do
22
+ KeyValue.set('xxx', 1)
23
+ KeyValue.set('xxx', 2)
24
+ KeyValue.get('xxx').should == 2
25
+ end
26
+
27
+ it "can unset" do
28
+ KeyValue.set('yyy', 1)
29
+ KeyValue.set('xxx', 1)
30
+ lambda{
31
+ KeyValue.set('xxx', nil)
32
+ }.should change{KeyValue.count}.by(-1)
33
+ KeyValue.get('xxx').should == nil
34
+ end
35
+
36
+ it "can del" do
37
+ KeyValue.set('xxx', 1)
38
+ lambda{
39
+ KeyValue.del('xxx')
40
+ }.should change{KeyValue.count}.by(-1)
41
+ KeyValue.get('xxx').should == nil
42
+ end
43
+ end
@@ -0,0 +1,19 @@
1
+ require 'active_record'
2
+
3
+ # connect
4
+ ActiveRecord::Base.establish_connection(
5
+ :adapter => "sqlite3",
6
+ :database => ":memory:"
7
+ )
8
+
9
+ # create tables
10
+ ActiveRecord::Schema.define(:version => 1) do
11
+ create_table :key_values do |t|
12
+ t.string :key, :null => false
13
+ t.text :value, :null => false
14
+ end
15
+ add_index :key_values, :key, :unique => true
16
+ end
17
+
18
+ $LOAD_PATH.unshift 'lib'
19
+ require 'key_value'
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: key_value
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Roman Heinrich
14
+ - Michael Grosser
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-05-03 00:00:00 +02:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ version_requirements: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ requirement: *id001
33
+ prerelease: false
34
+ type: :runtime
35
+ name: activerecord
36
+ description:
37
+ email: michael@grosser.it
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - Gemfile
46
+ - Gemfile.lock
47
+ - Rakefile
48
+ - Readme.md
49
+ - VERSION
50
+ - key_value.gemspec
51
+ - lib/key_value.rb
52
+ - spec/key_value_spec.rb
53
+ - spec/spec_helper.rb
54
+ has_rdoc: true
55
+ homepage: http://github.com/grosser/key_value
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options: []
60
+
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ requirements: []
82
+
83
+ rubyforge_project:
84
+ rubygems_version: 1.6.2
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Abuse Sql database as Key-Value store
88
+ test_files: []
89
+