Class: TwitterSnowflake::Snowflake
- Inherits:
-
Object
- Object
- TwitterSnowflake::Snowflake
- Defined in:
- lib/twitter_snowflake/snowflake.rb
Overview
A snowflake ID as a Ruby object.
Instance Attribute Summary collapse
-
#epoch ⇒ Integer
readonly
The base epoch in milliseconds to perform calculations.
-
#id ⇒ Integer
readonly
The snowflake ID in its original form.
-
#increment ⇒ Integer
readonly
The increment extracted from the ID.
-
#process_id ⇒ Integer
readonly
The proccess ID extracted from the ID.
-
#time ⇒ Time
readonly
The timestamp as a Time object.
-
#timestamp ⇒ Integer
readonly
The timestamp extracted from the ID in milliseconds.
-
#worker_id ⇒ Integer
readonly
The worker ID extracted form the ID.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Checks if two snowflakes are equal.
-
#after?(other) ⇒ Boolean
Checks if this snowflake has been generated after another snowflake.
-
#before?(other) ⇒ Boolean
Checks if this snowflake has been generated before another snowflake.
-
#initialize(id:, epoch:) ⇒ Snowflake
constructor
A new instance of Snowflake.
Constructor Details
#initialize(id:, epoch:) ⇒ Snowflake
Returns a new instance of Snowflake.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/twitter_snowflake/snowflake.rb', line 29 def initialize(id:, epoch:) # Data used during extraction process @id = id @epoch = epoch # Extracting data @timestamp = TwitterSnowflake.(@id, epoch: @epoch) @worker_id = TwitterSnowflake.worker_id(@id) @process_id = TwitterSnowflake.process_id(@id) @increment = TwitterSnowflake.increment(@id) # Useful information @time = Time.at(@timestamp / 1000.0) end |
Instance Attribute Details
#epoch ⇒ Integer (readonly)
Returns the base epoch in milliseconds to perform calculations.
10 11 12 |
# File 'lib/twitter_snowflake/snowflake.rb', line 10 def epoch @epoch end |
#id ⇒ Integer (readonly)
Returns the snowflake ID in its original form.
7 8 9 |
# File 'lib/twitter_snowflake/snowflake.rb', line 7 def id @id end |
#increment ⇒ Integer (readonly)
Returns the increment extracted from the ID.
22 23 24 |
# File 'lib/twitter_snowflake/snowflake.rb', line 22 def increment @increment end |
#process_id ⇒ Integer (readonly)
Returns the proccess ID extracted from the ID.
19 20 21 |
# File 'lib/twitter_snowflake/snowflake.rb', line 19 def process_id @process_id end |
#time ⇒ Time (readonly)
Returns the timestamp as a Time object.
25 26 27 |
# File 'lib/twitter_snowflake/snowflake.rb', line 25 def time @time end |
#timestamp ⇒ Integer (readonly)
Returns the timestamp extracted from the ID in milliseconds.
13 14 15 |
# File 'lib/twitter_snowflake/snowflake.rb', line 13 def @timestamp end |
#worker_id ⇒ Integer (readonly)
Returns the worker ID extracted form the ID.
16 17 18 |
# File 'lib/twitter_snowflake/snowflake.rb', line 16 def worker_id @worker_id end |
Instance Method Details
#==(other) ⇒ Boolean
Checks if two snowflakes are equal. Two snowflakes are considered equal if both have the same ID and are based on the same epoch.
50 51 52 |
# File 'lib/twitter_snowflake/snowflake.rb', line 50 def ==(other) (@id == other.id) && (@epoch == other.epoch) end |
#after?(other) ⇒ Boolean
Checks if this snowflake has been generated after another snowflake.
68 69 70 |
# File 'lib/twitter_snowflake/snowflake.rb', line 68 def after?(other) @timestamp > other. end |
#before?(other) ⇒ Boolean
Checks if this snowflake has been generated before another snowflake.
59 60 61 |
# File 'lib/twitter_snowflake/snowflake.rb', line 59 def before?(other) @timestamp < other. end |