Module: Plutonium::Positioning

Included in:
Definition::Base
Defined in:
lib/plutonium/positioning.rb,
lib/plutonium/positioning/model.rb,
lib/plutonium/positioning/config.rb

Overview

This file is deliberately dependency-free — no ActiveSupport, no ActiveRecord, not even the rest of Plutonium::Positioning. A controller, a definition DSL and a view component all need to name a strategy, and none of them should have to drag the model concern in to do it.

Defined Under Namespace

Modules: MigrationHelpers, Model Classes: Config, Move, Result

Constant Summary collapse

EPSILON =
1e-6

Class Method Summary collapse

Class Method Details

.gap_exhausted?(prev_val, next_val) ⇒ Boolean

Returns true when prev_val and next_val are so close together that inserting a new midpoint would produce a duplicate.

Returns:

  • (Boolean)


54
55
56
57
# File 'lib/plutonium/positioning.rb', line 54

def self.gap_exhausted?(prev_val, next_val)
  return false if prev_val.nil? || next_val.nil?
  (next_val - prev_val).abs < EPSILON
end

.position_between(prev_val, next_val) ⇒ Object

Returns the position that sits between prev_val and next_val.

Rules:

both nil  → 0.0            (first item in an empty list)
prev nil  → next_val - 1   (prepend)
next nil  → prev_val + 1   (append)
else      → midpoint


45
46
47
48
49
50
# File 'lib/plutonium/positioning.rb', line 45

def self.position_between(prev_val, next_val)
  return 0.0 if prev_val.nil? && next_val.nil?
  return next_val - 1 if prev_val.nil?
  return prev_val + 1 if next_val.nil?
  (prev_val + next_val) / 2.0
end