Class: FractionalIndexer::OrderKey

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

Constant Summary collapse

INTEGER_BASE_DIGIT =
2
POSITIVE_SIGNS =
("a".."z").freeze
NEGATIVE_SIGNS =
("A".."Z").freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ OrderKey

Returns a new instance of OrderKey.



11
12
13
# File 'lib/fractional_indexer/order_key.rb', line 11

def initialize(key)
  @key = key
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



9
10
11
# File 'lib/fractional_indexer/order_key.rb', line 9

def key
  @key
end

Class Method Details

.negative?(key) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/fractional_indexer/order_key.rb', line 15

def self.negative?(key)
  NEGATIVE_SIGNS.cover?(key[0])
end

.positive?(key) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/fractional_indexer/order_key.rb', line 19

def self.positive?(key)
  POSITIVE_SIGNS.cover?(key[0])
end

.zeroObject



23
24
25
# File 'lib/fractional_indexer/order_key.rb', line 23

def self.zero
  "a#{FractionalIndexer.configuration.digits.first}"
end

Instance Method Details

#decrementObject



27
28
29
30
31
32
# File 'lib/fractional_indexer/order_key.rb', line 27

def decrement
  new_order_key = Decrementer.execute(self)
  raise_error("it cannot decrement for min integer") if new_order_key.nil?

  new_order_key
end

#fractionalObject



34
35
36
37
38
# File 'lib/fractional_indexer/order_key.rb', line 34

def fractional
  validate!

  key[integer_digits..]
end

#incrementObject



40
41
42
43
44
45
# File 'lib/fractional_indexer/order_key.rb', line 40

def increment
  new_order_key = Incrementer.execute(self)
  raise_error("it cannot increment for max integer") if new_order_key.nil?

  new_order_key
end

#integerObject



47
48
49
50
51
# File 'lib/fractional_indexer/order_key.rb', line 47

def integer
  validate!

  key[0, integer_digits]
end

#maximum_integer?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/fractional_indexer/order_key.rb', line 53

def maximum_integer?
  integer == maximum_integer
end

#minimum?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/fractional_indexer/order_key.rb', line 61

def minimum?
  minimum_integer? && fractional.empty?
end

#minimum_integer?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/fractional_indexer/order_key.rb', line 57

def minimum_integer?
  integer == minimum_integer
end

#present?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/fractional_indexer/order_key.rb', line 65

def present?
  !(key.nil? || key.empty?)
end