Class: Snowflakey::Snowflake

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

Constant Summary collapse

BASE =
36

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#baseObject (readonly)

Returns the value of attribute base.



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

def base
  @base
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#prefixObject (readonly)

Returns the value of attribute prefix.



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

def prefix
  @prefix
end

#sizeObject (readonly)

Returns the value of attribute size.



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

def size
  @size
end

#timeObject (readonly)

Returns the value of attribute time.



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

def time
  @time
end

Instance Method Details

#inspectObject



23
24
25
# File 'lib/snowflakey/snowflake.rb', line 23

def inspect
  to_s
end

#to_sObject



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