riak-shim 0.0.1
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/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +31 -0
- data/Rakefile +2 -0
- data/lib/riak-shim/core_ext.rb +12 -0
- data/lib/riak-shim/persistable.rb +72 -0
- data/lib/riak-shim/store.rb +39 -0
- data/lib/riak-shim/version.rb +5 -0
- data/lib/riak-shim.rb +10 -0
- data/riak-shim.gemspec +23 -0
- data/spec/persistable_spec.rb +33 -0
- data/spec/spec_helper.rb +5 -0
- data/spec/store_spec.rb +51 -0
- data/spec/support/database.yml +9 -0
- metadata +138 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Michael Brodhead & Ines Sombra
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# Riak::Shim
|
2
|
+
|
3
|
+
A teeny shim between your code and the riak-client gem. Reads database configuration
|
4
|
+
out of config/database.yml and derives bucket names from your class names and an
|
5
|
+
appropriate prefix.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'riak-shim'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install riak-shim
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
## taken from active support, but don't want all the baggage...
|
2
|
+
## converts camel case to all lower case name delimited by underscores
|
3
|
+
## for bucket name prettyness
|
4
|
+
class String
|
5
|
+
def underscore
|
6
|
+
self.gsub(/::/, '/').
|
7
|
+
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
8
|
+
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
9
|
+
tr("-", "_").
|
10
|
+
downcase
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module Riak
|
2
|
+
module Shim
|
3
|
+
|
4
|
+
module Persistable
|
5
|
+
attr_writer :store
|
6
|
+
|
7
|
+
def bucket
|
8
|
+
return self.class.store.bucket(bucket_name)
|
9
|
+
end
|
10
|
+
|
11
|
+
def bucket_name
|
12
|
+
self.class.bucket_name
|
13
|
+
end
|
14
|
+
|
15
|
+
def store
|
16
|
+
self.class.store
|
17
|
+
end
|
18
|
+
|
19
|
+
def save
|
20
|
+
doc = bucket.get_or_new(key)
|
21
|
+
doc.data = to_hash
|
22
|
+
set_indexes(doc.indexes)
|
23
|
+
doc.store
|
24
|
+
self
|
25
|
+
end
|
26
|
+
|
27
|
+
def set_indexes(indexes)
|
28
|
+
indexes.clear
|
29
|
+
fields_to_index.each do |field|
|
30
|
+
index_name = "#{field}_bin"
|
31
|
+
index_value = send(field)
|
32
|
+
indexes[index_name] << index_value
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
module ClassMethods
|
37
|
+
def delete_all
|
38
|
+
bucket.keys.each do |key|
|
39
|
+
bucket.delete key
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def store
|
44
|
+
@store ||= Store.new
|
45
|
+
end
|
46
|
+
|
47
|
+
def bucket_name
|
48
|
+
myclass = self.to_s.underscore
|
49
|
+
"#{store.bucket_prefix}#{myclass}"
|
50
|
+
end
|
51
|
+
|
52
|
+
def bucket
|
53
|
+
return store.bucket(bucket_name)
|
54
|
+
end
|
55
|
+
|
56
|
+
def for_index(index, value)
|
57
|
+
bucket.get_index(index, value).map do |key|
|
58
|
+
from_hash(key)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def gen_key
|
63
|
+
UUIDTools::UUID.random_create.to_s
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.included(base)
|
68
|
+
base.extend(ClassMethods)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'riak'
|
2
|
+
require 'uuidtools'
|
3
|
+
|
4
|
+
module Riak
|
5
|
+
module Shim
|
6
|
+
|
7
|
+
class Store
|
8
|
+
attr_writer :config_location
|
9
|
+
attr_reader :riak
|
10
|
+
|
11
|
+
def config
|
12
|
+
env = ENV['RACK_ENV'] || 'test'
|
13
|
+
@config ||= read_config[env]
|
14
|
+
end
|
15
|
+
|
16
|
+
def read_config
|
17
|
+
YAML.load_file(config_location)
|
18
|
+
end
|
19
|
+
|
20
|
+
def config_location
|
21
|
+
@config_location ||= 'config/database.yml'
|
22
|
+
end
|
23
|
+
|
24
|
+
def bucket_prefix
|
25
|
+
return config['bucket_prefix']
|
26
|
+
end
|
27
|
+
|
28
|
+
def riak
|
29
|
+
@riak ||= Riak::Client.new(:http_backend => :Excon,
|
30
|
+
:nodes => [{:host => config['host'], :http_port => config['http_port']}])
|
31
|
+
end
|
32
|
+
|
33
|
+
def bucket(name)
|
34
|
+
riak.bucket(name)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
data/lib/riak-shim.rb
ADDED
data/riak-shim.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/riak-shim/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Michael Brodhead & Shai Rosenfeld"]
|
6
|
+
gem.email = ["mkb@engineyard.com"]
|
7
|
+
gem.description = %q{Riak shim for bucket names and config.}
|
8
|
+
gem.summary = %q{A tiny shim between you and riak-client. Reads config/database.yml and generates sensible bucket names.}
|
9
|
+
gem.homepage = "https://github.com/mkb/riak-shim"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "riak-shim"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Riak::Shim::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency 'riak-client'
|
19
|
+
gem.add_dependency 'uuidtools'
|
20
|
+
|
21
|
+
gem.add_development_dependency "rake"
|
22
|
+
gem.add_development_dependency "rspec"
|
23
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'riak-shim/persistable'
|
3
|
+
|
4
|
+
class PersistableTester
|
5
|
+
include Riak::Shim::Persistable
|
6
|
+
end
|
7
|
+
|
8
|
+
describe 'persistable' do
|
9
|
+
let(:persistable) { PersistableTester.new }
|
10
|
+
|
11
|
+
before do
|
12
|
+
Riak::Shim::Store.any_instance.stub(:read_config).and_return(DB_CONFIG)
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#bucket_name' do
|
16
|
+
it 'composes #db_name and class name' do
|
17
|
+
persistable.bucket_name.should == 'test_persistable_tester'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '#bucket' do
|
22
|
+
it 'returns the correct bucket' do
|
23
|
+
persistable.bucket.name.should == 'test_persistable_tester'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe '#store' do
|
28
|
+
it 'returns a something that provides buckets' do
|
29
|
+
persistable.store.should respond_to(:bucket)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
data/spec/spec_helper.rb
ADDED
data/spec/store_spec.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'riak-shim/store'
|
3
|
+
|
4
|
+
describe Riak::Shim::Store do
|
5
|
+
let(:store) { Riak::Shim::Store.new }
|
6
|
+
|
7
|
+
|
8
|
+
describe '#config_location' do
|
9
|
+
it 'keeps a path of the database config file' do
|
10
|
+
path = store.config_location
|
11
|
+
path.should_not be_nil
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'with test config' do
|
16
|
+
before do
|
17
|
+
tmpdir = Dir.tmpdir
|
18
|
+
FileUtils.cp('./spec/support/database.yml', tmpdir)
|
19
|
+
store.stub(:config_location).and_return("#{tmpdir}/database.yml")
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '#read_config' do
|
23
|
+
it 'reads yaml from config_location' do
|
24
|
+
store.read_config.should == DB_CONFIG
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '#config' do
|
29
|
+
context 'with RACK_ENV=test' do
|
30
|
+
before do
|
31
|
+
@stashed_rack_env = ENV['RACK_ENV']
|
32
|
+
ENV['RACK_ENV'] = 'test'
|
33
|
+
end
|
34
|
+
|
35
|
+
after do
|
36
|
+
ENV['RACK_ENV'] = @stashed_rack_env
|
37
|
+
end
|
38
|
+
|
39
|
+
it "says prefix is test_" do
|
40
|
+
store.config['bucket_prefix'].should == 'test_'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe '#bucket_prefix' do
|
46
|
+
it 'uses the prefix from the config' do
|
47
|
+
store.bucket_prefix.should == 'test_'
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
metadata
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: riak-shim
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Michael Brodhead & Shai Rosenfeld
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-07-11 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: riak-client
|
22
|
+
prerelease: false
|
23
|
+
requirement: &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
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: uuidtools
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
version: "0"
|
46
|
+
type: :runtime
|
47
|
+
version_requirements: *id002
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: rake
|
50
|
+
prerelease: false
|
51
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
type: :development
|
61
|
+
version_requirements: *id003
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
prerelease: false
|
65
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
hash: 3
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
type: :development
|
75
|
+
version_requirements: *id004
|
76
|
+
description: Riak shim for bucket names and config.
|
77
|
+
email:
|
78
|
+
- mkb@engineyard.com
|
79
|
+
executables: []
|
80
|
+
|
81
|
+
extensions: []
|
82
|
+
|
83
|
+
extra_rdoc_files: []
|
84
|
+
|
85
|
+
files:
|
86
|
+
- .gitignore
|
87
|
+
- Gemfile
|
88
|
+
- LICENSE
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- lib/riak-shim.rb
|
92
|
+
- lib/riak-shim/core_ext.rb
|
93
|
+
- lib/riak-shim/persistable.rb
|
94
|
+
- lib/riak-shim/store.rb
|
95
|
+
- lib/riak-shim/version.rb
|
96
|
+
- riak-shim.gemspec
|
97
|
+
- spec/persistable_spec.rb
|
98
|
+
- spec/spec_helper.rb
|
99
|
+
- spec/store_spec.rb
|
100
|
+
- spec/support/database.yml
|
101
|
+
homepage: https://github.com/mkb/riak-shim
|
102
|
+
licenses: []
|
103
|
+
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
hash: 3
|
115
|
+
segments:
|
116
|
+
- 0
|
117
|
+
version: "0"
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
none: false
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
hash: 3
|
124
|
+
segments:
|
125
|
+
- 0
|
126
|
+
version: "0"
|
127
|
+
requirements: []
|
128
|
+
|
129
|
+
rubyforge_project:
|
130
|
+
rubygems_version: 1.8.24
|
131
|
+
signing_key:
|
132
|
+
specification_version: 3
|
133
|
+
summary: A tiny shim between you and riak-client. Reads config/database.yml and generates sensible bucket names.
|
134
|
+
test_files:
|
135
|
+
- spec/persistable_spec.rb
|
136
|
+
- spec/spec_helper.rb
|
137
|
+
- spec/store_spec.rb
|
138
|
+
- spec/support/database.yml
|