Module: OrderId
- Defined in:
- lib/OrderId.rb,
lib/OrderId/version.rb
Defined Under Namespace
Classes: FormatError
Constant Summary collapse
- DECIMAL_LENGTH =
20- BASE =
36- SEPARATOR =
'-'- GROUP_LENGTH =
5- VERSION =
"0.1.1"
Class Method Summary collapse
- .generate(length: DECIMAL_LENGTH, base: BASE, separator: SEPARATOR, group_length: GROUP_LENGTH) ⇒ Object
- .get_time(id, length: DECIMAL_LENGTH, base: BASE, separator: SEPARATOR) ⇒ Object
Class Method Details
.generate(length: DECIMAL_LENGTH, base: BASE, separator: SEPARATOR, group_length: GROUP_LENGTH) ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/OrderId.rb', line 10 def self.generate(length: DECIMAL_LENGTH, base: BASE, separator: SEPARATOR, group_length: GROUP_LENGTH) if separator =~ (/[\w\d]/) raise FormatError, "Characters not allowed as separator: `#{separator}`" end t = Time.now.to_f ts = "%.#{length}f" % t ts.delete!('.') final = ts.to_i.to_s(base).upcase final.scan(/.{1,#{group_length}}/).join(separator) end |
.get_time(id, length: DECIMAL_LENGTH, base: BASE, separator: SEPARATOR) ⇒ Object
21 22 23 24 25 |
# File 'lib/OrderId.rb', line 21 def self.get_time(id, length: DECIMAL_LENGTH, base: BASE, separator: SEPARATOR) id.delete!(separator) ts = id.to_i(base) / BigDecimal("1e+#{length}") Time.at(ts) end |