mongoid_touch 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- gem "mongoid", "~> 2.1.7"
3
+ gem "mongoid", "~> 2.1"
4
4
 
5
5
  group :development, :test do
6
6
  gem "rake"
data/Gemfile.lock CHANGED
@@ -1,11 +1,11 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- activemodel (3.0.9)
5
- activesupport (= 3.0.9)
4
+ activemodel (3.0.10)
5
+ activesupport (= 3.0.10)
6
6
  builder (~> 2.1.2)
7
7
  i18n (~> 0.5.0)
8
- activesupport (3.0.9)
8
+ activesupport (3.0.10)
9
9
  bson (1.3.1)
10
10
  bson_ext (1.3.1)
11
11
  builder (2.1.2)
@@ -19,7 +19,7 @@ GEM
19
19
  rake
20
20
  mongo (1.3.1)
21
21
  bson (>= 1.3.1)
22
- mongoid (2.1.7)
22
+ mongoid (2.1.9)
23
23
  activemodel (~> 3.0)
24
24
  mongo (~> 1.3)
25
25
  tzinfo (~> 0.3.22)
@@ -45,7 +45,7 @@ DEPENDENCIES
45
45
  bundler (~> 1.0.0)
46
46
  database_cleaner (~> 0.6.7)
47
47
  jeweler (~> 1.6.4)
48
- mongoid (~> 2.1.7)
48
+ mongoid (~> 2.1)
49
49
  mongoid-rspec (~> 1.4.4)
50
50
  rake
51
51
  rspec (~> 2.3.0)
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # mongoid_touch ![build status](http://travis-ci.org/asaaki/mongoid_touch.png)
2
+
3
+ A tiny mongoid extension to provide the `touch` method known from ActiveRecord to Mongoid::Document.
4
+
5
+ ## Install and Usage
6
+
7
+ Use Bundler/Gemfile, add `gem "mongoid_touch"` right after your added mongoid.
8
+
9
+ The method `.touch` can have an optional parameter for your custom Time based field, otherwise it will try to use the `updated_at` field (include Mongoid::Timestamps in your model).
10
+
11
+ Use `.touch!` if you want to get exceptions if touching fails.
12
+
13
+ Now you can use:
14
+
15
+ ```ruby
16
+ my_model_instance.touch
17
+ my_model_instance.touch! # will raise errors if touching fails.
18
+ ```
19
+
20
+ This will update the `updated_at` field if present.
21
+
22
+ ```ruby
23
+ my_model_instance.touch(:modified_at)
24
+ my_model_instance.touch(:modified_at) # will raise errors if touching fails.
25
+ ```
26
+
27
+ This will update the custom field `modified_at` if present.
28
+
29
+ ## Exceptions for `touch!`
30
+
31
+ If the corresponding field is not present, mongoid_touch will raise a `Mongoid::Errors::MissingField`.
32
+
33
+ If the object is frozen, an exception of `Mongoid::Errors::FrozenInstance` is thrown.
34
+
35
+ If the underlying `update_attribute` doesn't return true, `Mongoid::Errors::DocumentNotUpdated` is thrown (unless mongoid itself throws an error).
36
+
37
+ ## Contributing to `mongoid_touch`
38
+
39
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
40
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
41
+ * Fork the project
42
+ * Start a feature/bugfix branch
43
+ * Commit and push until you are happy with your contribution
44
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
45
+ * 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.
46
+
47
+ ## Copyright
48
+
49
+ Copyright (c) 2011 Christoph Grabo. See LICENSE.txt for
50
+ further details.
51
+
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.4
1
+ 0.1.0
@@ -0,0 +1,12 @@
1
+ en:
2
+ mongoid:
3
+ errors:
4
+ messages:
5
+ touch:
6
+ document_not_updated:
7
+ The document %{name} was not updated successfully.
8
+ frozen_instance:
9
+ The document %{name} is frozen and can't be updated.
10
+ missing_field:
11
+ The field %{name} is not present for %{instance}.
12
+
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+
3
+ module Mongoid
4
+ module Document
5
+ module Touch
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ end
10
+
11
+ module InstanceMethods
12
+
13
+ def touch( at_field = nil )
14
+ unless self.frozen?
15
+ to_touch = at_field || :updated_at
16
+ if self.fields.include? to_touch.to_s
17
+ return true if self.update_attribute(to_touch.to_sym, Time.now.utc)
18
+ false
19
+ else
20
+ return false
21
+ end
22
+ else
23
+ false
24
+ end
25
+ end
26
+
27
+ def touch!( at_field = nil, force_fail = false )
28
+ raise Errors::DocumentNotUpdated.new(self) if force_fail
29
+ unless self.frozen?
30
+ to_touch = at_field || :updated_at
31
+ if self.fields.include? to_touch.to_s
32
+ return true if self.update_attribute(to_touch.to_sym, Time.now.utc)
33
+ raise Errors::DocumentNotUpdated, self
34
+ else
35
+ raise Errors::MissingField.new(to_touch, self)
36
+ end
37
+ else
38
+ raise Errors::FrozenInstance, self
39
+ end
40
+ end
41
+
42
+ end
43
+
44
+ end
45
+ end
46
+ end
47
+
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+
3
+ module Mongoid
4
+ module Errors
5
+ class DocumentNotUpdated < MongoidError
6
+ def initialize(name)
7
+ super(
8
+ translate("touch.document_not_updated", { :name => name.to_s })
9
+ )
10
+ end
11
+ end
12
+ end
13
+ end
14
+
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+
3
+ module Mongoid
4
+ module Errors
5
+ class FrozenInstance < MongoidError
6
+ def initialize(name)
7
+ super(
8
+ translate("touch.frozen_instance", { :name => name.to_s })
9
+ )
10
+ end
11
+ end
12
+ end
13
+ end
14
+
@@ -3,9 +3,9 @@
3
3
  module Mongoid
4
4
  module Errors
5
5
  class MissingField < MongoidError
6
- def initialize(name)
6
+ def initialize(name, instance)
7
7
  super(
8
- translate("missing_field", { :name => name })
8
+ translate("touch.missing_field", { :name => name.to_s, :instance => instance.to_s })
9
9
  )
10
10
  end
11
11
  end
data/lib/mongoid_touch.rb CHANGED
@@ -1,18 +1,9 @@
1
1
  # encoding: utf-8
2
-
2
+ require "mongoid"
3
+ require "mongoid/errors/document_not_updated"
4
+ require "mongoid/errors/frozen_instance"
3
5
  require "mongoid/errors/missing_field"
6
+ require "mongoid/document/touch"
4
7
 
5
- module Mongoid
6
- module Document
7
- def touch( at_field = nil )
8
- to_touch = at_field || :updated_at
9
- if self.fields.include? to_touch.to_s
10
- self.update_attribute(to_touch.to_sym, Time.now.utc)
11
- else
12
- raise Errors::MissingField.new(to_touch.to_s)
13
- end
14
- end
15
- alias :touch! :touch
16
- end
17
- end
8
+ I18n.load_path << File.join(File.dirname(__FILE__), "config", "locales", "en.yml")
18
9
 
@@ -5,16 +5,16 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{mongoid_touch}
8
- s.version = "0.0.4"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = [%q{Christoph Grabo}]
12
- s.date = %q{2011-08-14}
12
+ s.date = %q{2011-08-25}
13
13
  s.description = %q{A tiny mongoid extension to provide the `touch` method known from ActiveRecord to Mongoid::Document.}
14
14
  s.email = %q{chris@dinarrr.com}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE.txt",
17
- "README.rdoc"
17
+ "README.md"
18
18
  ]
19
19
  s.files = [
20
20
  ".document",
@@ -24,9 +24,13 @@ Gem::Specification.new do |s|
24
24
  "Gemfile",
25
25
  "Gemfile.lock",
26
26
  "LICENSE.txt",
27
- "README.rdoc",
27
+ "README.md",
28
28
  "Rakefile",
29
29
  "VERSION",
30
+ "lib/config/locales/en.yml",
31
+ "lib/mongoid/document/touch.rb",
32
+ "lib/mongoid/errors/document_not_updated.rb",
33
+ "lib/mongoid/errors/frozen_instance.rb",
30
34
  "lib/mongoid/errors/missing_field.rb",
31
35
  "lib/mongoid_touch.rb",
32
36
  "mongoid_touch.gemspec",
@@ -43,7 +47,7 @@ Gem::Specification.new do |s|
43
47
  s.specification_version = 3
44
48
 
45
49
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
46
- s.add_runtime_dependency(%q<mongoid>, ["~> 2.1.7"])
50
+ s.add_runtime_dependency(%q<mongoid>, ["~> 2.1"])
47
51
  s.add_development_dependency(%q<rake>, [">= 0"])
48
52
  s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
49
53
  s.add_development_dependency(%q<mongoid-rspec>, ["~> 1.4.4"])
@@ -52,7 +56,7 @@ Gem::Specification.new do |s|
52
56
  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
53
57
  s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
54
58
  else
55
- s.add_dependency(%q<mongoid>, ["~> 2.1.7"])
59
+ s.add_dependency(%q<mongoid>, ["~> 2.1"])
56
60
  s.add_dependency(%q<rake>, [">= 0"])
57
61
  s.add_dependency(%q<rspec>, ["~> 2.3.0"])
58
62
  s.add_dependency(%q<mongoid-rspec>, ["~> 1.4.4"])
@@ -62,7 +66,7 @@ Gem::Specification.new do |s|
62
66
  s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
63
67
  end
64
68
  else
65
- s.add_dependency(%q<mongoid>, ["~> 2.1.7"])
69
+ s.add_dependency(%q<mongoid>, ["~> 2.1"])
66
70
  s.add_dependency(%q<rake>, [">= 0"])
67
71
  s.add_dependency(%q<rspec>, ["~> 2.3.0"])
68
72
  s.add_dependency(%q<mongoid-rspec>, ["~> 1.4.4"])
@@ -2,34 +2,78 @@
2
2
 
3
3
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
4
4
 
5
- class TestDoc
5
+ class TestDocOne
6
6
  include Mongoid::Document
7
7
  include Mongoid::Timestamps
8
+ include Mongoid::Document::Touch
8
9
  field :name, type: String
9
10
  end
10
11
 
11
12
  class TestDocTwo
12
13
  include Mongoid::Document
14
+ include Mongoid::Document::Touch
13
15
  field :name, type: String
14
16
  field :modified_at, type: Time
15
17
  end
16
18
 
17
- describe "Mongoid::Document#touch" do
19
+ describe Mongoid::Document::Touch do
20
+
21
+ describe ".touch" do
22
+
23
+ it "updates the updated_at field" do
24
+ doc = TestDocOne.create(name: "My test doc")
25
+ origin_at = doc.updated_at
26
+ sleep 1.1
27
+ doc.touch.should be_true
28
+ doc.updated_at.should > origin_at
29
+ end
30
+
31
+ it "updates a custom Time field" do
32
+ doc = TestDocTwo.create(name: "My test doc", modified_at: Time.now.utc)
33
+ origin_at = doc.modified_at
34
+ sleep 1.1
35
+ doc.touch(:modified_at).should be_true
36
+ doc.modified_at.should > origin_at
37
+ end
18
38
 
19
- it "should update the updated_at field" do
20
- doc = TestDoc.create(name: "My test doc")
21
- origin_at = doc.updated_at
22
- sleep 1
23
- doc.touch
24
- doc.updated_at.should > origin_at
25
39
  end
26
-
27
- it "should update a custom Time field" do
28
- doc = TestDocTwo.create(name: "My test doc", modified_at: Time.now.utc)
29
- origin_at = doc.modified_at
30
- sleep 1
31
- doc.touch(:modified_at)
32
- doc.modified_at.should > origin_at
40
+
41
+ describe ".touch!" do
42
+
43
+ it "returns true if :touch is successful on a timestamp" do
44
+ doc = TestDocOne.create(name: "My test doc")
45
+ doc.touch!.should be_true
46
+ end
47
+
48
+ it "raises an error if :touch failed on frozen object" do
49
+ doc = TestDocOne.create(name: "My test doc")
50
+ doc.freeze
51
+ lambda { doc.touch! }.should raise_error(Mongoid::Errors::FrozenInstance)
52
+ end
53
+
54
+ it "returns true if :touch! is successful on a custom time field" do
55
+ doc = TestDocTwo.create(name: "My test doc", modified_at: Time.now.utc)
56
+ doc.touch!(:modified_at).should be_true
57
+ end
58
+
59
+ it "raises an error if :touch! failed on frozen object" do
60
+ doc = TestDocTwo.create(name: "My test doc", modified_at: Time.now.utc)
61
+ doc.freeze
62
+ lambda { doc.touch!(:modified_at) }.should raise_error(Mongoid::Errors::FrozenInstance)
63
+ end
64
+
65
+ it "raises a MissingField error if :touch! tries to modify a non-existing field" do
66
+ doc = TestDocTwo.create(name: "My test doc", modified_at: Time.now.utc)
67
+ lambda { doc.touch!(:undefined_at) }.should raise_error(Mongoid::Errors::MissingField)
68
+ end
69
+
70
+ # force this special fail with the optional param "force_fail = true" in #touch! method
71
+ it "raises a DocumentNotUpdated error if :touch! can't update." do
72
+ doc = TestDocOne.create(name: "My test doc")
73
+ lambda { doc.touch!(:undefined_at,true) }.should raise_error(Mongoid::Errors::DocumentNotUpdated)
74
+ end
75
+
33
76
  end
34
77
 
35
78
  end
79
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_touch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,22 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-14 00:00:00.000000000Z
12
+ date: 2011-08-25 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid
16
- requirement: &14012140 !ruby/object:Gem::Requirement
16
+ requirement: &28563080 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: 2.1.7
21
+ version: '2.1'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *14012140
24
+ version_requirements: *28563080
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &14011540 !ruby/object:Gem::Requirement
27
+ requirement: &28562380 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *14011540
35
+ version_requirements: *28562380
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &14011000 !ruby/object:Gem::Requirement
38
+ requirement: &28561680 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 2.3.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *14011000
46
+ version_requirements: *28561680
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: mongoid-rspec
49
- requirement: &14010520 !ruby/object:Gem::Requirement
49
+ requirement: &28560880 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.4.4
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *14010520
57
+ version_requirements: *28560880
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: bson_ext
60
- requirement: &13990880 !ruby/object:Gem::Requirement
60
+ requirement: &28560200 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ~>
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 1.3.1
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *13990880
68
+ version_requirements: *28560200
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: database_cleaner
71
- requirement: &13990200 !ruby/object:Gem::Requirement
71
+ requirement: &28534820 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ~>
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: 0.6.7
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *13990200
79
+ version_requirements: *28534820
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: bundler
82
- requirement: &13989560 !ruby/object:Gem::Requirement
82
+ requirement: &28534020 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ~>
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: 1.0.0
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *13989560
90
+ version_requirements: *28534020
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: jeweler
93
- requirement: &13988980 !ruby/object:Gem::Requirement
93
+ requirement: &28533280 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ~>
@@ -98,7 +98,7 @@ dependencies:
98
98
  version: 1.6.4
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *13988980
101
+ version_requirements: *28533280
102
102
  description: A tiny mongoid extension to provide the `touch` method known from ActiveRecord
103
103
  to Mongoid::Document.
104
104
  email: chris@dinarrr.com
@@ -106,7 +106,7 @@ executables: []
106
106
  extensions: []
107
107
  extra_rdoc_files:
108
108
  - LICENSE.txt
109
- - README.rdoc
109
+ - README.md
110
110
  files:
111
111
  - .document
112
112
  - .rspec
@@ -115,9 +115,13 @@ files:
115
115
  - Gemfile
116
116
  - Gemfile.lock
117
117
  - LICENSE.txt
118
- - README.rdoc
118
+ - README.md
119
119
  - Rakefile
120
120
  - VERSION
121
+ - lib/config/locales/en.yml
122
+ - lib/mongoid/document/touch.rb
123
+ - lib/mongoid/errors/document_not_updated.rb
124
+ - lib/mongoid/errors/frozen_instance.rb
121
125
  - lib/mongoid/errors/missing_field.rb
122
126
  - lib/mongoid_touch.rb
123
127
  - mongoid_touch.gemspec
@@ -138,7 +142,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
138
142
  version: '0'
139
143
  segments:
140
144
  - 0
141
- hash: -1506535053835906170
145
+ hash: -1334556587712060989
142
146
  required_rubygems_version: !ruby/object:Gem::Requirement
143
147
  none: false
144
148
  requirements:
data/README.rdoc DELETED
@@ -1,19 +0,0 @@
1
- = mongoid_touch
2
-
3
- A tiny mongoid extension to provide the `touch` method known from ActiveRecord to Mongoid::Document.
4
-
5
- == Contributing to mongoid_touch
6
-
7
- * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
- * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
- * Fork the project
10
- * Start a feature/bugfix branch
11
- * Commit and push until you are happy with your contribution
12
- * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
- * 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.
14
-
15
- == Copyright
16
-
17
- Copyright (c) 2011 Christoph Grabo. See LICENSE.txt for
18
- further details.
19
-