mongoid_immutable_fields 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.txt +20 -0
- data/README.markdown +55 -0
- data/lib/mongoid_immutable_fields/immutable_fields.rb +28 -0
- data/lib/mongoid_immutable_fields/mongoid_filthy.rb +13 -0
- data/lib/mongoid_immutable_fields.rb +2 -0
- data/spec/immutable_fields_spec.rb +36 -0
- data/spec/spec_helper.rb +32 -0
- data/spec/support/dummy_document.rb +11 -0
- metadata +189 -0
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Jeff Bozek
|
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.markdown
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# Mongoid Immutable Fields
|
2
|
+
|
3
|
+
## Description
|
4
|
+
|
5
|
+
A simple gem that enforces immutability on selected fields.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Include the gem in your Gemfile
|
10
|
+
|
11
|
+
gem 'mongoid_immutable_fields'
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
Add this to your document
|
16
|
+
|
17
|
+
include Mongoid::ImmutableFields
|
18
|
+
|
19
|
+
then declare your immutable fields like so
|
20
|
+
|
21
|
+
immutable_fields :name, :reference_id
|
22
|
+
|
23
|
+
your document will look something like
|
24
|
+
|
25
|
+
class DummyDocument
|
26
|
+
include Mongoid::Document
|
27
|
+
include Mongoid::ImmutableFields
|
28
|
+
|
29
|
+
field :name
|
30
|
+
field :description
|
31
|
+
field :reference_id
|
32
|
+
|
33
|
+
immutable_fields :name, :reference_id
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
Now documents will not update if one of the immutable fields has changed value since initial persistence/creation. If a field is changed and attempted to be saved/updated the following error will be added to the document for the changed immutable field
|
38
|
+
|
39
|
+
is immutable and cannot be updated
|
40
|
+
|
41
|
+
## Contributing to mongoid_immutable_fields
|
42
|
+
|
43
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
44
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
45
|
+
* Fork the project
|
46
|
+
* Start a feature/bugfix branch
|
47
|
+
* Commit and push until you are happy with your contribution
|
48
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
49
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
50
|
+
|
51
|
+
## Copyright
|
52
|
+
|
53
|
+
Copyright (c) 2011 Jeff Bozek. See LICENSE.txt for
|
54
|
+
further details.
|
55
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Mongoid
|
2
|
+
|
3
|
+
module ImmutableFields
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
validate :check_immutability, :on => :update
|
8
|
+
class_attribute :immutable_field_names
|
9
|
+
delegate :immutable_field_names, :to => "self.class"
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
def immutable_fields(*field_names)
|
14
|
+
self.immutable_field_names = field_names
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
def check_immutability
|
20
|
+
changed_as_symbols.each do |field|
|
21
|
+
if immutable_field_names.include? field
|
22
|
+
errors.add( field, 'is immutable and cannot be updated' )
|
23
|
+
self.reset_attribute! field.to_s
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongoid::ImmutableFields do
|
4
|
+
|
5
|
+
describe 'when persisting a document with immutable fields' do
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
@dummy = DummyDocument.new(:name => 'Test', :description => 'Test document.', :reference_id => 'abc123')
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should be persisted normally' do
|
12
|
+
@dummy.save.should eq true
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should be persisted normally when immutable fields change prior to initial persistence' do
|
16
|
+
@dummy.name = 'Test2'
|
17
|
+
@dummy.save.should eq true
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
describe 'when updating a document with immutable fields' do
|
23
|
+
|
24
|
+
before(:each) do
|
25
|
+
@dummy = DummyDocument.create(:name => 'Test', :description => 'Test document.', :reference_id => 'abc123')
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should not allow a document to be persisted if an immutable field has changed' do
|
29
|
+
@dummy.name = 'New Test'
|
30
|
+
@dummy.save.should eq false
|
31
|
+
@dummy.errors[:name].include?("is immutable and cannot be updated").should eq true
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'mongoid'
|
2
|
+
require 'database_cleaner'
|
3
|
+
require 'mongoid_immutable_fields'
|
4
|
+
require 'rubygems'
|
5
|
+
|
6
|
+
Dir["./spec/support/**/*.rb"].each {|f| require f}
|
7
|
+
|
8
|
+
RSpec.configure do |config|
|
9
|
+
|
10
|
+
Mongoid.configure do |mconfig|
|
11
|
+
name = "monogid_immutable_fields_test"
|
12
|
+
host = "localhost"
|
13
|
+
port = 27017
|
14
|
+
mconfig.database = Mongo::Connection.new.db(name)
|
15
|
+
end
|
16
|
+
|
17
|
+
DatabaseCleaner.strategy = :truncation
|
18
|
+
|
19
|
+
config.before(:each) do
|
20
|
+
DatabaseCleaner.clean
|
21
|
+
end
|
22
|
+
|
23
|
+
# == Mock Framework
|
24
|
+
#
|
25
|
+
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
|
26
|
+
#
|
27
|
+
# config.mock_with :mocha
|
28
|
+
# config.mock_with :flexmock
|
29
|
+
# config.mock_with :rr
|
30
|
+
config.mock_with :rspec
|
31
|
+
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,189 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid_immutable_fields
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jeff Bozek
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-03-23 00:00:00 -05:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: bundler
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ~>
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.0.0
|
24
|
+
type: :development
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: jeweler
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 1.5.2
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: rspec
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
type: :development
|
47
|
+
prerelease: false
|
48
|
+
version_requirements: *id003
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: shoulda
|
51
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
type: :development
|
58
|
+
prerelease: false
|
59
|
+
version_requirements: *id004
|
60
|
+
- !ruby/object:Gem::Dependency
|
61
|
+
name: mongo
|
62
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ~>
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "1.1"
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: *id005
|
71
|
+
- !ruby/object:Gem::Dependency
|
72
|
+
name: mongo_ext
|
73
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: "0"
|
79
|
+
type: :development
|
80
|
+
prerelease: false
|
81
|
+
version_requirements: *id006
|
82
|
+
- !ruby/object:Gem::Dependency
|
83
|
+
name: mongoid
|
84
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - "="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 2.0.0.rc.7
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: *id007
|
93
|
+
- !ruby/object:Gem::Dependency
|
94
|
+
name: bson_ext
|
95
|
+
requirement: &id008 !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ~>
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: "1.2"
|
101
|
+
type: :development
|
102
|
+
prerelease: false
|
103
|
+
version_requirements: *id008
|
104
|
+
- !ruby/object:Gem::Dependency
|
105
|
+
name: database_cleaner
|
106
|
+
requirement: &id009 !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: "0"
|
112
|
+
type: :development
|
113
|
+
prerelease: false
|
114
|
+
version_requirements: *id009
|
115
|
+
- !ruby/object:Gem::Dependency
|
116
|
+
name: mongo
|
117
|
+
requirement: &id010 !ruby/object:Gem::Requirement
|
118
|
+
none: false
|
119
|
+
requirements:
|
120
|
+
- - ~>
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: "1.1"
|
123
|
+
type: :runtime
|
124
|
+
prerelease: false
|
125
|
+
version_requirements: *id010
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: mongoid
|
128
|
+
requirement: &id011 !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - "="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: 2.0.0.rc.7
|
134
|
+
type: :runtime
|
135
|
+
prerelease: false
|
136
|
+
version_requirements: *id011
|
137
|
+
description: Prevent fields from being updated by including a mongoid module and marking the fields as immutable.
|
138
|
+
email: jeff.bozek@gmail.com
|
139
|
+
executables: []
|
140
|
+
|
141
|
+
extensions: []
|
142
|
+
|
143
|
+
extra_rdoc_files:
|
144
|
+
- LICENSE.txt
|
145
|
+
- README.markdown
|
146
|
+
files:
|
147
|
+
- lib/mongoid_immutable_fields.rb
|
148
|
+
- lib/mongoid_immutable_fields/immutable_fields.rb
|
149
|
+
- lib/mongoid_immutable_fields/mongoid_filthy.rb
|
150
|
+
- LICENSE.txt
|
151
|
+
- README.markdown
|
152
|
+
- spec/immutable_fields_spec.rb
|
153
|
+
- spec/spec_helper.rb
|
154
|
+
- spec/support/dummy_document.rb
|
155
|
+
has_rdoc: true
|
156
|
+
homepage: http://github.com/jeffbozek/mongoid_immutable_fields
|
157
|
+
licenses:
|
158
|
+
- MIT
|
159
|
+
post_install_message:
|
160
|
+
rdoc_options: []
|
161
|
+
|
162
|
+
require_paths:
|
163
|
+
- lib
|
164
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
165
|
+
none: false
|
166
|
+
requirements:
|
167
|
+
- - ">="
|
168
|
+
- !ruby/object:Gem::Version
|
169
|
+
hash: 1685392550182324666
|
170
|
+
segments:
|
171
|
+
- 0
|
172
|
+
version: "0"
|
173
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
174
|
+
none: false
|
175
|
+
requirements:
|
176
|
+
- - ">="
|
177
|
+
- !ruby/object:Gem::Version
|
178
|
+
version: "0"
|
179
|
+
requirements: []
|
180
|
+
|
181
|
+
rubyforge_project:
|
182
|
+
rubygems_version: 1.5.3
|
183
|
+
signing_key:
|
184
|
+
specification_version: 3
|
185
|
+
summary: A simple gem that allows for creating immutable fields on your Mongoid documents.
|
186
|
+
test_files:
|
187
|
+
- spec/immutable_fields_spec.rb
|
188
|
+
- spec/spec_helper.rb
|
189
|
+
- spec/support/dummy_document.rb
|