Class: FractionalIndexer::Incrementer

Inherits:
Object
  • Object
show all
Defined in:
lib/fractional_indexer/incrementer.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.execute(order_key) ⇒ Object



5
6
7
# File 'lib/fractional_indexer/incrementer.rb', line 5

def self.execute(order_key)
  new.execute(order_key)
end

Instance Method Details

#execute(order_key) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fractional_indexer/incrementer.rb', line 9

def execute(order_key)
  prefix, *digs = order_key.integer.chars
  carry = true

  (digs.length - 1).downto(0) do |i|
    incremented_index = digits.index(digs[i]) + 1

    if carry_over?(incremented_index)
      digs[i] = digits[0]
    else
      digs[i] = digits[incremented_index]
      carry   = false

      break
    end
  end

  return prefix + digs.join unless carry

  return order_key.class.zero if prefix == "Z"
  return if prefix == "z"

  new_key = (prefix.ord + 1).chr
  new_key > "a" ? digs.push("0") : digs.pop

  new_key + digs.join
end