conflict_detector 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 +2 -0
- data/Gemfile +9 -0
- data/LICENSE +22 -0
- data/README.md +22 -0
- data/Rakefile +7 -0
- data/conflict_detector.gemspec +16 -0
- data/lib/conflict_detector.rb +53 -0
- data/lib/conflict_detector/version.rb +3 -0
- data/spec/conflict_detector_spec.rb +109 -0
- metadata +67 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Ennova
|
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,22 @@
|
|
1
|
+
# Conflict Detector
|
2
|
+
|
3
|
+
ConflictDetector is a convenience class for saving the record, or returning the conflict.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
``` ruby
|
10
|
+
gem 'conflict_detector'
|
11
|
+
```
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
``` ruby
|
16
|
+
def create
|
17
|
+
@post = Post.build(params[:post])
|
18
|
+
detector = ConflictDetector.new(@post, :title, :content, :user_id)
|
19
|
+
@post = detector.conflicting_or_created_record
|
20
|
+
respond_with @post, :status => detector.status
|
21
|
+
end
|
22
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/conflict_detector/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ['Tate Johnson', 'Odin Dutton', 'Jason Weathered']
|
6
|
+
gem.email = ['tate@tatey.com', 'odindutton@gmail.com', 'jason@jasoncodes.com']
|
7
|
+
gem.summary = %q{ConflictDetector is a convenience class for saving the record, or returning the conflict.}
|
8
|
+
gem.description = gem.summary
|
9
|
+
gem.homepage = 'https://github.com/ennova/conflict_detector'
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.test_files = gem.files.grep(%r{^spec/})
|
13
|
+
gem.name = 'conflict_detector'
|
14
|
+
gem.require_paths = ['lib']
|
15
|
+
gem.version = ConflictDetector::VERSION
|
16
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'conflict_detector/version'
|
2
|
+
|
3
|
+
class ConflictDetector
|
4
|
+
attr_accessor :model
|
5
|
+
attr_accessor :names
|
6
|
+
attr_accessor :status
|
7
|
+
|
8
|
+
def initialize model, *names
|
9
|
+
@model = model
|
10
|
+
@names = names
|
11
|
+
end
|
12
|
+
|
13
|
+
# Checks if the model is a duplicate. Duplication is determined by finding
|
14
|
+
# a record that matches the values of the attribute +names+.
|
15
|
+
#
|
16
|
+
# Returns the conflicting model if it is found, otherwise nil.
|
17
|
+
def conflict
|
18
|
+
record = model.class.where(conditions).first
|
19
|
+
if record
|
20
|
+
conflict!
|
21
|
+
record
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def create
|
26
|
+
created!
|
27
|
+
model.save
|
28
|
+
model
|
29
|
+
end
|
30
|
+
|
31
|
+
# Convenience method that attempts to save the model, detecting for a
|
32
|
+
# conflict first.
|
33
|
+
#
|
34
|
+
# Returns conflicting record if conflicting record is found, otherwise
|
35
|
+
# the created record.
|
36
|
+
def conflicting_or_created_record
|
37
|
+
conflict || create
|
38
|
+
end
|
39
|
+
|
40
|
+
protected
|
41
|
+
|
42
|
+
def conditions
|
43
|
+
names.inject({}) { |conditions, name| conditions.merge name => model.public_send(name) }
|
44
|
+
end
|
45
|
+
|
46
|
+
def conflict!
|
47
|
+
@status = :conflict
|
48
|
+
end
|
49
|
+
|
50
|
+
def created!
|
51
|
+
@status = :created
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'conflict_detector'
|
2
|
+
|
3
|
+
describe ConflictDetector do
|
4
|
+
describe 'conflict' do
|
5
|
+
describe 'record does exist' do
|
6
|
+
let(:detector) { ConflictDetector.new(model, :description) }
|
7
|
+
let(:record) { Object.new }
|
8
|
+
let(:model) do
|
9
|
+
mock = mock 'Model'
|
10
|
+
mock.should_receive(:description).and_return('Hello, World!')
|
11
|
+
mock.should_receive(:class).and_return(mock)
|
12
|
+
mock.should_receive(:where).with(:description => 'Hello, World!').and_return(mock)
|
13
|
+
mock.should_receive(:first).and_return(record)
|
14
|
+
mock
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should be the record' do
|
18
|
+
detector.conflict.should equal(record)
|
19
|
+
end
|
20
|
+
|
21
|
+
describe 'status' do
|
22
|
+
before do
|
23
|
+
detector.conflict
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should be conflict' do
|
27
|
+
detector.status.should eq(:conflict)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe 'record does not exist' do
|
33
|
+
let(:detector) { ConflictDetector.new(model) }
|
34
|
+
let(:model) do
|
35
|
+
mock = mock 'Model'
|
36
|
+
mock.should_receive(:class).and_return(mock)
|
37
|
+
mock.should_receive(:where).and_return(mock)
|
38
|
+
mock.should_receive(:first).and_return(nil)
|
39
|
+
mock
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should be nil' do
|
43
|
+
detector.conflict.should be_nil
|
44
|
+
end
|
45
|
+
|
46
|
+
describe 'status' do
|
47
|
+
before do
|
48
|
+
detector.conflict
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should be nil' do
|
52
|
+
detector.status.should be_nil
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe 'create' do
|
59
|
+
let(:detector) { ConflictDetector.new(model) }
|
60
|
+
let(:model) do
|
61
|
+
mock = mock 'Model'
|
62
|
+
mock.should_receive(:save).and_return(true)
|
63
|
+
mock
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'should be the model' do
|
67
|
+
detector.create.should equal(model)
|
68
|
+
end
|
69
|
+
|
70
|
+
describe 'status' do
|
71
|
+
before do
|
72
|
+
detector.create
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'should be created' do
|
76
|
+
detector.status.should eq(:created)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe 'conflicting or creating record' do
|
82
|
+
describe 'conflict' do
|
83
|
+
let(:record) { Object.new }
|
84
|
+
let(:detector) do
|
85
|
+
detector = ConflictDetector.new(nil)
|
86
|
+
detector.should_receive(:conflict).and_return(record)
|
87
|
+
detector
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'should be the conflicting record' do
|
91
|
+
detector.conflicting_or_created_record.should equal(record)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe 'create' do
|
96
|
+
let(:record) { Object.new }
|
97
|
+
let(:detector) do
|
98
|
+
detector = ConflictDetector.new(nil)
|
99
|
+
detector.should_receive(:conflict).and_return(nil)
|
100
|
+
detector.should_receive(:create).and_return(record)
|
101
|
+
detector
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'should be the created record' do
|
105
|
+
detector.conflicting_or_created_record.should equal(record)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: conflict_detector
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tate Johnson
|
9
|
+
- Odin Dutton
|
10
|
+
- Jason Weathered
|
11
|
+
autorequire:
|
12
|
+
bindir: bin
|
13
|
+
cert_chain: []
|
14
|
+
date: 2012-09-04 00:00:00.000000000 Z
|
15
|
+
dependencies: []
|
16
|
+
description: ConflictDetector is a convenience class for saving the record, or returning
|
17
|
+
the conflict.
|
18
|
+
email:
|
19
|
+
- tate@tatey.com
|
20
|
+
- odindutton@gmail.com
|
21
|
+
- jason@jasoncodes.com
|
22
|
+
executables: []
|
23
|
+
extensions: []
|
24
|
+
extra_rdoc_files: []
|
25
|
+
files:
|
26
|
+
- .gitignore
|
27
|
+
- Gemfile
|
28
|
+
- LICENSE
|
29
|
+
- README.md
|
30
|
+
- Rakefile
|
31
|
+
- conflict_detector.gemspec
|
32
|
+
- lib/conflict_detector.rb
|
33
|
+
- lib/conflict_detector/version.rb
|
34
|
+
- spec/conflict_detector_spec.rb
|
35
|
+
homepage: https://github.com/ennova/conflict_detector
|
36
|
+
licenses: []
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
segments:
|
48
|
+
- 0
|
49
|
+
hash: -1871014682422888898
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
hash: -1871014682422888898
|
59
|
+
requirements: []
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.8.23
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: ConflictDetector is a convenience class for saving the record, or returning
|
65
|
+
the conflict.
|
66
|
+
test_files:
|
67
|
+
- spec/conflict_detector_spec.rb
|