mongoid_token 2.1.1 → 2.1.2
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.
- checksums.yaml +4 -4
- data/LICENSE.txt +22 -0
- data/README.md +11 -1
- data/lib/mongoid/token/finders.rb +6 -2
- data/lib/mongoid/token/generator.rb +9 -5
- data/lib/mongoid/token/options.rb +4 -0
- data/lib/version.rb +1 -1
- data/mongoid_token.gemspec +1 -0
- data/spec/mongoid/token/finders_spec.rb +20 -2
- data/spec/mongoid/token/generator_spec.rb +8 -0
- data/spec/mongoid/token_spec.rb +5 -1
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 70068b3941cfb6ef389bf2b25f371702ef34d911
|
4
|
+
data.tar.gz: 8cb79b51d9b94fb6c93da485a8c3c488f327a9ca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9627f4505f3fbd85b499b16b04a5c2a578e480dd9033107e36549c7746a128eb6963bdc51af1eb93aaa7b2fa067d0901b2b20d7ebd0eb3343c9d788456bb063b
|
7
|
+
data.tar.gz: 3c7707128f42056c1feb7b8bd1ba88a731954106a67e273e93043cefa07177c8db97deb9f0cfdbfa202c16644a9891c6791a35aadb3e719ed9375af72ea15955
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Nicholas Bruning
|
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
CHANGED
@@ -24,7 +24,7 @@ In your gemfile, add:
|
|
24
24
|
|
25
25
|
Then update your bundle
|
26
26
|
|
27
|
-
$ bundle
|
27
|
+
$ bundle install
|
28
28
|
|
29
29
|
In your Mongoid documents, just add `include Mongoid::Token` and the
|
30
30
|
`token` method will take care of all the setup, like so:
|
@@ -50,6 +50,13 @@ automatically creating a unique index on your documents using the token
|
|
50
50
|
field. In order to take advantage of this feature (and ensure that your
|
51
51
|
documents always have unique tokens) remember to create your indexes.
|
52
52
|
|
53
|
+
## Using without Rails
|
54
|
+
|
55
|
+
If you're using Mongoid without Rails, remember to:
|
56
|
+
|
57
|
+
require 'mongoid_token'
|
58
|
+
|
59
|
+
in your app
|
53
60
|
|
54
61
|
## Finders
|
55
62
|
|
@@ -110,6 +117,9 @@ as numbers
|
|
110
117
|
* `:fixed_numeric` - integer, but will always be of length `:length`
|
111
118
|
* `:fixed_numeric_no_leading_zeros` - same as `:fixed_numeric`, but will
|
112
119
|
never start with zeros
|
120
|
+
* `:fixed_hex_numeric` - hex integer, but will always be of length `:length`
|
121
|
+
* `:fixed_hex_numeric_no_leading_zeros` - same as `:fixed_hex_numeric`, but will
|
122
|
+
never start with zeros
|
113
123
|
|
114
124
|
__Examples:__
|
115
125
|
```ruby
|
@@ -2,8 +2,12 @@ module Mongoid
|
|
2
2
|
module Token
|
3
3
|
module Finders
|
4
4
|
def self.define_custom_token_finder_for(klass, field_name = :token)
|
5
|
-
klass.define_singleton_method(:"find_by_#{field_name
|
6
|
-
|
5
|
+
klass.define_singleton_method(:"find_by_#{field_name}") do |token|
|
6
|
+
if token.is_a?(Array)
|
7
|
+
self.in field_name.to_sym => token
|
8
|
+
else
|
9
|
+
self.find_by field_name.to_sym => token
|
10
|
+
end
|
7
11
|
end
|
8
12
|
|
9
13
|
klass.define_singleton_method :"find_with_#{field_name}" do |*args| # this is going to be painful if tokens happen to look like legal object ids
|
@@ -12,7 +12,7 @@
|
|
12
12
|
module Mongoid
|
13
13
|
module Token
|
14
14
|
module Generator
|
15
|
-
REPLACE_PATTERN = /%((?<character>[
|
15
|
+
REPLACE_PATTERN = /%((?<character>[cCdDhHpsw]{1})(?<length>\d+(,\d+)?)?)/
|
16
16
|
|
17
17
|
def self.generate(pattern)
|
18
18
|
pattern.gsub REPLACE_PATTERN do |match|
|
@@ -29,6 +29,10 @@ module Mongoid
|
|
29
29
|
digits(length)
|
30
30
|
when 'D'
|
31
31
|
integer(length)
|
32
|
+
when 'h'
|
33
|
+
digits(length, 16)
|
34
|
+
when 'H'
|
35
|
+
integer(length, 16)
|
32
36
|
when 's'
|
33
37
|
alphanumeric(length)
|
34
38
|
when 'w'
|
@@ -52,12 +56,12 @@ module Mongoid
|
|
52
56
|
self.rand_string_from_chars ('A'..'Z').to_a, length
|
53
57
|
end
|
54
58
|
|
55
|
-
def self.integer(length = 1)
|
56
|
-
(rand(
|
59
|
+
def self.integer(length = 1, base = 10)
|
60
|
+
(rand(base**length - base**(length-1)) + base**(length-1)).to_s(base)
|
57
61
|
end
|
58
62
|
|
59
|
-
def self.digits(length = 1)
|
60
|
-
rand(
|
63
|
+
def self.digits(length = 1, base = 10)
|
64
|
+
rand(base**length).to_s(base).rjust(length, "0")
|
61
65
|
end
|
62
66
|
|
63
67
|
def self.alpha(length = 1)
|
@@ -43,6 +43,10 @@ class Mongoid::Token::Options
|
|
43
43
|
"%d#{@options[:length]}"
|
44
44
|
when :fixed_numeric_no_leading_zeros
|
45
45
|
"%D#{@options[:length]}"
|
46
|
+
when :fixed_hex_numeric
|
47
|
+
"%h#{@options[:length]}"
|
48
|
+
when :fixed_hex_numeric_no_leading_zeros
|
49
|
+
"%H#{@options[:length]}"
|
46
50
|
end
|
47
51
|
end
|
48
52
|
|
data/lib/version.rb
CHANGED
data/mongoid_token.gemspec
CHANGED
@@ -9,6 +9,7 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.authors = ["Nicholas Bruning"]
|
10
10
|
s.email = ["nicholas@bruning.com.au"]
|
11
11
|
s.homepage = "http://github.com/thetron/mongoid_token"
|
12
|
+
s.licenses = ['MIT']
|
12
13
|
s.summary = %q{A little random, unique token generator for Mongoid documents.}
|
13
14
|
s.description = %q{Mongoid token is a gem for creating random, unique tokens for mongoid documents. Highly configurable and great for making URLs a little more compact.}
|
14
15
|
|
@@ -1,9 +1,11 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), %w[.. .. spec_helper])
|
2
2
|
|
3
3
|
describe Mongoid::Token::Finders do
|
4
|
-
|
4
|
+
after do
|
5
|
+
Object.send(:remove_const, :Document) if Object.constants.include?(:Document)
|
6
|
+
Object.send(:remove_const, :AnotherDocument) if Object.constants.include?(:AnotherDocument)
|
5
7
|
end
|
6
|
-
|
8
|
+
|
7
9
|
it "define a finder based on a field_name" do
|
8
10
|
klass = Class.new
|
9
11
|
field = :another_token
|
@@ -32,10 +34,26 @@ describe Mongoid::Token::Finders do
|
|
32
34
|
Document.find_by_token("1234").should == document
|
33
35
|
end
|
34
36
|
|
37
|
+
it 'retrieves multiple documents using the dynamic finder' do
|
38
|
+
class Document; include Mongoid::Document; field :token; end
|
39
|
+
document = Document.create!(:token => "1234")
|
40
|
+
document2 = Document.create!(:token => "5678")
|
41
|
+
Mongoid::Token::Finders.define_custom_token_finder_for(Document)
|
42
|
+
Document.find_by_token(["1234", "5678"]).should == [document, document2]
|
43
|
+
end
|
44
|
+
|
35
45
|
it "retrieve a document using the `find` method" do
|
36
46
|
class AnotherDocument; include Mongoid::Document; field :token; end
|
37
47
|
document = AnotherDocument.create! :token => "1234"
|
38
48
|
Mongoid::Token::Finders.define_custom_token_finder_for(AnotherDocument)
|
39
49
|
AnotherDocument.find("1234").should == document
|
40
50
|
end
|
51
|
+
|
52
|
+
it 'retrieves multiple documents using the `find` method' do
|
53
|
+
class AnotherDocument; include Mongoid::Document; field :token; end
|
54
|
+
document = AnotherDocument.create! :token => "1234"
|
55
|
+
document2 = AnotherDocument.create! :token => "5678"
|
56
|
+
Mongoid::Token::Finders.define_custom_token_finder_for(AnotherDocument)
|
57
|
+
AnotherDocument.find(["1234", "5678"]).should == [document, document2]
|
58
|
+
end
|
41
59
|
end
|
@@ -18,6 +18,14 @@ describe Mongoid::Token::Generator do
|
|
18
18
|
100.times{ Mongoid::Token::Generator.generate("%D").should =~ /[1-9]/ }
|
19
19
|
end
|
20
20
|
|
21
|
+
it "generates hexdigits" do
|
22
|
+
100.times{ Mongoid::Token::Generator.generate("%h").should =~ /[0-9a-f]/ }
|
23
|
+
end
|
24
|
+
|
25
|
+
it "generates non-zero hexdigits" do
|
26
|
+
100.times{ Mongoid::Token::Generator.generate("%H").should =~ /[1-9a-f]/ }
|
27
|
+
end
|
28
|
+
|
21
29
|
it "generates alphanumeric characters" do
|
22
30
|
100.times{ Mongoid::Token::Generator.generate("%s").should =~ /[A-Za-z0-9]/ }
|
23
31
|
end
|
data/spec/mongoid/token_spec.rb
CHANGED
@@ -1,8 +1,12 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), %w[.. spec_helper])
|
2
2
|
|
3
3
|
describe Mongoid::Token do
|
4
|
+
after do
|
5
|
+
Object.send(:remove_const, :Document) if Object.constants.include?(:Document)
|
6
|
+
Object.send(:remove_const, :AnotherDocument) if Object.constants.include?(:AnotherDocument)
|
7
|
+
end
|
8
|
+
|
4
9
|
let(:document_class) do
|
5
|
-
Object.send(:remove_const, :Document)
|
6
10
|
class Document
|
7
11
|
include Mongoid::Document
|
8
12
|
include Mongoid::Token
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_token
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.
|
4
|
+
version: 2.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nicholas Bruning
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-07-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|
@@ -37,6 +37,7 @@ files:
|
|
37
37
|
- ".rspec"
|
38
38
|
- ".travis.yml"
|
39
39
|
- Gemfile
|
40
|
+
- LICENSE.txt
|
40
41
|
- README.md
|
41
42
|
- Rakefile
|
42
43
|
- benchmarks/benchmark.rb
|
@@ -58,7 +59,8 @@ files:
|
|
58
59
|
- spec/mongoid/token_spec.rb
|
59
60
|
- spec/spec_helper.rb
|
60
61
|
homepage: http://github.com/thetron/mongoid_token
|
61
|
-
licenses:
|
62
|
+
licenses:
|
63
|
+
- MIT
|
62
64
|
metadata: {}
|
63
65
|
post_install_message:
|
64
66
|
rdoc_options: []
|
@@ -76,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
78
|
version: '0'
|
77
79
|
requirements: []
|
78
80
|
rubyforge_project: mongoid_token
|
79
|
-
rubygems_version: 2.
|
81
|
+
rubygems_version: 2.4.6
|
80
82
|
signing_key:
|
81
83
|
specification_version: 4
|
82
84
|
summary: A little random, unique token generator for Mongoid documents.
|