Class: Lapidar::Chain

Inherits:
Object
  • Object
show all
Defined in:
lib/lapidar/chain.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeChain

Returns a new instance of Chain.



5
6
7
8
# File 'lib/lapidar/chain.rb', line 5

def initialize
  @block_stacks = []
  @unconsolidated_blocks = []
end

Instance Attribute Details

#block_stacksObject (readonly)

Returns the value of attribute block_stacks.



3
4
5
# File 'lib/lapidar/chain.rb', line 3

def block_stacks
  @block_stacks
end

Instance Method Details

#add(block) ⇒ Object

TODO: Check for duplicates and dont add them to the chains



11
12
13
14
15
16
17
18
# File 'lib/lapidar/chain.rb', line 11

def add(block)
  if valid?(block)
    connect(block)
    consolidate_successors(block)
  else
    queue_unconsolidated(block)
  end
end

#blocksObject

For each position in the chain the candidate positioned first is considered the valid one



21
22
23
# File 'lib/lapidar/chain.rb', line 21

def blocks
  @block_stacks.map { |candidates| candidates&.first }
end

#to_colorful_string(depth = 0) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/lapidar/chain.rb', line 25

def to_colorful_string(depth = 0)
  [*0..depth].map { |level|
    @block_stacks.map { |block_stack|
      if block_stack[level]
        number_display = block_stack[level].number.to_s
        if defined? Paint
          number_display = Paint[number_display, block_stack[level].hash[-6..-1]] # use last hash digits as color
          number_display = Paint[number_display, :bright, :underline] if level == 0 # emphasize preferred chain
          number_display
        end
        number_display
      else
        " " * block_stack[0].number.to_s.length # padding by digit count
      end
    }.join(" ")
  }.join("\n")
end