mongoid_signature 0.0.4

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 ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ spec/tmp
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --colour
2
+ --format nested
3
+ --backtrace
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source :rubygems
2
+
3
+ gemspec
4
+
5
+ gem 'bson_ext', :platforms => :mri
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Connected Bits, LLC
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,88 @@
1
+ Sign mongoid documents to prevent duplicates.
2
+
3
+ == Requirements
4
+
5
+ * mongoid (>= 2.0.0.rc7)
6
+
7
+ == Install
8
+
9
+ To install mongoid_signature, simply add it to your Gemfile:
10
+
11
+ gem 'mongoid_signature'
12
+
13
+ In order to get the latest development version of mongoid_signature:
14
+
15
+ gem 'mongoid_signature', :git => 'https://github.com/connectedbits/mongoid_signature.git'
16
+
17
+ And then:
18
+ bundle install
19
+
20
+ == Usage
21
+
22
+ Add the <tt>include Mongoid::Signature</tt> in the model:
23
+
24
+ class User
25
+ include Mongoid::Document
26
+ include Mongoid::Signature
27
+
28
+ field :name
29
+ field :email
30
+
31
+ references_many :posts
32
+ references_many :comments
33
+
34
+ sign_document :include => [:name]
35
+ end
36
+
37
+ class Post
38
+ include Mongoid::Document
39
+ include Mongoid::Timestamps
40
+ include Mongoid::Signature
41
+
42
+ field :title
43
+ field :body
44
+ field :published, :type => Boolean, :default => false
45
+
46
+ referenced_in :users
47
+ embeds_many :comments
48
+
49
+ sign_document :include => [:user, :title, :body]
50
+ end
51
+
52
+ class Comment
53
+ include Mongoid::Document
54
+ include Mongoid::Timestamps
55
+ include Mongoid::Signature
56
+
57
+ field :body
58
+
59
+ embedded_in :post
60
+ referenced_in :user
61
+
62
+ sign_document :include => [:user, :body]
63
+ end
64
+
65
+ === Signing
66
+
67
+ This will add a <tt>signature</tt> field to your document and provide additional signature_string and sign! methods:
68
+
69
+ >> @user = User.new(:name => 'Eric')
70
+ => #<User _id: 4d66a345bf354104bd000002, name: "Eric", signature: nil, email: nil>
71
+ >> @user.sign!
72
+ => "535d6fcc4483b2d02167d2d4bc05fdff5e34f6538ac6fa897c63b11d8417e815"
73
+
74
+ == Known issues
75
+
76
+ See https://github.com//mongoid_signature/issues
77
+
78
+ == Repository
79
+
80
+ See https://github.com/connectedbits/mongoid_signature and feel free to fork it!
81
+
82
+ == Contributors
83
+
84
+ See a list of all contributors at https://github.com/connected/mongoid_signature/contributors. Thanks a lot everyone!
85
+
86
+ == Copyright
87
+
88
+ Copyright (c) 2011 Connected Bits, LLC. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,33 @@
1
+ require "rubygems"
2
+ require "rake"
3
+ require "rake/rdoctask"
4
+ require "rspec"
5
+ require "rspec/core/rake_task"
6
+ require 'rake/gempackagetask'
7
+
8
+ gemspec = eval(File.read('mongoid_signature.gemspec'))
9
+ Rake::GemPackageTask.new(gemspec) do |pkg|
10
+ pkg.gem_spec = gemspec
11
+ end
12
+
13
+ desc "build the gem and release it to rubygems.org"
14
+ task :release => :gem do
15
+ sh "gem push pkg/mongoid_signature-#{gemspec.version}.gem"
16
+ end
17
+
18
+ desc 'Generate documentation for the mongoid_signature plugin.'
19
+ Rake::RDocTask.new(:rdoc) do |rdoc|
20
+ rdoc.rdoc_dir = 'rdoc'
21
+ rdoc.title = 'MongoidSignature'
22
+ rdoc.options << '--line-numbers' << '--inline-source'
23
+ rdoc.rdoc_files.include('README.rdoc')
24
+ rdoc.rdoc_files.include('lib/**/*.rb')
25
+ end
26
+
27
+ Rspec::Core::RakeTask.new('spec:unit') do |spec|
28
+ spec.pattern = "spec/**/*_spec.rb"
29
+ end
30
+
31
+ task :spec => ['spec:unit']
32
+
33
+ task :default => :spec
@@ -0,0 +1,64 @@
1
+ require 'digest/md5'
2
+
3
+ module Mongoid::Signature
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ class_attribute :sign_fields
8
+ delegate :sign_fields, :to => "self.class"
9
+ end
10
+
11
+ def signature_string
12
+ sig = ''
13
+ self.sign_fields.each do |field|
14
+ if self.respond_to?(field)
15
+ if value = self.send(field)
16
+ if value.respond_to?('signature_string')
17
+ value = value.signature_string
18
+ elsif value.respond_to?('to_s')
19
+ value = value.to_s
20
+ end
21
+ sig << value if !value.blank?
22
+ end
23
+ end
24
+ sig << ';'
25
+ end if self.sign_fields
26
+ sig
27
+ end
28
+
29
+ def sign!
30
+ sig = (Digest::MD5.new << self.signature_string).to_s
31
+ self.signature = sig if self.respond_to?(:signature)
32
+ end
33
+
34
+ def unique_signature?
35
+ match = self.class.where(signature: self.signature).first
36
+ if match.nil?
37
+ true
38
+ else
39
+ match._id == self._id && self.class.where(signature: self.signature).count < 2
40
+ end
41
+ end
42
+
43
+ def validate_unique_signature
44
+ if unique_signature?
45
+ true
46
+ else
47
+ errors.add(:base, "is a duplicate")
48
+ false
49
+ end
50
+ end
51
+
52
+ module ClassMethods
53
+ def sign_document(options = {})
54
+ self.sign_fields = options[:include]
55
+ if options[:save_signature]
56
+ class_eval <<-EOV
57
+ field :signature, :type => String, :required => true
58
+ index :signature
59
+ before_validation :sign!
60
+ EOV
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1 @@
1
+ require 'mongoid/signature'
@@ -0,0 +1,20 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "mongoid_signature"
3
+ s.version = "0.0.4"
4
+ s.platform = Gem::Platform::RUBY
5
+ s.authors = ["Connected Bits"]
6
+ s.email = ["info@connectedbits.com"]
7
+ s.homepage = "http://github.com/connectedbits/mongoid_signature"
8
+ s.summary = %q{Sign mongoid documents to prevent dupes}
9
+ s.description = %q{Makes it easy to sign Mongoid documents based on a subset of fields to prevent duplicate documents.}
10
+
11
+ s.rubyforge_project = "mongoid_signature"
12
+
13
+ s.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
+ s.require_paths = ["lib"]
17
+ s.add_runtime_dependency('mongoid', ['>= 2.0.0'])
18
+ s.add_development_dependency('rspec')
19
+ s.add_development_dependency('rake')
20
+ end
@@ -0,0 +1,12 @@
1
+ class Comment
2
+ include Mongoid::Document
3
+ include Mongoid::Timestamps
4
+ include Mongoid::Signature
5
+
6
+ field :body
7
+
8
+ embedded_in :post
9
+ referenced_in :user
10
+
11
+ sign_document :include => [:user, :body]
12
+ end
@@ -0,0 +1,14 @@
1
+ class Post
2
+ include Mongoid::Document
3
+ include Mongoid::Timestamps
4
+ include Mongoid::Signature
5
+
6
+ field :title
7
+ field :body
8
+ field :published, :type => Boolean, :default => false
9
+
10
+ referenced_in :users
11
+ embeds_many :comments
12
+
13
+ sign_document :include => [:user, :title, :body], :save_signature => true
14
+ end
@@ -0,0 +1,12 @@
1
+ class User
2
+ include Mongoid::Document
3
+ include Mongoid::Signature
4
+
5
+ field :name
6
+ field :email
7
+
8
+ references_many :posts
9
+ references_many :comments
10
+
11
+ sign_document :include => [:name]
12
+ end
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Signature do
4
+ before do
5
+ @user = User.new(:name => 'Eric')
6
+ @post = Post.new
7
+ end
8
+
9
+ describe "a class with save signature option" do
10
+ it "should have a signature field" do
11
+ @post.should respond_to(:signature)
12
+ end
13
+ end
14
+
15
+ describe "a class without save signature option" do
16
+ it "should not have a signature field" do
17
+ @user.should_not respond_to(:signature)
18
+ end
19
+ end
20
+
21
+ describe "two instances with the same signature fields" do
22
+ before do
23
+ @post1 = Post.new(:title => 'My Great Blog Post', :body => 'I was born...')
24
+ @post2 = Post.new(:title => 'My Great Blog Post', :body => 'I was born...')
25
+ @post1.sign!
26
+ @post2.sign!
27
+ end
28
+
29
+ it "should have the same signature" do
30
+ @post1.signature.should == @post2.signature
31
+ end
32
+
33
+ describe "with otherwise different data" do
34
+ before do
35
+ @post1.published = true
36
+ @post2.published = false
37
+ end
38
+
39
+ it "should both have the same signature" do
40
+ @post1.signature.should == @post2.signature
41
+ end
42
+ end
43
+ end
44
+
45
+ describe "two instances with different signature fields" do
46
+ before do
47
+ @post1 = Post.new(:title => 'My Great Blog Post', :body => 'I was born...')
48
+ @post2 = Post.new(:title => 'Been Busy!', :body => 'Sorry for not posting but...')
49
+ @post1.sign!
50
+ @post2.sign!
51
+ end
52
+
53
+ it "should not have the same signature" do
54
+ @post1.signature.should_not == @post2.signature
55
+ end
56
+ end
57
+
58
+ describe "an instance with a related document" do
59
+ before do
60
+ @post_from_user1 = Post.new(:title => 'My Great Blog Post', :body => 'I was born...', :user => User.new(:name => 'Eric'))
61
+ @post_from_user2 = Post.new(:title => 'My Great Blog Post', :body => 'I was born...', :user => User.new(:name => 'Dave'))
62
+ @post_from_user1.sign!
63
+ @post_from_user2.sign!
64
+ end
65
+
66
+ it "should not have the same signature" do
67
+ @post_from_user1.signature.should_not == @post_from_user2.signature
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,24 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.setup
4
+
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+
8
+ MODELS = File.join(File.dirname(__FILE__), "models")
9
+ $LOAD_PATH.unshift(MODELS)
10
+
11
+ require 'mongoid'
12
+ require 'mongoid_signature'
13
+ require 'rspec'
14
+
15
+ Mongoid.configure do |config|
16
+ name = "mongoid_signature_test"
17
+ host = "localhost"
18
+ config.master = Mongo::Connection.new.db(name)
19
+ end
20
+
21
+ Dir[ File.join(MODELS, "*.rb") ].sort.each { |file| require File.basename(file) }
22
+
23
+ RSpec.configure do |config|
24
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_signature
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Connected Bits
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mongoid
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 2.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Makes it easy to sign Mongoid documents based on a subset of fields to
63
+ prevent duplicate documents.
64
+ email:
65
+ - info@connectedbits.com
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .gitignore
71
+ - .rspec
72
+ - Gemfile
73
+ - MIT-LICENSE
74
+ - README.rdoc
75
+ - Rakefile
76
+ - lib/mongoid/signature.rb
77
+ - lib/mongoid_signature.rb
78
+ - mongoid_signature.gemspec
79
+ - spec/models/comment.rb
80
+ - spec/models/post.rb
81
+ - spec/models/user.rb
82
+ - spec/signature_spec.rb
83
+ - spec/spec_helper.rb
84
+ homepage: http://github.com/connectedbits/mongoid_signature
85
+ licenses: []
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ segments:
97
+ - 0
98
+ hash: 2726516450144486484
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ segments:
106
+ - 0
107
+ hash: 2726516450144486484
108
+ requirements: []
109
+ rubyforge_project: mongoid_signature
110
+ rubygems_version: 1.8.24
111
+ signing_key:
112
+ specification_version: 3
113
+ summary: Sign mongoid documents to prevent dupes
114
+ test_files:
115
+ - spec/models/comment.rb
116
+ - spec/models/post.rb
117
+ - spec/models/user.rb
118
+ - spec/signature_spec.rb
119
+ - spec/spec_helper.rb