Class: Snowflakey::Snowflake
- Inherits:
-
Object
- Object
- Snowflakey::Snowflake
- Defined in:
- lib/snowflakey/snowflake.rb
Constant Summary collapse
- BASE =
36
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
Instance Method Summary collapse
-
#initialize(prefix, size, time, id, base) ⇒ Snowflake
constructor
A new instance of Snowflake.
- #inspect ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(prefix, size, time, id, base) ⇒ Snowflake
Returns a new instance of Snowflake.
5 6 7 8 9 10 11 |
# File 'lib/snowflakey/snowflake.rb', line 5 def initialize(prefix, size, time, id, base) @prefix = prefix @size = size @time = time @id = id @base = base.to_i end |
Instance Attribute Details
#base ⇒ Object (readonly)
Returns the value of attribute base.
13 14 15 |
# File 'lib/snowflakey/snowflake.rb', line 13 def base @base end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
13 14 15 |
# File 'lib/snowflakey/snowflake.rb', line 13 def id @id end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
13 14 15 |
# File 'lib/snowflakey/snowflake.rb', line 13 def prefix @prefix end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
13 14 15 |
# File 'lib/snowflakey/snowflake.rb', line 13 def size @size end |
#time ⇒ Object (readonly)
Returns the value of attribute time.
13 14 15 |
# File 'lib/snowflakey/snowflake.rb', line 13 def time @time end |
Instance Method Details
#inspect ⇒ Object
23 24 25 |
# File 'lib/snowflakey/snowflake.rb', line 23 def inspect to_s end |
#to_s ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/snowflakey/snowflake.rb', line 14 def to_s t = (@time.to_f * 1e3).round id = t << (@size - 41) id = id | @id % (2 ** (@size - 41)) id = Baseconv.convert(id, from_base: 10, to_base: @base) [*@prefix, id].compact.join("_") end |