nagoriyuki 0.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f6ac76b019d63fc584724388eb4ea31c474d9d7b13c89adfbd82748c3ed7ff83
4
+ data.tar.gz: c1d92517a7f01a18cfabdf426c9a549c504110fa452f23052ac2e9b342c44110
5
+ SHA512:
6
+ metadata.gz: 6a35bf2e68cf3bbe8cf05871ad7f353d6d305c810ea748abf9f5e416cf71808f3ed0fd8c34aed67fa6455cf5d63207b5ac3ad57651942aa7afb1ec46fd6e7be7
7
+ data.tar.gz: e490a59f4a4511d50817e8220ad92e02cc6b0e851038664f98947857a584394b511c12150c41d025ab0f816e656cb75bcddb13641b334964c98fb97a401953d0
data/.gitignore ADDED
@@ -0,0 +1,50 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.5.5
4
+ bundler_args: --jobs=2
5
+ script: bundle exec rake spec
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # CHANGELOG
2
+
3
+ ## 0.0.1 - First Release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nagoriyuki.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,111 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ nagoriyuki (0.0.1)
5
+ activesupport
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (5.2.3)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 0.7, < 2)
13
+ minitest (~> 5.1)
14
+ tzinfo (~> 1.1)
15
+ chronic (0.10.2)
16
+ coderay (1.1.2)
17
+ concurrent-ruby (1.1.5)
18
+ coveralls (0.8.23)
19
+ json (>= 1.8, < 3)
20
+ simplecov (~> 0.16.1)
21
+ term-ansicolor (~> 1.3)
22
+ thor (>= 0.19.4, < 2.0)
23
+ tins (~> 1.6)
24
+ delorean (2.1.0)
25
+ chronic
26
+ diff-lcs (1.3)
27
+ docile (1.3.2)
28
+ ffi (1.11.1)
29
+ formatador (0.2.5)
30
+ guard (2.15.0)
31
+ formatador (>= 0.2.4)
32
+ listen (>= 2.7, < 4.0)
33
+ lumberjack (>= 1.0.12, < 2.0)
34
+ nenv (~> 0.1)
35
+ notiffany (~> 0.0)
36
+ pry (>= 0.9.12)
37
+ shellany (~> 0.0)
38
+ thor (>= 0.18.1)
39
+ guard-compat (1.2.1)
40
+ guard-rspec (4.7.3)
41
+ guard (~> 2.1)
42
+ guard-compat (~> 1.1)
43
+ rspec (>= 2.99.0, < 4.0)
44
+ i18n (1.6.0)
45
+ concurrent-ruby (~> 1.0)
46
+ json (2.2.0)
47
+ listen (3.1.5)
48
+ rb-fsevent (~> 0.9, >= 0.9.4)
49
+ rb-inotify (~> 0.9, >= 0.9.7)
50
+ ruby_dep (~> 1.2)
51
+ lumberjack (1.0.13)
52
+ method_source (0.9.2)
53
+ minitest (5.11.3)
54
+ nenv (0.3.0)
55
+ notiffany (0.1.1)
56
+ nenv (~> 0.1)
57
+ shellany (~> 0.0)
58
+ pry (0.12.2)
59
+ coderay (~> 1.1.0)
60
+ method_source (~> 0.9.0)
61
+ rake (12.3.3)
62
+ rb-fsevent (0.10.3)
63
+ rb-inotify (0.10.0)
64
+ ffi (~> 1.0)
65
+ rspec (3.5.0)
66
+ rspec-core (~> 3.5.0)
67
+ rspec-expectations (~> 3.5.0)
68
+ rspec-mocks (~> 3.5.0)
69
+ rspec-core (3.5.4)
70
+ rspec-support (~> 3.5.0)
71
+ rspec-expectations (3.5.0)
72
+ diff-lcs (>= 1.2.0, < 2.0)
73
+ rspec-support (~> 3.5.0)
74
+ rspec-its (1.3.0)
75
+ rspec-core (>= 3.0.0)
76
+ rspec-expectations (>= 3.0.0)
77
+ rspec-mocks (3.5.0)
78
+ diff-lcs (>= 1.2.0, < 2.0)
79
+ rspec-support (~> 3.5.0)
80
+ rspec-support (3.5.0)
81
+ ruby_dep (1.5.0)
82
+ shellany (0.0.1)
83
+ simplecov (0.16.1)
84
+ docile (~> 1.1)
85
+ json (>= 1.8, < 3)
86
+ simplecov-html (~> 0.10.0)
87
+ simplecov-html (0.10.2)
88
+ term-ansicolor (1.7.1)
89
+ tins (~> 1.0)
90
+ thor (0.20.3)
91
+ thread_safe (0.3.6)
92
+ tins (1.21.1)
93
+ tzinfo (1.2.5)
94
+ thread_safe (~> 0.1)
95
+
96
+ PLATFORMS
97
+ ruby
98
+
99
+ DEPENDENCIES
100
+ bundler (~> 2.0.0)
101
+ coveralls
102
+ delorean
103
+ guard-rspec
104
+ nagoriyuki!
105
+ pry
106
+ rake
107
+ rspec (~> 3.5.0)
108
+ rspec-its
109
+
110
+ BUNDLED WITH
111
+ 2.0.1
data/Guardfile ADDED
@@ -0,0 +1,11 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec,
5
+ cmd: "bundle exec rspec",
6
+ all_after_pass: true,
7
+ all_on_start: true do
8
+ watch(%r{^spec/.+_spec\.rb$})
9
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
10
+ watch('spec/spec_helper.rb') { "spec" }
11
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2014 Drecom Co., Ltd.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,85 @@
1
+ # Nagoriyuki
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/nagoriyuki.svg)](http://badge.fury.io/rb/nagoriyuki)
4
+ [![Coverage Status](https://img.shields.io/coveralls/chibicco/nagoriyuki.svg)](https://coveralls.io/r/chibicco/nagoriyuki)
5
+ [![Build Status](https://travis-ci.org/chibicco/nagoriyuki.svg)](https://travis-ci.org/chibicco/nagoriyuki)
6
+
7
+ Distributed ID generator(like twitter/snowflake)
8
+
9
+
10
+ This is the extraction of the minimum functionality with referred to the code of [drecom/barrage](https://github.com/drecom/barrage).
11
+
12
+ And there are only a few feature additions.
13
+
14
+ ## Installation
15
+
16
+ Add this line to your application's Gemfile:
17
+
18
+ gem 'nagoriyuki'
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install nagoriyuki
27
+
28
+ ## Usage
29
+
30
+ ### Example
31
+
32
+ ```ruby
33
+ # 39bit: msec (17.4 years from offset_epoch)
34
+ # 16bit: pid
35
+ # 9bit: sequence
36
+ require 'nagoriyuki'
37
+
38
+ nagoriyuki = Nagoriyuki.new(
39
+ "generators" => [
40
+ {"name" => "msec", "length" => 39, "offset_epoch" => 1546268400000},
41
+ {"name" => "pid", "length" => 16},
42
+ {"name" => "sequence", "length" => 9}
43
+ ]
44
+ )
45
+
46
+ id = nagoriyuki.next
47
+ # => 596299014690041345
48
+
49
+ ts = nagoriyuki.timestamp(id)
50
+ # => 1564039495
51
+
52
+ Time.at(ts)
53
+ # => 2019-07-25 16:24:55 +0900
54
+ ```
55
+
56
+ ### Generators
57
+
58
+ #### msec
59
+
60
+ #### pid
61
+
62
+ #### sequence
63
+
64
+ ### Creating your own generator
65
+
66
+ ```ruby
67
+ module Nagoriyuki::Generators
68
+ class YourOwnGenerator < Base
69
+ self.required_options += %w(your_option_value)
70
+ def generate
71
+ # generated code
72
+ end
73
+
74
+ def your_option_value
75
+ options["your_option_value"]
76
+ end
77
+ end
78
+ end
79
+
80
+ nagoriyuki = Nagoriyuki.new("generators" => [{"name"=>"your_own", "length" => 8, "your_option_value"=>"xxx"}])
81
+ ```
82
+
83
+ ## License
84
+
85
+ [Copyright (c) 2014 Drecom Co., Ltd.](https://github.com/drecom/barrage/blob/master/LICENSE.txt)
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
8
+ desc "pry console"
9
+ task :console do
10
+ require "pry"
11
+ require "nagoriyuki"
12
+ binding.pry
13
+ end
data/lib/nagoriyuki.rb ADDED
@@ -0,0 +1,81 @@
1
+ require "nagoriyuki/version"
2
+ require "nagoriyuki/generators"
3
+ require "nagoriyuki/generators/base"
4
+ require "nagoriyuki/generators/msec"
5
+ require "nagoriyuki/generators/pid"
6
+ require "nagoriyuki/generators/sequence"
7
+ require "active_support/core_ext/string/inflections"
8
+
9
+ class Nagoriyuki
10
+ class InvalidOption < StandardError; end
11
+ attr_reader :generators
12
+
13
+ #
14
+ # generators:
15
+ # - name: msec
16
+ # length: 40 # (2 ** 40 - 1) / 1000 / 365 / 24 / 60 / 60.0
17
+ # start_at: 1546268400000 # 2019-01-01 00:00:00 +0900 (msec)
18
+ # - name: pid
19
+ # length: 16
20
+ # - name: sequence
21
+ # length: 8
22
+ def initialize(options = {})
23
+ @options = options
24
+ @lock = Mutex.new
25
+ @generators = @options["generators"].map do |h|
26
+ generator_name = h["name"]
27
+ "Nagoriyuki::Generators::#{generator_name.classify}".constantize.new(h)
28
+ end
29
+ end
30
+
31
+ def generate
32
+ @lock.synchronize do
33
+ shift_size = length
34
+ @generators.inject(0) do |result, generator|
35
+ shift_size = shift_size - generator.length
36
+ result += generator.generate << shift_size
37
+ end
38
+ end
39
+ end
40
+ alias_method :next, :generate
41
+
42
+ def current
43
+ @lock.synchronize do
44
+ shift_size = length
45
+ @generators.inject(0) do |result, generator|
46
+ shift_size = shift_size - generator.length
47
+ result += generator.current << shift_size
48
+ end
49
+ end
50
+ end
51
+
52
+ def timestamp(id)
53
+ generator = @generators.detect {|g| g.name == "msec" }
54
+ shift_size = length - generator.length
55
+ ((id >> shift_size) + generator.offset_epoch) / 1000
56
+ end
57
+
58
+ def smallest(timestamp)
59
+ @lock.synchronize do
60
+ shift_size = length
61
+ @generators.inject(0) do |result, generator|
62
+ shift_size = shift_size - generator.length
63
+ result += generator.smallest(timestamp) << shift_size
64
+ end
65
+ end
66
+ end
67
+
68
+ def biggest(timestamp)
69
+ @lock.synchronize do
70
+ shift_size = length
71
+ @generators.inject(0) do |result, generator|
72
+ shift_size = shift_size - generator.length
73
+ result += generator.biggest(timestamp) << shift_size
74
+ end
75
+ end
76
+ end
77
+
78
+ def length
79
+ @generators.inject(0) {|sum, g| sum += g.length }
80
+ end
81
+ end
@@ -0,0 +1,4 @@
1
+ class Nagoriyuki
2
+ module Generators
3
+ end
4
+ end
@@ -0,0 +1,54 @@
1
+ require "nagoriyuki/generators"
2
+ require "active_support/core_ext/class/attribute"
3
+
4
+ class Nagoriyuki
5
+ module Generators
6
+ class Base
7
+ attr_reader :options
8
+ class_attribute :required_options, :available_options
9
+ self.required_options = %w(length)
10
+ self.available_options = []
11
+
12
+ def initialize(options = {})
13
+ if (missing = missing_required_options(options)) && !missing.empty?
14
+ raise ArgumentError, "Missing Required options: #{missing.join(', ')}"
15
+ end
16
+ @options = options
17
+ end
18
+
19
+ def name
20
+ options["name"]
21
+ end
22
+
23
+ def length
24
+ options["length"]
25
+ end
26
+
27
+ def generate
28
+ raise NotImplemented, "Please Override"
29
+ end
30
+
31
+ def current
32
+ raise NotImplemented, "Please Override"
33
+ end
34
+
35
+ def smallest(timestamp)
36
+ raise NotImplemented, "Please Override"
37
+ end
38
+
39
+ def biggest(timestamp)
40
+ raise NotImplemented, "Please Override"
41
+ end
42
+
43
+ private
44
+
45
+ def bit_fill
46
+ (2 ** length - 1)
47
+ end
48
+
49
+ def missing_required_options(given_options)
50
+ required_options.reject { |k| given_options.has_key?(k) }
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,37 @@
1
+ require "nagoriyuki/generators/base"
2
+
3
+ class Nagoriyuki
4
+ module Generators
5
+ class Msec < Base
6
+ class InvalidSystemClock < StandardError; end
7
+
8
+ self.required_options += %w(offset_epoch)
9
+
10
+ def generate
11
+ subtract & bit_fill
12
+ end
13
+
14
+ alias_method :current, :generate
15
+
16
+ def offset_epoch
17
+ options["offset_epoch"]
18
+ end
19
+
20
+ def smallest(timestamp)
21
+ ((timestamp.to_s + "000").to_i - offset_epoch) & bit_fill
22
+ end
23
+
24
+ def biggest(timestamp)
25
+ ((timestamp.to_s + "999").to_i - offset_epoch) & bit_fill
26
+ end
27
+
28
+ private
29
+
30
+ def subtract
31
+ subtract = (Time.now.to_f * 1000).round - offset_epoch
32
+ raise InvalidSystemClock, "Clock moved backwards" if subtract > bit_fill
33
+ subtract
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,21 @@
1
+ require "nagoriyuki/generators/base"
2
+
3
+ class Nagoriyuki
4
+ module Generators
5
+ class Pid < Base
6
+ def generate
7
+ Process.pid & bit_fill
8
+ end
9
+
10
+ alias_method :current, :generate
11
+
12
+ def smallest(timestamp)
13
+ 0
14
+ end
15
+
16
+ def biggest(timestamp)
17
+ bit_fill
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,28 @@
1
+ require "nagoriyuki/generators/base"
2
+
3
+ class Nagoriyuki
4
+ module Generators
5
+ class Sequence < Base
6
+ def initialize(options)
7
+ @sequence = 0
8
+ super
9
+ end
10
+
11
+ def generate
12
+ @sequence = (@sequence + 1) & bit_fill
13
+ end
14
+
15
+ def current
16
+ @sequence
17
+ end
18
+
19
+ def smallest(timestamp)
20
+ 0
21
+ end
22
+
23
+ def biggest(timestamp)
24
+ bit_fill
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ class Nagoriyuki
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "nagoriyuki/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "nagoriyuki"
8
+ spec.version = Nagoriyuki::VERSION
9
+ spec.authors = ["chibicco"]
10
+ spec.email = ["chibiccoes@gmail.com"]
11
+ spec.summary = %q{Distributed id generator}
12
+ spec.description = %q{Distributed id generator}
13
+ spec.homepage = "http://github.com/chibicco/nagoriyuki"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activesupport"
22
+
23
+ spec.add_development_dependency "bundler", "~> 2.0.0"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec", "~> 3.5.0"
26
+ spec.add_development_dependency "rspec-its"
27
+ spec.add_development_dependency "pry"
28
+ spec.add_development_dependency "guard-rspec"
29
+ spec.add_development_dependency "delorean"
30
+ spec.add_development_dependency "coveralls"
31
+ end
@@ -0,0 +1,43 @@
1
+ require "spec_helper"
2
+ require "nagoriyuki/generators/msec"
3
+
4
+ describe Nagoriyuki::Generators::Msec do
5
+ context "When initialized" do
6
+ subject { described_class.new(options) }
7
+
8
+ context "with empty hash" do
9
+ let(:options) { {} }
10
+
11
+ it { expect { subject }.to raise_error(ArgumentError) }
12
+ end
13
+
14
+ context "with required options" do
15
+ let(:options) { {"length" => 16, "offset_epoch" => (Time.now.to_f * 1000).round } }
16
+
17
+ it { is_expected.to be_instance_of(Nagoriyuki::Generators::Msec) }
18
+ its(:length) { is_expected.to eq(options["length"]) }
19
+ its(:offset_epoch) { is_expected.to eq(options["offset_epoch"]) }
20
+ end
21
+ end
22
+
23
+ describe "#generate" do
24
+ subject { described_class.new(options).generate }
25
+ let(:length) { 39 }
26
+ let(:offset_epoch) { (Time.parse("2019-01-01 00:00:00").to_f*1000).round }
27
+ let(:options) { {"length" => length, "offset_epoch" => offset_epoch} }
28
+
29
+ it { is_expected.to be_instance_of(Fixnum) }
30
+ it { is_expected.to be < 2 ** length }
31
+
32
+ context "generate two numbers between 10 millisecond " do
33
+ subject { described_class.new(options) }
34
+
35
+ it "numbers difference that generated in interval 10 millisecond should exactly 10" do
36
+ t = Time.parse("2019-01-01 00:00:00")
37
+ first = time_travel_to(t) { subject.generate }
38
+ second = time_travel_to(Time.at(t.to_i, 10000)) { subject.generate }
39
+ expect(second - first).to eq(10)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,58 @@
1
+ require "spec_helper"
2
+ require "nagoriyuki/generators/sequence"
3
+
4
+ describe Nagoriyuki::Generators::Sequence do
5
+ context "When initialized" do
6
+ subject { described_class.new(options) }
7
+
8
+ context "with empty hash" do
9
+ let(:options) { {} }
10
+
11
+ it { expect { subject }.to raise_error(ArgumentError) }
12
+ end
13
+
14
+ context "with required options" do
15
+ let(:options) { {"length" => 16 } }
16
+
17
+ it { is_expected.to be_instance_of(Nagoriyuki::Generators::Sequence) }
18
+ its(:length) { is_expected.to eq(options["length"]) }
19
+ end
20
+ end
21
+
22
+ describe "#generate" do
23
+ subject { described_class.new(options).generate }
24
+ let(:length) { 9 }
25
+ let(:options) { {"length" => length } }
26
+
27
+ it { is_expected.to be_instance_of(Fixnum) }
28
+ it { is_expected.to be < 2 ** length }
29
+
30
+ context "When sequence is 0" do
31
+ before do
32
+ subject.instance_variable_set(:@sequence, 0)
33
+ end
34
+ subject { described_class.new(options) }
35
+
36
+ it "should generates '1'" do
37
+ expect(subject.generate).to eq(1)
38
+ end
39
+
40
+ it "if generate twice in a row, generated numbers should be in succession" do
41
+ first = subject.generate
42
+ second = subject.generate
43
+ expect(first.succ).to eq(second)
44
+ end
45
+ end
46
+
47
+ context "When sequence is max" do
48
+ before do
49
+ subject.instance_variable_set(:@sequence, (2 ** length) - 1)
50
+ end
51
+ subject { described_class.new(options) }
52
+
53
+ it "should generates zero" do
54
+ expect(subject.generate).to be_zero
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,59 @@
1
+ require "spec_helper"
2
+
3
+ describe Nagoriyuki do
4
+ it "has a version number" do
5
+ expect(Nagoriyuki::VERSION).not_to be nil
6
+ end
7
+
8
+ context "when initialized" do
9
+ subject { Nagoriyuki.new(options) }
10
+
11
+ context "with empty generators" do
12
+ let(:options) { {"generators" => []} }
13
+ its(:generators) { is_expected.to be_empty }
14
+ end
15
+
16
+ context "with generators" do
17
+ let(:options) {
18
+ {
19
+ "generators" => [
20
+ {"name" => "msec", "length" => 39, "offset_epoch" => 1396278000000},
21
+ {"name" => "pid", "length" => 16},
22
+ {"name" => "sequence", "length" => 9}
23
+ ]
24
+ }
25
+ }
26
+ it { expect(subject.generators.size).to eq(options["generators"].size) }
27
+ its(:generators) {
28
+ is_expected.to contain_exactly(
29
+ be_kind_of(Nagoriyuki::Generators::Msec),
30
+ be_kind_of(Nagoriyuki::Generators::Pid),
31
+ be_kind_of(Nagoriyuki::Generators::Sequence)
32
+ )
33
+ }
34
+
35
+ describe "#generate" do
36
+ let(:nagoriyuki) { described_class.new(options) }
37
+ subject { nagoriyuki.generate }
38
+ it { is_expected.to be_kind_of(Integer) }
39
+ it { is_expected.to be < 2 ** nagoriyuki.length }
40
+ end
41
+
42
+ describe "#length" do
43
+ subject { described_class.new(options).length }
44
+ it { is_expected.to eq(options["generators"].map { |h| h["length"]}.inject(:+)) }
45
+ end
46
+
47
+ describe "#timestamp" do
48
+ let(:nagoriyuki) { described_class.new(options) }
49
+
50
+ it {
51
+ t = Time.parse("2019-01-01 00:00:00")
52
+ id = time_travel_to(t) { nagoriyuki.generate }
53
+ timestamp = time_travel_to(t) { nagoriyuki.timestamp(id) }
54
+ expect(timestamp).to eq(t.to_i)
55
+ }
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,15 @@
1
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
2
+ require "bundler/setup"
3
+ require "rspec/its"
4
+ require "delorean"
5
+
6
+ require "coveralls"
7
+ Coveralls.wear!
8
+
9
+ require "nagoriyuki"
10
+
11
+ RSpec.configure do |config|
12
+ include Delorean
13
+ config.filter_run focus: true
14
+ config.run_all_when_everything_filtered = true
15
+ end
metadata ADDED
@@ -0,0 +1,195 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nagoriyuki
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - chibicco
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-07-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.5.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.5.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-its
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: delorean
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: coveralls
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: Distributed id generator
140
+ email:
141
+ - chibiccoes@gmail.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".travis.yml"
148
+ - CHANGELOG.md
149
+ - Gemfile
150
+ - Gemfile.lock
151
+ - Guardfile
152
+ - LICENSE
153
+ - README.md
154
+ - Rakefile
155
+ - lib/nagoriyuki.rb
156
+ - lib/nagoriyuki/generators.rb
157
+ - lib/nagoriyuki/generators/base.rb
158
+ - lib/nagoriyuki/generators/msec.rb
159
+ - lib/nagoriyuki/generators/pid.rb
160
+ - lib/nagoriyuki/generators/sequence.rb
161
+ - lib/nagoriyuki/version.rb
162
+ - nagoriyuki.gemspec
163
+ - spec/nagoriyuki/generators/msec_spec.rb
164
+ - spec/nagoriyuki/generators/sequence_spec.rb
165
+ - spec/nagoriyuki_spec.rb
166
+ - spec/spec_helper.rb
167
+ homepage: http://github.com/chibicco/nagoriyuki
168
+ licenses:
169
+ - MIT
170
+ metadata: {}
171
+ post_install_message:
172
+ rdoc_options: []
173
+ require_paths:
174
+ - lib
175
+ required_ruby_version: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ version: '0'
180
+ required_rubygems_version: !ruby/object:Gem::Requirement
181
+ requirements:
182
+ - - ">="
183
+ - !ruby/object:Gem::Version
184
+ version: '0'
185
+ requirements: []
186
+ rubyforge_project:
187
+ rubygems_version: 2.7.6.2
188
+ signing_key:
189
+ specification_version: 4
190
+ summary: Distributed id generator
191
+ test_files:
192
+ - spec/nagoriyuki/generators/msec_spec.rb
193
+ - spec/nagoriyuki/generators/sequence_spec.rb
194
+ - spec/nagoriyuki_spec.rb
195
+ - spec/spec_helper.rb