Class: TwitterSnowflake::Snowflake

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter_snowflake/snowflake.rb

Overview

A snowflake ID as a Ruby object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, epoch:) ⇒ Snowflake

Returns a new instance of Snowflake.

Parameters:

  • id (Integer)

    the ID itself.

  • epoch (Intger)

    base epoch in milliseconds to perform calculations.



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.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

Instance Attribute Details

#epochInteger (readonly)

Returns the base epoch in milliseconds to perform calculations.

Returns:

  • (Integer)

    the base epoch in milliseconds to perform calculations.



10
11
12
# File 'lib/twitter_snowflake/snowflake.rb', line 10

def epoch
  @epoch
end

#idInteger (readonly)

Returns the snowflake ID in its original form.

Returns:

  • (Integer)

    the snowflake ID in its original form.



7
8
9
# File 'lib/twitter_snowflake/snowflake.rb', line 7

def id
  @id
end

#incrementInteger (readonly)

Returns the increment extracted from the ID.

Returns:

  • (Integer)

    the increment extracted from the ID.



22
23
24
# File 'lib/twitter_snowflake/snowflake.rb', line 22

def increment
  @increment
end

#process_idInteger (readonly)

Returns the proccess ID extracted from the ID.

Returns:

  • (Integer)

    the proccess ID extracted from the ID.



19
20
21
# File 'lib/twitter_snowflake/snowflake.rb', line 19

def process_id
  @process_id
end

#timeTime (readonly)

Returns the timestamp as a Time object.

Returns:

  • (Time)

    the timestamp as a Time object



25
26
27
# File 'lib/twitter_snowflake/snowflake.rb', line 25

def time
  @time
end

#timestampInteger (readonly)

Returns the timestamp extracted from the ID in milliseconds.

Returns:

  • (Integer)

    the timestamp extracted from the ID in milliseconds.



13
14
15
# File 'lib/twitter_snowflake/snowflake.rb', line 13

def timestamp
  @timestamp
end

#worker_idInteger (readonly)

Returns the worker ID extracted form the ID.

Returns:

  • (Integer)

    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.

Parameters:

Returns:

  • (Boolean)

    whether the snowflakes are equal or not.



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.

Parameters:

Returns:

  • (Boolean)

    whether this snowflake has been generated after another snowflake or not.



68
69
70
# File 'lib/twitter_snowflake/snowflake.rb', line 68

def after?(other)
  @timestamp > other.timestamp
end

#before?(other) ⇒ Boolean

Checks if this snowflake has been generated before another snowflake.

Parameters:

Returns:

  • (Boolean)

    whether this snowflake has been generated before another snowflake or not.



59
60
61
# File 'lib/twitter_snowflake/snowflake.rb', line 59

def before?(other)
  @timestamp < other.timestamp
end