class TwitterSnowflake::Snowflake

A snowflake ID as a Ruby object.

Attributes

epoch[R]

@return [Integer] the base epoch in milliseconds to perform calculations.

id[R]

@return [Integer] the snowflake ID in its original form.

increment[R]

@return [Integer] the increment extracted from the ID.

process_id[R]

@return [Integer] the proccess ID extracted from the ID.

time[R]

@return [Time] the timestamp as a Time object

timestamp[R]

@return [Integer] the timestamp extracted from the ID in milliseconds.

worker_id[R]

@return [Integer] the worker ID extracted form the ID.

Public Class Methods

new(id:, epoch:) click to toggle source

@param id [Integer] the ID itself. @param epoch [Intger] base epoch in milliseconds to perform calculations.

# 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.timestamp(@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

Public Instance Methods

==(other) click to toggle source

Checks if two snowflakes are equal. Two snowflakes are considered equal if both have the same ID and are based on the same epoch.

@param other [Snowflake] the other snowflake.

@return [Boolean] whether the snowflakes are equal or not.

# File lib/twitter_snowflake/snowflake.rb, line 50
def ==(other)
  (@id == other.id) && (@epoch == other.epoch)
end
after?(other) click to toggle source

Checks if this snowflake has been generated after another snowflake.

@param other [Snowflake] the other snowflake.

@return [Boolean] whether this snowflake has been generated after another snowflake or not.

# File lib/twitter_snowflake/snowflake.rb, line 68
def after?(other)
  @timestamp > other.timestamp
end
before?(other) click to toggle source

Checks if this snowflake has been generated before another snowflake.

@param other [Snowflake] the other snowflake.

@return [Boolean] whether this snowflake has been generated before another snowflake or not.

# File lib/twitter_snowflake/snowflake.rb, line 59
def before?(other)
  @timestamp < other.timestamp
end