Class: Lite::Validation::Validator::Option::Some::Complex::Registry::Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/lite/validation/validator/option/some/complex/registry/abstract.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.insert(node, children) ⇒ Object



17
18
19
20
21
22
# File 'lib/lite/validation/validator/option/some/complex/registry/abstract.rb', line 17

def self.insert(node, children)
  updated = insert_as_child(node, children)
  return updated if updated

  insert_as_parent(node, children)
end

.insert_as_child(node, children) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/lite/validation/validator/option/some/complex/registry/abstract.rb', line 24

def self.insert_as_child(node, children)
  parents, unrelated = children.partition { |child| child.key >= node.key }

  case parents.length
  when 0 then nil
  when 1
    [parents.first.insert(node), *unrelated]
  else
    raise Error, "Multiple parents found for #{node.key}: #{parents.map(&:key).join(', ')}"
  end
end

.insert_as_parent(node, children) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/lite/validation/validator/option/some/complex/registry/abstract.rb', line 36

def self.insert_as_parent(node, children)
  descendants, unrelated = children.partition { |child| child.key < node.key }

  if descendants.empty?
    [*unrelated, node]
  else
    [node.with(children: descendants.freeze), *unrelated]
  end
end

Instance Method Details

#insert(node) ⇒ Object



46
47
48
# File 'lib/lite/validation/validator/option/some/complex/registry/abstract.rb', line 46

def insert(node)
  with(children: self.class.insert(node, children).freeze)
end

#wrapper_for(key) ⇒ Object



50
51
52
53
# File 'lib/lite/validation/validator/option/some/complex/registry/abstract.rb', line 50

def wrapper_for(key)
  provider = children.find { |candidate| candidate.key >= key }
  provider&.wrapper_for(key)
end