class Floss::Log::Simple

Attributes

entries[RW]

@return [Array<Entry>] The log's entries.

Public Class Methods

new(options={}) click to toggle source
# File lib/floss/log/simple.rb, line 16
def initialize(options={})
  self.entries = []
end

Public Instance Methods

append(new_entries) click to toggle source

@param [Array] The entries to append to the log.

# File lib/floss/log/simple.rb, line 21
def append(new_entries)
  raise ArgumentError, 'The passed array is empty.' if new_entries.empty?

  entries.concat(new_entries)
  last_index
end
last_index() click to toggle source

Returns the last index in the log or nil if the log is empty.

# File lib/floss/log/simple.rb, line 33
def last_index
  entries.any? ? entries.size - 1 : nil
end
last_term() click to toggle source

Returns the term of the last entry in the log or nil if the log is empty.

# File lib/floss/log/simple.rb, line 38
def last_term
  entry = entries.last
  entry ? entry.term : nil
end
remove_starting_with(index) click to toggle source
# File lib/floss/log/simple.rb, line 51
def remove_starting_with(index)
  entries.slice!(index..-1)
end
starting_with(index) click to toggle source
# File lib/floss/log/simple.rb, line 28
def starting_with(index)
  entries[index..-1]
end
validate(index, term) click to toggle source
# File lib/floss/log/simple.rb, line 43
def validate(index, term)
  # Special case: Accept the first entry if the log is empty.
  return empty? if index.nil? && term.nil?

  entry = entries[index]
  entry && entry.term == term
end