rubyflake 0.1.0

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.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +5 -0
  3. data/lib/rubyflake.rb +33 -0
  4. metadata +46 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c3b4e92e938e501a17d7051b05eac6638b2328b4
4
+ data.tar.gz: dd3472f19ee6df699e1c3e2dd20f28c68a406d12
5
+ SHA512:
6
+ metadata.gz: 9f0b909764d964b351257e35b78e8e88271f3b4a27ffb32f9dae73c4f22af517b21912976d812fd829a6abe62201f7f8c94ce716c9fd644a3d7c20196f0902a1
7
+ data.tar.gz: d87c17b6b710350c5d888baeb3c7b1f32cca12e25fbd5b27df534748466eb3ab4acc7a71ab3f19d064c06b3ebb4a6f9ac75ae4a4678f144697ff72350d4ec134
data/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # Rubyflake
2
+
3
+ Distributed 64-bit ID generation.
4
+
5
+ A simplified ruby version of python's [simpleflake](https://github.com/SawdustSoftware/simpleflake).
data/lib/rubyflake.rb ADDED
@@ -0,0 +1,33 @@
1
+ class Rubyflake
2
+ # 01/01/2011
3
+ EPOCH = 1293858000
4
+
5
+ FLAKE_TIMESTAMP_LENGTH = 41
6
+
7
+ FLAKE_RANDOM_LENGTH = 0b1111111111111111111111111
8
+ FLAKE_TIMESTAMP_SHIFT = 23
9
+
10
+ def initialize(epoch = EPOCH)
11
+ @epoch = epoch
12
+ end
13
+
14
+ def generate
15
+ # Figure out how many milliseconds have occurred since epoch.
16
+ milliseconds = ((Time.now().to_f - @epoch) * 1000).to_i
17
+
18
+ # Generate 23 random bits.
19
+ random_bits = Random.new.rand(0..Rubyflake::FLAKE_RANDOM_LENGTH)
20
+
21
+ # Shift our timestamp over 23 bits to make room for the random bits,
22
+ # and then add the two together.
23
+ (milliseconds << Rubyflake::FLAKE_TIMESTAMP_SHIFT) + random_bits.to_i
24
+
25
+ end
26
+
27
+ class << self
28
+ def generate(epoch = Rubyflake::EPOCH)
29
+ Rubyflake.new(epoch).generate()
30
+ end
31
+ end
32
+
33
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubyflake
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matt Hogan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-25 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: For the times you can't rely on auto-incrementing IDs, you'll need a
14
+ clever way to generate unique ids. Inspired heavily by Simpleflake for Python.
15
+ email: m@fubelly.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - README.md
21
+ - lib/rubyflake.rb
22
+ homepage: https://github.com/fubelly/rubyflake
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.2.2
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Simple, distributed 64-bit unique ID generation.
46
+ test_files: []