mince 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/.rspec +1 -0
- data/.rvmrc +1 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +39 -0
- data/README.md +26 -0
- data/Rakefile +1 -0
- data/lib/mince/connection.rb +26 -0
- data/lib/mince/data_store.rb +71 -0
- data/lib/mince/version.rb +3 -0
- data/lib/mince.rb +1 -0
- data/mince.gemspec +27 -0
- data/spec/lib/connection_spec.rb +15 -0
- data/spec/lib/data_store_spec.rb +104 -0
- metadata +116 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
.idea
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 1.9.3@mince --create
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
mince (0.0.1)
|
5
|
+
activesupport (~> 3.0)
|
6
|
+
bson_ext (~> 1.5.2)
|
7
|
+
mongo (~> 1.5.2)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: http://rubygems.org/
|
11
|
+
specs:
|
12
|
+
activesupport (3.2.2)
|
13
|
+
i18n (~> 0.6)
|
14
|
+
multi_json (~> 1.0)
|
15
|
+
bson (1.5.2)
|
16
|
+
bson_ext (1.5.2)
|
17
|
+
bson (= 1.5.2)
|
18
|
+
diff-lcs (1.1.3)
|
19
|
+
i18n (0.6.0)
|
20
|
+
mongo (1.5.2)
|
21
|
+
bson (= 1.5.2)
|
22
|
+
multi_json (1.1.0)
|
23
|
+
rake (0.8.7)
|
24
|
+
rspec (2.8.0.rc1)
|
25
|
+
rspec-core (= 2.8.0.rc1)
|
26
|
+
rspec-expectations (= 2.8.0.rc1)
|
27
|
+
rspec-mocks (= 2.8.0.rc1)
|
28
|
+
rspec-core (2.8.0.rc1)
|
29
|
+
rspec-expectations (2.8.0.rc1)
|
30
|
+
diff-lcs (~> 1.1.2)
|
31
|
+
rspec-mocks (2.8.0.rc1)
|
32
|
+
|
33
|
+
PLATFORMS
|
34
|
+
ruby
|
35
|
+
|
36
|
+
DEPENDENCIES
|
37
|
+
mince!
|
38
|
+
rake
|
39
|
+
rspec
|
data/README.md
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# What is this?
|
2
|
+
|
3
|
+
Provides a very lightweight interface to store and retrieve data in MongoDB in Ruby.
|
4
|
+
|
5
|
+
# Why would you want this?
|
6
|
+
|
7
|
+
- To build a non-active-record implementation application using MongoDB.
|
8
|
+
- Allows interchanging data persistence with HashyDb
|
9
|
+
|
10
|
+
# Todos
|
11
|
+
|
12
|
+
- Make an example rails app that utilizes this gem
|
13
|
+
- Add a better readme
|
14
|
+
|
15
|
+
# Contribute
|
16
|
+
|
17
|
+
- fork into a topic branch, write specs, make a pull request.
|
18
|
+
|
19
|
+
# Owners
|
20
|
+
|
21
|
+
Matt Simpson - [@railsgrammer](https://twitter.com/railsgrammer)
|
22
|
+
<br />
|
23
|
+
Jason Mayer - [@farkerhaiku](https://twitter.com/farkerhaiku)
|
24
|
+
|
25
|
+
# Contributors
|
26
|
+
- Your name here!
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'mongo'
|
2
|
+
require 'singleton'
|
3
|
+
require 'active_support/core_ext/object/blank'
|
4
|
+
|
5
|
+
module Mince
|
6
|
+
class Connection
|
7
|
+
include Singleton
|
8
|
+
|
9
|
+
attr_reader :connection, :db
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@connection = Mongo::Connection.new
|
13
|
+
@db = connection.db(database)
|
14
|
+
end
|
15
|
+
|
16
|
+
# todo: push this into a config class which uses yaml or something like that to pull in the different environment settings?
|
17
|
+
def database
|
18
|
+
env_suffix = if (ENV['TEST_ENV_NUMBER'].blank?)
|
19
|
+
""
|
20
|
+
else
|
21
|
+
"-#{ENV['TEST_ENV_NUMBER']}"
|
22
|
+
end
|
23
|
+
"mince#{env_suffix}"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
|
3
|
+
require_relative 'connection'
|
4
|
+
|
5
|
+
module Mince
|
6
|
+
class DataStore
|
7
|
+
include Singleton
|
8
|
+
|
9
|
+
def self.primary_key_identifier
|
10
|
+
'_id'
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.generate_unique_id(*args)
|
14
|
+
BSON::ObjectId.new.to_s
|
15
|
+
end
|
16
|
+
|
17
|
+
def add(collection_name, hash)
|
18
|
+
collection(collection_name).insert(hash)
|
19
|
+
end
|
20
|
+
|
21
|
+
def replace(collection_name, hash)
|
22
|
+
collection(collection_name).update({"_id" => hash[:_id]}, hash)
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_all_for_key_with_value(collection_name, key, value)
|
26
|
+
get_by_params(collection_name, key.to_s => value)
|
27
|
+
end
|
28
|
+
|
29
|
+
def get_by_params(collection_name, hash)
|
30
|
+
collection(collection_name).find(hash)
|
31
|
+
end
|
32
|
+
|
33
|
+
def find_all(collection_name)
|
34
|
+
collection(collection_name).find
|
35
|
+
end
|
36
|
+
|
37
|
+
def find(collection_name, key, value)
|
38
|
+
collection(collection_name).find_one({key.to_s => value})
|
39
|
+
end
|
40
|
+
|
41
|
+
def push_to_array(collection_name, identifying_key, identifying_value, array_key, value_to_push)
|
42
|
+
collection(collection_name).update({identifying_key.to_s => identifying_value}, {'$push' => {array_key.to_s => value_to_push}})
|
43
|
+
end
|
44
|
+
|
45
|
+
def remove_from_array(collection_name, identifying_key, identifying_value, array_key, value_to_remove)
|
46
|
+
collection(collection_name).update({identifying_key.to_s => identifying_value}, {'$pull' => {array_key.to_s => value_to_remove}})
|
47
|
+
end
|
48
|
+
|
49
|
+
def containing_any(collection_name, key, values)
|
50
|
+
collection(collection_name).find({key.to_s => {"$in" => values}})
|
51
|
+
end
|
52
|
+
|
53
|
+
def array_contains(collection_name, key, value)
|
54
|
+
collection(collection_name).find(key.to_s => value)
|
55
|
+
end
|
56
|
+
|
57
|
+
def clear
|
58
|
+
db.collection_names.each do |collection_name|
|
59
|
+
db.drop_collection collection_name unless collection_name.include?('system')
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def collection(collection_name)
|
64
|
+
db.collection(collection_name)
|
65
|
+
end
|
66
|
+
|
67
|
+
def db
|
68
|
+
Mince::Connection.instance.db
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/lib/mince.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'mince/data_store'
|
data/mince.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require 'mince/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "mince"
|
7
|
+
s.version = Mince::VERSION
|
8
|
+
s.authors = ["Matt Simpson", "Jason Mayer"]
|
9
|
+
s.email = ["matt@railsgrammer.com", "jason.mayer@gmail.com"]
|
10
|
+
s.homepage = "https://github.com/coffeencoke/mince"
|
11
|
+
s.summary = %q{Lightweight MongoDB ORM for Ruby.}
|
12
|
+
s.description = %q{Lightweight MongoDB ORM for Ruby.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "mince"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency('activesupport', '~> 3.0')
|
22
|
+
s.add_dependency('mongo', '~> 1.5.2')
|
23
|
+
s.add_dependency('bson_ext', '~> 1.5.2')
|
24
|
+
|
25
|
+
s.add_development_dependency('rake')
|
26
|
+
s.add_development_dependency('rspec')
|
27
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require_relative '../../lib/mince/connection'
|
2
|
+
|
3
|
+
describe Mince::Connection do
|
4
|
+
subject { described_class.instance }
|
5
|
+
|
6
|
+
let(:mongo_connection) { mock 'a mongo connection object', :db => db }
|
7
|
+
let(:db) { mock 'db'}
|
8
|
+
|
9
|
+
it 'is a mongo connection' do
|
10
|
+
Mongo::Connection.should_receive(:new).and_return(mongo_connection)
|
11
|
+
mongo_connection.should_receive(:db).with("mince").and_return(db)
|
12
|
+
|
13
|
+
subject.connection.should == mongo_connection
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require_relative '../../lib/mince/data_store'
|
2
|
+
|
3
|
+
describe Mince::DataStore do
|
4
|
+
subject { described_class.instance }
|
5
|
+
|
6
|
+
let(:db) { mock 'mongo database', collection: collection }
|
7
|
+
let(:connection) { mock 'mongo connection', db: db }
|
8
|
+
let(:mongo_data_store_connection) { mock 'mongo_data_store_connection', :db => db}
|
9
|
+
let(:collection) { mock 'some collection'}
|
10
|
+
let(:collection_name) { 'some_collection_name'}
|
11
|
+
let(:primary_key) { mock 'primary key'}
|
12
|
+
let(:mock_id) { mock 'id' }
|
13
|
+
let(:data) { { :_id => mock_id}}
|
14
|
+
let(:return_data) { mock 'return data' }
|
15
|
+
|
16
|
+
before do
|
17
|
+
Mince::Connection.stub(:instance => mongo_data_store_connection)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'uses the correct collection' do
|
21
|
+
db.stub(collection: collection)
|
22
|
+
subject.collection('collection name').should == collection
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'has a primary key identifier' do
|
26
|
+
described_class.primary_key_identifier.should == '_id'
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "Generating a primary key" do
|
30
|
+
let(:unique_id) { mock 'id' }
|
31
|
+
it 'should create a reasonably unique id' do
|
32
|
+
BSON::ObjectId.should_receive(:new).and_return(unique_id)
|
33
|
+
|
34
|
+
described_class.generate_unique_id('something').should == unique_id.to_s
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'can write to the collection' do
|
39
|
+
collection.should_receive(:insert).with(data).and_return(return_data)
|
40
|
+
|
41
|
+
subject.add(collection_name, data).should == return_data
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'can read from the collection' do
|
45
|
+
collection.should_receive(:find).and_return(return_data)
|
46
|
+
|
47
|
+
subject.find_all(collection_name).should == return_data
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'can replace a record' do
|
51
|
+
collection.should_receive(:update).with({"_id" => data[:_id]}, data)
|
52
|
+
|
53
|
+
subject.replace(collection_name, data)
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'can get one document' do
|
57
|
+
field = "stuff"
|
58
|
+
value = "more stuff"
|
59
|
+
|
60
|
+
collection.should_receive(:find_one).with(field => value).and_return(return_data)
|
61
|
+
|
62
|
+
subject.find(collection_name, field, value).should == return_data
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'can clear the data store' do
|
66
|
+
collection_names = %w(collection_1 collection_2 system_profiles)
|
67
|
+
db.stub(:collection_names => collection_names)
|
68
|
+
|
69
|
+
db.should_receive(:drop_collection).with('collection_1')
|
70
|
+
db.should_receive(:drop_collection).with('collection_2')
|
71
|
+
|
72
|
+
subject.clear
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'can get all records of a specific key value' do
|
76
|
+
collection.should_receive(:find).with({"key" => "value"}).and_return(return_data)
|
77
|
+
|
78
|
+
subject.get_all_for_key_with_value(collection_name, "key", "value").should == return_data
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'can get all records where a value includes any of a set of values' do
|
82
|
+
collection.should_receive(:find).with({"key1" => { "$in" => [1,2,4]} }).and_return(return_data)
|
83
|
+
|
84
|
+
subject.containing_any(collection_name, "key1", [1,2,4]).should == return_data
|
85
|
+
end
|
86
|
+
|
87
|
+
it 'can get all records where the array includes a value' do
|
88
|
+
collection.should_receive(:find).with({"key" => "value"}).and_return(return_data)
|
89
|
+
|
90
|
+
subject.array_contains(collection_name, "key", "value").should == return_data
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'can push a value to an array for a specific record' do
|
94
|
+
collection.should_receive(:update).with({"key" => "value"}, { '$push' => { "array_key" => "value_to_push"}}).and_return(return_data)
|
95
|
+
|
96
|
+
subject.push_to_array(collection_name, :key, "value", :array_key, "value_to_push").should == return_data
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'can remove a value from an array for a specific record' do
|
100
|
+
collection.should_receive(:update).with({"key" => "value"}, { '$pull' => { "array_key" => "value_to_remove"}}).and_return(return_data)
|
101
|
+
|
102
|
+
subject.remove_from_array(collection_name, :key, "value", :array_key, "value_to_remove").should == return_data
|
103
|
+
end
|
104
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mince
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Matt Simpson
|
9
|
+
- Jason Mayer
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-03-02 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activesupport
|
17
|
+
requirement: &17984140 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ~>
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '3.0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *17984140
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: mongo
|
28
|
+
requirement: &17983380 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.5.2
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *17983380
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: bson_ext
|
39
|
+
requirement: &18417860 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: 1.5.2
|
45
|
+
type: :runtime
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *18417860
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: rake
|
50
|
+
requirement: &18417480 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
type: :development
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: *18417480
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
name: rspec
|
61
|
+
requirement: &18416960 !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
type: :development
|
68
|
+
prerelease: false
|
69
|
+
version_requirements: *18416960
|
70
|
+
description: Lightweight MongoDB ORM for Ruby.
|
71
|
+
email:
|
72
|
+
- matt@railsgrammer.com
|
73
|
+
- jason.mayer@gmail.com
|
74
|
+
executables: []
|
75
|
+
extensions: []
|
76
|
+
extra_rdoc_files: []
|
77
|
+
files:
|
78
|
+
- .gitignore
|
79
|
+
- .rspec
|
80
|
+
- .rvmrc
|
81
|
+
- Gemfile
|
82
|
+
- Gemfile.lock
|
83
|
+
- README.md
|
84
|
+
- Rakefile
|
85
|
+
- lib/mince.rb
|
86
|
+
- lib/mince/connection.rb
|
87
|
+
- lib/mince/data_store.rb
|
88
|
+
- lib/mince/version.rb
|
89
|
+
- mince.gemspec
|
90
|
+
- spec/lib/connection_spec.rb
|
91
|
+
- spec/lib/data_store_spec.rb
|
92
|
+
homepage: https://github.com/coffeencoke/mince
|
93
|
+
licenses: []
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ! '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
requirements: []
|
111
|
+
rubyforge_project: mince
|
112
|
+
rubygems_version: 1.8.10
|
113
|
+
signing_key:
|
114
|
+
specification_version: 3
|
115
|
+
summary: Lightweight MongoDB ORM for Ruby.
|
116
|
+
test_files: []
|