Project

nagoriyuki

0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Distributed id generator
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.0.0
>= 0
>= 0
~> 3.5.0

Runtime

 Project Readme

Nagoriyuki

Gem Version Coverage Status Build Status

nagoriyuki(なごり雪) is a twitter/snowflake like ID generator.

This is the extraction of the minimum functionality with referred to the code of drecom/barrage.

And there are only a few feature additions.

Installation

Add this line to your application's Gemfile:

gem 'nagoriyuki'

And then execute:

$ bundle

Or install it yourself as:

$ gem install nagoriyuki

Usage

Example

# 39bit: msec (17.4 years from offset_epoch)
# 16bit: pid
# 9bit:  sequence
require 'nagoriyuki'

nagoriyuki = Nagoriyuki.new(
  "generators" => [
    {"name" => "msec", "length" => 39, "offset_epoch" => 1546268400000},
    {"name" => "pid", "length" => 16},
    {"name" => "sequence", "length" => 9}
  ]
)

id = nagoriyuki.next
# => 596299014690041345
  
ts = nagoriyuki.timestamp(id)
# => 1564039495

Time.at(ts)
# => 2019-07-25 16:24:55 +0900

Generators

msec

pid

sequence

Creating your own generator

module Nagoriyuki::Generators
  class YourOwnGenerator < Base
    self.required_options += %w(your_option_value)
    def generate
      # generated code
    end

    def your_option_value
      options["your_option_value"]
    end
  end
end

nagoriyuki = Nagoriyuki.new("generators" => [{"name"=>"your_own", "length" => 8, "your_option_value"=>"xxx"}])

License

Copyright (c) 2014 Drecom Co., Ltd.