mongoid-bigbang 0.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 07d925181ee2f503fac3385f29ca45dc87e39f87
4
+ data.tar.gz: 1fc92dda7e7fc6b1144ccb142367f791bf5efa4b
5
+ SHA512:
6
+ metadata.gz: 2b07eac33662dbf71a0f35dccb3461cd9f2626c33e87a56a8380afed44e10179a4acab80f94e8f30a189ac9581725feb458e56340ff5d70931f3d8760950a4f2
7
+ data.tar.gz: 6657d7cc692842f6e9bb4546fe141f86b55ce20cb787a5e0f377568ea0b9f90c9656d0fb06ebaa05dbbc34536ceb90c7cc004be932d4e6a79d63704d0ef05c72
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Daniel Serrano
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.
@@ -0,0 +1,63 @@
1
+ # Mongoid::Bigbang
2
+
3
+ > They say it all started out with a big bang. But, what I wonder is, was it a big bang or did it just seem big because there wasn't anything else drown it out at the time? - Karl Pilkington
4
+
5
+ ## Why?
6
+
7
+ When you have a project in which you are not using `Mongoid::Timestamps` and you want to mock an object's creation time, you have to do some cumbersome operations in order to get those first 4 bytes of the `ObjectId` to represent the seconds since the Unix epoch that you want for that object.
8
+
9
+ Particularly, if you want to have two objects with the same creation time, it would not suffice to generate the IDs via the `BSON::ObjectId.from_time` method, since it would yield the same ID for both objects, and you probably do not want them to be seen as the same object.
10
+
11
+ This gem solves this little annoying issue by generating a unique ID for the given timestamp by using the other 8 bytes in `ObjectId` to generate the needed additional entropy.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'mongoid-bigbang'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install mongoid-bigbang
28
+
29
+ ## Usage
30
+
31
+ Whenever you want to generate a new `ObjectId` for a given timestamp, just call the `Mongoid::Bigbang.id_from_time` method, like follows:
32
+
33
+ ```ruby
34
+ # from time as String
35
+ Mongoid::Bigbang.id_from_time('18-01-2015 15:53:00 +0000')
36
+
37
+ # from time as Integer
38
+ Mongoid::Bigbang.id_from_time(1421596380)
39
+
40
+ # from time as Time
41
+ time = Time.parse('18-01-2015 15:53:00 +0000')
42
+ Mongoid::Bigbang.id_from_time(time)
43
+ ```
44
+
45
+ It is recommended that you add the following to your `spec_helper`:
46
+
47
+ ```ruby
48
+ RSpec.configure do |config|
49
+ config.before(:each) do
50
+ Mongoid::Bigbang.clean
51
+ end
52
+
53
+ # your other configs...
54
+ end
55
+ ```
56
+
57
+ ## Contributing
58
+
59
+ 1. Fork it ( https://github.com/[my-github-username]/mongoid-bigbang/fork )
60
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
61
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
62
+ 4. Push to the branch (`git push origin my-new-feature`)
63
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,16 @@
1
+ require 'mongoid/bigbang/version'
2
+ require 'mongoid/bigbang/generator'
3
+
4
+ module Mongoid
5
+ module Bigbang
6
+
7
+ def self.id_from_time(time)
8
+ Mongoid::Bigbang::Generator.instance.id_from_time(time)
9
+ end
10
+
11
+ def self.clean
12
+ Mongoid::Bigbang::Generator.instance.clean
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,61 @@
1
+ require 'singleton'
2
+ require 'time'
3
+ require 'securerandom'
4
+ require 'moped'
5
+
6
+ module Mongoid
7
+ module Bigbang
8
+ class Generator
9
+
10
+ include Singleton
11
+
12
+ attr_reader :used
13
+
14
+ def initialize()
15
+ clean
16
+ end
17
+
18
+ def clean
19
+ @used = {}
20
+ end
21
+
22
+ def id_from_time(time)
23
+ time = unix_time(time)
24
+ string = time.to_s(16) + random_for(time)
25
+ BSON::ObjectId.from_string(string)
26
+ end
27
+
28
+ private
29
+ def unix_time(time)
30
+ case time
31
+ when Time
32
+ time.to_i
33
+ when Integer
34
+ time
35
+ when String
36
+ Time.parse(time).to_i
37
+ else
38
+ raise ArgumentError
39
+ end
40
+ end
41
+
42
+ def random_for(time)
43
+ random = secure_random
44
+ while already_used?(time, random)
45
+ random = secure_random
46
+ end
47
+ used[time] ? used[time].push(random) : used[time] = [random]
48
+ random
49
+ end
50
+
51
+ def secure_random
52
+ SecureRandom.hex(8)
53
+ end
54
+
55
+ def already_used?(time, random)
56
+ used[time] && used[time].include?(random)
57
+ end
58
+
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,5 @@
1
+ module Mongoid
2
+ module Bigbang
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mongoid/bigbang/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mongoid-bigbang"
8
+ spec.version = Mongoid::Bigbang::VERSION
9
+ spec.authors = ["Daniel Serrano"]
10
+ spec.email = ["danieljdserrano@gmail.com"]
11
+ spec.summary = %q{Mock creation times through Moped::BSON::ObjectId.}
12
+ spec.description = %Q{
13
+ When you have a project in which you are not using Mongoid::Timestamps and you want to mock an object's creation time, you have to do some cumbersome operations in order to get those first 4 bytes of the ObjectId to represent the seconds since the Unix epoch that you want for that object.\n
14
+ Particularly, if you want to have two objects with the same creation time, it would not suffice to generate the IDs via the BSON::ObjectId.from_time method, since it would yield the same ID for both objects, and you probably do not want them to be seen as the same object.\n
15
+ This gem solves this little annoying issue by generating a unique ID for the given timestamp by using the other 8 bytes in ObjectId to generate the needed additional entropy.
16
+ }
17
+ spec.homepage = "https://github.com/dnlserrano/mongoid-bigbang"
18
+ spec.license = "MIT"
19
+
20
+ spec.files = `git ls-files -z`.split("\x0")
21
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.7"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.1.0"
28
+ spec.add_development_dependency "pry", "~> 0.10.1"
29
+ spec.add_development_dependency "pry-byebug", "~> 2.0.0"
30
+
31
+ spec.add_dependency "moped", "~> 2.0.0"
32
+ end
@@ -0,0 +1,17 @@
1
+ require 'database_cleaner'
2
+ require 'spec_helper'
3
+
4
+ RSpec.configure do |config|
5
+ config.before(:suite) do
6
+ DatabaseCleaner.strategy = :transaction
7
+ DatabaseCleaner.clean_with(:truncation)
8
+ end
9
+
10
+ config.before(:each) do
11
+ DatabaseCleaner.clean_with :truncation, { :reset_ids => true }
12
+ end
13
+
14
+ config.after(:each) do
15
+ DatabaseCleaner.clean_with :truncation, { :reset_ids => true }
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ RSpec.configure do |config|
2
+ config.color = true
3
+
4
+ config.expect_with :rspec do |c|
5
+ c.syntax = :should
6
+ end
7
+
8
+ config.mock_with :rspec do |c|
9
+ c.syntax = :should
10
+ end
11
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+ require 'mongoid/bigbang'
3
+
4
+ describe Mongoid::Bigbang do
5
+
6
+ subject { Mongoid::Bigbang }
7
+
8
+ let(:generator) { Mongoid::Bigbang::Generator.instance }
9
+ let(:time) { '18-01-2015 15:53:00 +0000' }
10
+
11
+ describe '.id_from_time' do
12
+ it 'calls the corresponding method on the generator' do
13
+ generator.should_receive(:id_from_time)
14
+ subject.id_from_time(time)
15
+ end
16
+ end
17
+
18
+ describe '.clean' do
19
+ it 'calls the corresponding method on the generator' do
20
+ generator.should_receive(:clean)
21
+ subject.clean
22
+ end
23
+ end
24
+
25
+ end
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+ require 'mongoid/bigbang/generator'
3
+
4
+ describe Mongoid::Bigbang::Generator do
5
+
6
+ subject { Mongoid::Bigbang::Generator.instance }
7
+
8
+ describe '#id_from_time' do
9
+ let(:bigbang) { Time.parse('18-01-2015 15:53:00 +0000') }
10
+
11
+ it 'generates an id for the given time' do
12
+ id = subject.id_from_time(bigbang)
13
+ id.generation_time.should eq bigbang
14
+ end
15
+
16
+ context 'when the time is a string' do
17
+ let(:time) { bigbang.to_s }
18
+ it 'generates an id for the given time' do
19
+ id = subject.id_from_time(time)
20
+ id.generation_time.should eq bigbang
21
+ end
22
+ end
23
+
24
+ context 'when the time is an integer' do
25
+ let(:time) { bigbang.to_i }
26
+ it 'generates an id for the given time' do
27
+ id = subject.id_from_time(time)
28
+ id.generation_time.should eq bigbang
29
+ end
30
+ end
31
+
32
+ context 'when the random part has already been used for that timestamp' do
33
+ let(:secure_random) { SecureRandom }
34
+ it 'generates another random part' do
35
+ secure_random.should_receive(:hex).twice.and_return("63dd436fe9be4ece")
36
+ secure_random.should_receive(:hex).once.and_return("7cfe2c1518f8711b")
37
+ subject.should_receive(:secure_random).exactly(3).times.and_call_original
38
+
39
+ # create first ObjectId for this timestamp
40
+ # 1st call - does not enter loop since random 63dd436fe9be4ece doesn't exist yet
41
+ subject.id_from_time(bigbang)
42
+
43
+ # create second ObjectId for this timestamp
44
+ # 2nd call - enters loop because we force random 63dd436fe9be4ece to be returned again
45
+ # 3rd call - does not enter loop because it generates a new random 7cfe2c1518f8711b
46
+ subject.id_from_time(bigbang)
47
+ end
48
+ end
49
+
50
+ describe '#clean' do
51
+ it 'clears the already used randoms for each timestamp' do
52
+ subject.id_from_time(bigbang)
53
+ subject.used.length.should_not eq 0
54
+
55
+ subject.clean
56
+ subject.used.length.should eq 0
57
+ end
58
+ end
59
+ end
60
+
61
+ end
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-bigbang
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Serrano
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.1.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.1.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.10.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.10.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 2.0.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 2.0.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: moped
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 2.0.0
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 2.0.0
97
+ description: "\n When you have a project in which you are not using Mongoid::Timestamps
98
+ and you want to mock an object's creation time, you have to do some cumbersome operations
99
+ in order to get those first 4 bytes of the ObjectId to represent the seconds since
100
+ the Unix epoch that you want for that object.\n\n Particularly, if you want to
101
+ have two objects with the same creation time, it would not suffice to generate the
102
+ IDs via the BSON::ObjectId.from_time method, since it would yield the same ID for
103
+ both objects, and you probably do not want them to be seen as the same object.\n\n
104
+ \ This gem solves this little annoying issue by generating a unique ID for the
105
+ given timestamp by using the other 8 bytes in ObjectId to generate the needed additional
106
+ entropy.\n "
107
+ email:
108
+ - danieljdserrano@gmail.com
109
+ executables: []
110
+ extensions: []
111
+ extra_rdoc_files: []
112
+ files:
113
+ - ".gitignore"
114
+ - Gemfile
115
+ - LICENSE.txt
116
+ - README.md
117
+ - Rakefile
118
+ - lib/mongoid/bigbang.rb
119
+ - lib/mongoid/bigbang/generator.rb
120
+ - lib/mongoid/bigbang/version.rb
121
+ - mongoid-bigbang.gemspec
122
+ - spec/integration_spec_helper.rb
123
+ - spec/spec_helper.rb
124
+ - spec/unit/mongoid/bigbang/bigbang_spec.rb
125
+ - spec/unit/mongoid/bigbang/generator_spec.rb
126
+ homepage: https://github.com/dnlserrano/mongoid-bigbang
127
+ licenses:
128
+ - MIT
129
+ metadata: {}
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubyforge_project:
146
+ rubygems_version: 2.4.5
147
+ signing_key:
148
+ specification_version: 4
149
+ summary: Mock creation times through Moped::BSON::ObjectId.
150
+ test_files:
151
+ - spec/integration_spec_helper.rb
152
+ - spec/spec_helper.rb
153
+ - spec/unit/mongoid/bigbang/bigbang_spec.rb
154
+ - spec/unit/mongoid/bigbang/generator_spec.rb