mongoid_token 1.1.0 → 1.1.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/.travis.yml CHANGED
@@ -7,3 +7,5 @@ gemfile:
7
7
  notifications:
8
8
  recipients:
9
9
  - nicholas@bruning.com.au
10
+ services:
11
+ - mongodb
data/Gemfile CHANGED
@@ -4,6 +4,5 @@ gemspec
4
4
 
5
5
  group :test do
6
6
  gem 'database_cleaner'
7
- gem 'autotest'
8
7
  gem 'mongoid-rspec', '1.5.1'
9
8
  end
data/README.md CHANGED
@@ -15,11 +15,19 @@ Into something more like this:
15
15
  http://myawesomewebapp.com/video/83xQ3r/edit
16
16
 
17
17
 
18
+ ## Mongoid 3.x Support
19
+
20
+ As of version 1.1.0, Mongoid::Token now supports Mongoid 3.x.
21
+
22
+ > If you still require __Mongoid 2.x__ support, please install
23
+ > Mongoid::Token 1.0.0.
24
+
25
+
18
26
  ## Getting started
19
27
 
20
28
  In your gemfile, add:
21
29
 
22
- gem 'mongoid_token', '~> 1.0.0'
30
+ gem 'mongoid_token', '~> 1.1.0'
23
31
 
24
32
  Then update your bundle
25
33
 
@@ -63,9 +71,13 @@ The options for `contains` are as follows:
63
71
 
64
72
  * `:alphanumeric` - letters (upper and lowercase) and numbers
65
73
  * `:alpha` - letters (upper and lowercase) only
74
+ * `:alpha_lower` - letters (lowercase) only
75
+ * `:alpha_upper` - letters (uppercase) only
66
76
  * `:numeric` - numbers only, anything from 1 character long, up to and
67
77
  `length`
68
78
  * `:fixed_numeric` - numbers only, but with the number of characters always the same as `length`
79
+ * `:fixed_numeric_no_leading_zeros` - as above, but will never start with
80
+ zeros
69
81
 
70
82
  You can also rename the token field, if required, using the
71
83
  `:field_name` option:
@@ -77,10 +89,13 @@ You can also rename the token field, if required, using the
77
89
  * `token :length => 8, :contains => :alphanumeric` generates something like `8Sdc98dQ`
78
90
  * `token :length => 5, :contains => :alpha` gereates something like
79
91
  `ASlkj`
92
+ * token :length
80
93
  * `token :length => 4, :contains => :numeric` could generate anything
81
94
  from `0` upto `9999` - but in a random order
82
95
  * `token :length => 4, :contains => :fixed_numeric` will generate
83
96
  anything from `0000` to `9999` in a random order
97
+ * `token :length => 4, :contains => :fixed_numeric_no_leading_zeros` will
98
+ generate anything from `1000` to `9999` in a random order
84
99
 
85
100
 
86
101
  ## Finders
@@ -127,5 +142,14 @@ If you find a problem, please [submit an issue](http://github.com/thetron/mongoi
127
142
  you can). Pull requests and feature requests are always welcome and
128
143
  greatly appreciated.
129
144
 
130
- Thanks to everyone that has contributed to this gem over the past year,
131
- in particular [eagleas](https://github.com/eagleas) and [jamesotron](https://github.com/jamesotron). Many, many thanks guys.
145
+ Thanks to everyone that has contributed to this gem over the past year.
146
+ Many, many thanks - you guys rawk.
147
+
148
+ ## Contributors:
149
+
150
+ * [olliem](https://github.com/olliem)
151
+ * [msolli](https://github.com/msolli)
152
+ * [siong1987](https://github.com/siong1987)
153
+ * [stephan778](https://github.com/stephan778)
154
+ * [eagleas](https://github.com/eagleas)
155
+ * [jamesotron](https://github.com/jamesotron).
@@ -2,25 +2,24 @@ $: << File.expand_path("../../lib", __FILE__)
2
2
 
3
3
  require 'database_cleaner'
4
4
  require 'mongoid'
5
- require 'mongoid-rspec'
6
5
  require 'mongoid_token'
7
6
  require 'benchmark'
8
7
 
9
8
  Mongoid.configure do |config|
10
- config.master = Mongo::Connection.new.db("mongoid_token_benchmark")
9
+ config.connect_to("mongoid_token_benchmark")
11
10
  end
12
11
 
13
12
  DatabaseCleaner.strategy = :truncation
14
13
 
15
14
  # start benchmarks
16
15
 
17
- token_length = 2
16
+ TOKEN_LENGTH = 8
18
17
 
19
18
  class Link
20
19
  include Mongoid::Document
21
20
  include Mongoid::Token
22
21
  field :url
23
- token :length => 2, :contains => :alphanumeric
22
+ token :length => TOKEN_LENGTH, :contains => :alphanumeric
24
23
  end
25
24
 
26
25
  class NoTokenLink
@@ -39,7 +38,7 @@ end
39
38
  Link.destroy_all
40
39
  Link.create_indexes
41
40
  num_records = [1, 50, 100, 1000, 2000, 3000, 4000]
42
- puts "-- Alphanumeric token of length #{token_length} (#{62**token_length} possible tokens)"
41
+ puts "-- Alphanumeric token of length #{TOKEN_LENGTH} (#{62**TOKEN_LENGTH} possible tokens)"
43
42
  Benchmark.bm do |b|
44
43
  num_records.each do |qty|
45
44
  b.report("#{qty.to_s.rjust(5, " ")} records "){ qty.times{ create_link(false) } }
data/lib/mongoid_token.rb CHANGED
@@ -50,16 +50,22 @@ module Mongoid
50
50
  end
51
51
 
52
52
  def to_param
53
- self.send(@token_field_name.to_sym)
53
+ self.send(@token_field_name.to_sym) || super
54
54
  end
55
55
 
56
56
  protected
57
57
 
58
58
  def resolve_token_collisions
59
59
  retries = @max_collision_retries
60
+
60
61
  begin
61
62
  yield
62
63
  rescue Moped::Errors::OperationFailure => e
64
+ # This is horrible, but seems to be the only way to get the details of the exception?
65
+ continue unless [11000, 11001].include?(e.details['code'])
66
+ continue unless e.details['err'] =~ /dup key/ &&
67
+ e.details['err'] =~ /"#{self.send(@token_field_name.to_sym)}"/
68
+
63
69
  if (retries -= 1) > 0
64
70
  self.create_token(@token_length, @token_contains)
65
71
  retry
@@ -83,7 +89,9 @@ module Mongoid
83
89
  end
84
90
 
85
91
  def create_token_if_nil(length, characters)
86
- self.create_token(length, characters) if self[@token_field_name.to_sym].nil?
92
+ if self[@token_field_name.to_sym].blank?
93
+ self.create_token(length, characters)
94
+ end
87
95
  end
88
96
 
89
97
  def generate_token(length, characters = :alphanumeric)
@@ -94,8 +102,14 @@ module Mongoid
94
102
  rand(10**length).to_s
95
103
  when :fixed_numeric
96
104
  rand(10**length).to_s.rjust(length,rand(10).to_s)
105
+ when :fixed_numeric_no_leading_zeros
106
+ (rand(10**length - 10**(length-1)) + 10**(length-1)).to_s
97
107
  when :alpha
98
108
  Array.new(length).map{['A'..'Z','a'..'z'].map{|r|r.to_a}.flatten[rand(52)]}.join
109
+ when :alpha_lower
110
+ Array.new(length).map{['a'..'z'].map{|r|r.to_a}.flatten[rand(26)]}.join
111
+ when :alpha_upper
112
+ Array.new(length).map{['A'..'Z'].map{|r|r.to_a}.flatten[rand(26)]}.join
99
113
  end
100
114
  end
101
115
  end
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module MongoidToken
2
- VERSION = "1.1.0"
2
+ VERSION = "1.1.1"
3
3
  end
data/spec/mongoid.yml CHANGED
@@ -6,3 +6,4 @@ test:
6
6
  - localhost:27017
7
7
  options:
8
8
  consistency: :strong
9
+ safe: false
@@ -20,6 +20,8 @@ class Link
20
20
 
21
21
  field :url
22
22
  token :length => 3, :contains => :alphanumeric
23
+
24
+ validates :url, :presence => true
23
25
  end
24
26
 
25
27
  class FailLink
@@ -37,6 +39,22 @@ class Video
37
39
  token :length => 8, :contains => :alpha, :field_name => :vid
38
40
  end
39
41
 
42
+ class Image
43
+ include Mongoid::Document
44
+ include Mongoid::Token
45
+
46
+ field :url
47
+ token :length => 8, :contains => :fixed_numeric_no_leading_zeros
48
+ end
49
+
50
+ class Event
51
+ include Mongoid::Document
52
+ include Mongoid::Token
53
+
54
+ field :name
55
+ token :length => 8, :contains => :alpha_lower
56
+ end
57
+
40
58
  class Node
41
59
  include Mongoid::Document
42
60
  include Mongoid::Token
@@ -60,11 +78,15 @@ describe Mongoid::Token do
60
78
  @account = Account.create(:name => "Involved Pty. Ltd.")
61
79
  @link = Link.create(:url => "http://involved.com.au")
62
80
  @video = Video.create(:name => "Nyan nyan")
81
+ @image = Image.create(:url => "http://involved.com.au/image.png")
82
+ @event = Event.create(:name => "Super cool party!")
63
83
 
64
84
  Account.create_indexes
65
85
  Link.create_indexes
66
86
  FailLink.create_indexes
67
87
  Video.create_indexes
88
+ Image.create_indexes
89
+ Event.create_indexes
68
90
  Node.create_indexes
69
91
  end
70
92
 
@@ -78,6 +100,7 @@ describe Mongoid::Token do
78
100
  @account.token.length.should == 16
79
101
  @link.token.length.should == 3
80
102
  @video.vid.length.should == 8
103
+ @image.token.length.should == 8
81
104
  end
82
105
 
83
106
  it "should only generate unique tokens" do
@@ -106,6 +129,11 @@ describe Mongoid::Token do
106
129
  @video = Video.create(:name => "A test video")
107
130
  @video.vid.gsub(/[A-Za-z]/, "").length.should == 0
108
131
  end
132
+
133
+ 50.times do |index|
134
+ @event = Event.create(:name => "Super cool party!2")
135
+ @event.token.gsub(/[a-z]/, "").length.should == 0
136
+ end
109
137
  end
110
138
 
111
139
  it "should create the only after the first save" do
@@ -145,7 +173,7 @@ describe Mongoid::Token do
145
173
  @account.token.should_not be_nil
146
174
  end
147
175
 
148
- it "should fail with an exception after 3 retries (by default)" do
176
+ it "should fail with an exception after 3 retries" do
149
177
  Link.destroy_all
150
178
  Link.create_indexes
151
179
 
@@ -153,7 +181,6 @@ describe Mongoid::Token do
153
181
  Link.any_instance.stub(:generate_token).and_return(@first_link.token)
154
182
  @link = Link.new(:url => "http://fail.com")
155
183
 
156
- #lambda{ @link.save! }.should raise_error(Mongoid::Token::CollisionRetriesExceeded)
157
184
  expect { @link.save! }.to raise_error(Mongoid::Token::CollisionRetriesExceeded)
158
185
 
159
186
  Link.count.should == 1
@@ -165,8 +192,13 @@ describe Mongoid::Token do
165
192
 
166
193
  Link.any_instance.stub(:generate_token).and_return(link.token)
167
194
 
168
- expect { Link.create!(url: "http://example.com/2") }
169
- .to raise_error(Mongoid::Token::CollisionRetriesExceeded)
195
+ expect { Link.create!(url: "http://example.com/2") }.to raise_error(Mongoid::Token::CollisionRetriesExceeded)
196
+ end
197
+
198
+ it "should not raise a custom error if an operation failure is thrown for another reason" do
199
+ link = Link.new
200
+ lambda{ link.save! }.should_not raise_error(Mongoid::Token::CollisionRetriesExceeded)
201
+ link.valid?.should == false
170
202
  end
171
203
 
172
204
  it "should not raise a custom exception if retries are set to zero" do
@@ -177,7 +209,7 @@ describe Mongoid::Token do
177
209
  Link.any_instance.stub(:generate_token).and_return(@first_link.token)
178
210
  @link = FailLink.new(:url => "http://fail.com")
179
211
 
180
- lambda{ @link.save }.should_not raise_error(Mongoid::Token::CollisionRetriesExceeded)
212
+ lambda{ @link.save! }.should_not raise_error(Mongoid::Token::CollisionRetriesExceeded)
181
213
  end
182
214
 
183
215
  it "should create unique indexes on embedded documents" do
@@ -191,4 +223,13 @@ describe Mongoid::Token do
191
223
  node.token.match(/[0-9]{8}/).should_not == nil
192
224
  end
193
225
  end
226
+
227
+ describe "with :fixed_numeric_not_null" do
228
+ it "should not start with 0" do
229
+ 1000.times do
230
+ image = Image.create(:url => "http://something.com/image.png")
231
+ image.token.should_not start_with "0"
232
+ end
233
+ end
234
+ end
194
235
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_token
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-07 00:00:00.000000000 Z
12
+ date: 2013-02-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid
16
- requirement: &70226827473880 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,7 +21,12 @@ dependencies:
21
21
  version: '3.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70226827473880
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
25
30
  description: Mongoid token is a gem for creating random, unique tokens for mongoid
26
31
  documents. Highly configurable and great for making URLs a little more compact.
27
32
  email:
@@ -65,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
70
  version: '0'
66
71
  requirements: []
67
72
  rubyforge_project: mongoid_token
68
- rubygems_version: 1.8.15
73
+ rubygems_version: 1.8.25
69
74
  signing_key:
70
75
  specification_version: 3
71
76
  summary: A little random, unique token generator for Mongoid documents.