mongo_trail 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/.rspec +2 -0
- data/Gemfile +19 -0
- data/Guardfile +6 -0
- data/LICENSE +22 -0
- data/README.md +37 -0
- data/Rakefile +6 -0
- data/lib/mongo_trail.rb +47 -0
- data/lib/mongo_trail/test/mongo_helper.rb +15 -0
- data/lib/mongo_trail/version.rb +3 -0
- data/mongo_trail.gemspec +18 -0
- data/spec/configuration_spec.rb +10 -0
- data/spec/helper.rb +11 -0
- data/spec/mongo_spec.rb +41 -0
- data/spec/mongo_trail_spec.rb +25 -0
- metadata +77 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in mongo_trail.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
group :development, :test do
|
7
|
+
gem "bson_ext", require: false
|
8
|
+
gem "guard-rspec"
|
9
|
+
gem "rake"
|
10
|
+
gem "rb-fsevent" # for guard
|
11
|
+
gem "rspec"
|
12
|
+
gem "ruby_gntp" # for guard
|
13
|
+
end
|
14
|
+
|
15
|
+
group :test do
|
16
|
+
gem "ruby-debug19", require: false
|
17
|
+
gem "simplecov", require: false
|
18
|
+
#em "plymouth" # invoke pry after rspec failures
|
19
|
+
end
|
data/Guardfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Mark Lanett
|
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,37 @@
|
|
1
|
+
# MongoTrail
|
2
|
+
|
3
|
+
Track audit trails in MongoDB.
|
4
|
+
|
5
|
+
A single document is created with an id given by you.
|
6
|
+
You can append simple records to it.
|
7
|
+
The document can be exported in a way that is easy to turn into CSV.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'mongo_trail'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install mongo_trail
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
MongoTrail.record_audit_trail 1, "author" => "Mark Lanett", "gem" => "MongoTrail"
|
26
|
+
MongoTrail.record_audit_trail 1, "author" => "Mark Lanett", "gem" => "RedisLock"
|
27
|
+
MongoTrail.export_audit_trail(1).should have(2).items
|
28
|
+
MongoTrail.export_audit_trail(1).first.should include( "author" => "Mark Lanett", "gem" => "MongoTrail" )
|
29
|
+
MongoTrail.export_audit_trail(1).last.should include( "author" => "Mark Lanett", "gem" => "RedisLock" )
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
1. Fork it
|
34
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
35
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
36
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
37
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/mongo_trail.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require "mongo_trail/version"
|
3
|
+
require "mongo"
|
4
|
+
|
5
|
+
module MongoTrail
|
6
|
+
|
7
|
+
module Configuration
|
8
|
+
attr :audit_trail_configuration
|
9
|
+
def configure(&block)
|
10
|
+
@audit_trail_configuration = yield
|
11
|
+
@audit_trail_configuration.ensure_index( "id", unique: true )
|
12
|
+
end
|
13
|
+
end # Configuration
|
14
|
+
|
15
|
+
# Document structure:
|
16
|
+
# => id Primary key (can't use _id due to limitations in upsert)
|
17
|
+
# => keys Array of Strings
|
18
|
+
# => data Array of Document
|
19
|
+
module AuditTrail
|
20
|
+
|
21
|
+
def record_audit_trail( id, data = {} )
|
22
|
+
bkey = { id: id }
|
23
|
+
keys = data.keys.sort
|
24
|
+
audit_trail_configuration.update( bkey, { "$set" => bkey, "$addToSet" => { keys: { "$each" => keys } }, "$push" => { data: data } }, upsert: true )
|
25
|
+
end
|
26
|
+
|
27
|
+
# If a block is given, writes an array to the block line by line
|
28
|
+
# Otherwise, returns an array.
|
29
|
+
def export_audit_trail( id, &block )
|
30
|
+
if ! block then
|
31
|
+
return [].tap { |result| export_audit_trail( id ) { |row| result << row } }
|
32
|
+
end
|
33
|
+
|
34
|
+
bkey = { id: id }
|
35
|
+
doc = audit_trail_configuration.find( bkey ).to_a.first
|
36
|
+
if doc then
|
37
|
+
keys = doc["keys"].sort
|
38
|
+
data = doc["data"]
|
39
|
+
data.each { |row| block.call( row ) }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end # AuditTrail
|
44
|
+
|
45
|
+
extend Configuration
|
46
|
+
extend AuditTrail
|
47
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
RSpec.configure do |config|
|
2
|
+
|
3
|
+
config.before( :each, :mongo => true ) do
|
4
|
+
MongoTrail.configure do
|
5
|
+
Mongo::Connection.new.db("test").collection("audit_trails").tap do |c|
|
6
|
+
c.db.drop_collection( c.name)
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
config.after( :all, :mongo => true ) do
|
12
|
+
MongoTrail.audit_trail_configuration.db.drop_collection( MongoTrail.audit_trail_configuration.name)
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
data/mongo_trail.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/mongo_trail/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Mark Lanett"]
|
6
|
+
gem.email = ["mark.lanett@gmail.com"]
|
7
|
+
gem.summary = %q{Track audit trails in MongoDB}
|
8
|
+
gem.homepage = ""
|
9
|
+
|
10
|
+
gem.files = `git ls-files`.split($\)
|
11
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
12
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
13
|
+
gem.name = "mongo_trail"
|
14
|
+
gem.require_paths = ["lib"]
|
15
|
+
gem.version = MongoTrail::VERSION
|
16
|
+
|
17
|
+
gem.add_dependency "mongo"
|
18
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require "helper"
|
2
|
+
|
3
|
+
describe MongoTrail::Configuration do
|
4
|
+
|
5
|
+
it "can be configured" do
|
6
|
+
MongoTrail.configure { Mongo::Connection.new.db("test").collection("audit_trails") }
|
7
|
+
MongoTrail.audit_trail_configuration.should be_an_instance_of(Mongo::Collection)
|
8
|
+
end
|
9
|
+
|
10
|
+
end
|
data/spec/helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require "bundler/setup" # set up gem paths
|
3
|
+
require "ruby-debug"
|
4
|
+
require "simplecov" # code coverage
|
5
|
+
SimpleCov.start # must be loaded before our own code
|
6
|
+
require "mongo_trail" # load this gem
|
7
|
+
require "mongo_trail/test/mongo_helper"
|
8
|
+
|
9
|
+
RSpec.configure do |spec|
|
10
|
+
#
|
11
|
+
end
|
data/spec/mongo_spec.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require "helper"
|
2
|
+
|
3
|
+
describe Mongo do
|
4
|
+
|
5
|
+
subject { Mongo::Connection.new.db("test").collection("test") }
|
6
|
+
before { subject.db.drop_collection( subject.name) }
|
7
|
+
after { subject.db.drop_collection( subject.name) }
|
8
|
+
|
9
|
+
it "can insert" do
|
10
|
+
subject.insert( { "a" => 1, "b" => 2 } )
|
11
|
+
subject.find.count.should == 1
|
12
|
+
end
|
13
|
+
|
14
|
+
it "can find" do
|
15
|
+
subject.insert( { "a" => 1, "b" => 2 } )
|
16
|
+
subject.insert( { "a" => 3, "b" => 4 } )
|
17
|
+
subject.find({ "a" => 3 }).first.should include({ "b" => 4 })
|
18
|
+
end
|
19
|
+
|
20
|
+
it "can update" do
|
21
|
+
subject.update( { id: 1 }, { id: 1, fields: [], changes: [] }, upsert: true )
|
22
|
+
subject.find({ id: 1 }).find.count.should == 1
|
23
|
+
|
24
|
+
# update can't mix modifiers and regular values, so must $set the keys
|
25
|
+
# update can't modify and set _id (as of 2.2)
|
26
|
+
subject.update( { id: 1 }, { "$set" => { id: 1 }, "$inc" => { magic: 1 }, "$addToSet" => { foo: "bar" } }, upsert: true, safe: true )
|
27
|
+
subject.update( { id: 1 }, { "$set" => { id: 1 }, "$inc" => { magic: 1 }, "$addToSet" => { foo: { "$each" => [ "zap", "zip" ] } } }, upsert: true )
|
28
|
+
subject.find({ id: 1 }).find.count.should == 1
|
29
|
+
subject.find({ id: 1 }).find.first.should include({ "foo" => [ "bar", "zap", "zip" ] })
|
30
|
+
end
|
31
|
+
|
32
|
+
it "can not upsert and set custom _id" do
|
33
|
+
expect { subject.update( { _id: 1 }, { "$set" => { _id: 1 } }, upsert: true, safe: true ) }.to raise_exception
|
34
|
+
expect { subject.update( { _id: 1 }, { "$iset" => { _id: 1 } }, upsert: true, safe: true ) }.to raise_exception
|
35
|
+
end
|
36
|
+
|
37
|
+
it "finds nothing as empty array" do
|
38
|
+
subject.find( foo: 1 ).to_a.should == []
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require "helper"
|
2
|
+
|
3
|
+
describe MongoTrail, mongo: true do
|
4
|
+
|
5
|
+
it "exports empty audit trails for unknown objects" do
|
6
|
+
MongoTrail.export_audit_trail(1).should have(0).items
|
7
|
+
end
|
8
|
+
|
9
|
+
it "can record and export audit trails" do
|
10
|
+
MongoTrail.record_audit_trail 1, "author" => "Mark Lanett", "gem" => "MongoTrail"
|
11
|
+
MongoTrail.record_audit_trail 1, "author" => "Mark Lanett", "gem" => "RedisLock"
|
12
|
+
MongoTrail.export_audit_trail(1).should have(2).items
|
13
|
+
MongoTrail.export_audit_trail(1).first.should include( "author" => "Mark Lanett", "gem" => "MongoTrail" )
|
14
|
+
MongoTrail.export_audit_trail(1).last.should include( "author" => "Mark Lanett", "gem" => "RedisLock" )
|
15
|
+
puts MongoTrail.audit_trail_configuration.find.to_a
|
16
|
+
end
|
17
|
+
|
18
|
+
it "normalizes data with all keys in use" do
|
19
|
+
MongoTrail.record_audit_trail 1, "author" => "Mark Lanett", "gem" => "MongoTrail"
|
20
|
+
MongoTrail.record_audit_trail 1, "author" => "Mark Lanett"
|
21
|
+
# expect all documents to have author and gem
|
22
|
+
MongoTrail.export_audit_trail(1).last.should include( "author" => "Mark Lanett", "gem" => nil )
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongo_trail
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease: !!null
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mark Lanett
|
9
|
+
autorequire: !!null
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-14 00:00:00.000000000 -07:00
|
13
|
+
default_executable: !!null
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: mongo
|
17
|
+
requirement: &70247499619260 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *70247499619260
|
26
|
+
description: !!null
|
27
|
+
email:
|
28
|
+
- mark.lanett@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- .gitignore
|
34
|
+
- .rspec
|
35
|
+
- Gemfile
|
36
|
+
- Guardfile
|
37
|
+
- LICENSE
|
38
|
+
- README.md
|
39
|
+
- Rakefile
|
40
|
+
- lib/mongo_trail.rb
|
41
|
+
- lib/mongo_trail/test/mongo_helper.rb
|
42
|
+
- lib/mongo_trail/version.rb
|
43
|
+
- mongo_trail.gemspec
|
44
|
+
- spec/configuration_spec.rb
|
45
|
+
- spec/helper.rb
|
46
|
+
- spec/mongo_spec.rb
|
47
|
+
- spec/mongo_trail_spec.rb
|
48
|
+
has_rdoc: true
|
49
|
+
homepage: ''
|
50
|
+
licenses: []
|
51
|
+
post_install_message: !!null
|
52
|
+
rdoc_options: []
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
requirements: []
|
68
|
+
rubyforge_project: !!null
|
69
|
+
rubygems_version: 1.5.0
|
70
|
+
signing_key: !!null
|
71
|
+
specification_version: 3
|
72
|
+
summary: Track audit trails in MongoDB
|
73
|
+
test_files:
|
74
|
+
- spec/configuration_spec.rb
|
75
|
+
- spec/helper.rb
|
76
|
+
- spec/mongo_spec.rb
|
77
|
+
- spec/mongo_trail_spec.rb
|