class Ellipses::Client::Lines

Constants

Surround

Public Class Methods

new(array_of_lines) click to toggle source
Calls superclass method
# File lib/ellipses/client/lines.rb, line 67
def initialize(array_of_lines)
  super

  @delegate_sd_obj = array_of_lines
end

Private Class Methods

[](object = nil) click to toggle source
# File lib/ellipses/client/lines.rb, line 142
def [](object = nil)
  case object
  when self       then object
  when Array      then new(object)
  when Enumerator then new(object.to_a)
  when NilClass   then new([])
  else                 new([*object])
  end
end

Public Instance Methods

insertion_by_entropy() click to toggle source
# File lib/ellipses/client/lines.rb, line 89
def insertion_by_entropy
  return if empty?

  signature = self[center = index_of_maximum_entropy]
  before    = center
  after     = length - center - 1
  digest    = Support.digest(*self)

  Meta::Insertion.new before: before, after: after, signature: signature, digest: digest
end
insertion_by_surround(i, before: 0, after: 0) click to toggle source
# File lib/ellipses/client/lines.rb, line 80
def insertion_by_surround(i, before: 0, after: 0)
  return unless i < length

  signature = self[i]
  digest    = Support.digest(*self[Surround.new(before: before, after: after).range(i)])

  Meta::Insertion.new before: before, after: after, signature: signature, digest: digest
end
likes(insertion) click to toggle source
# File lib/ellipses/client/lines.rb, line 100
def likes(insertion)
  likes = []

  each_with_index do |line, i|
    next unless line == insertion.signature
    next unless (chunk = surrounding_chunk(i, before: insertion.before, after: insertion.after))
    next unless chunk.insertion == insertion

    likes << chunk
  end

  likes
end
substitute_uniq_like_chunk!(insertion, *lines) click to toggle source
# File lib/ellipses/client/lines.rb, line 121
def substitute_uniq_like_chunk!(insertion, *lines)
  uniq_like_chunk!(insertion).tap do |chunk|
    substitute_within(chunk.origin_range, *lines)
  end
end
surrounding_chunk(i, before: 0, after: 0) click to toggle source
# File lib/ellipses/client/lines.rb, line 73
def surrounding_chunk(i, before: 0, after: 0)
  return unless i < length

  range = (surround = Surround.new(before: before, after: after)).range(i)
  Chunk.new self[range], center: surround.center, origin: range.begin
end
uniq_like_chunk!(insertion) click to toggle source
# File lib/ellipses/client/lines.rb, line 114
def uniq_like_chunk!(insertion)
  raise UnfoundError,   "No chunks found like: #{insertion}"       if likes(insertion).empty?
  raise AmbiguousError, "Multiple chunks found like: #{insertion}" unless (likes = likes(insertion)).size == 1

  likes.first
end

Private Instance Methods

index_of_maximum_entropy() click to toggle source
# File lib/ellipses/client/lines.rb, line 129
def index_of_maximum_entropy
  each_index.max_by { |i| Support.entropy(self[i]) }
end
substitute_at(index, *lines) click to toggle source
# File lib/ellipses/client/lines.rb, line 133
def substitute_at(index, *lines)
  slice!(index).tap { insert(index, *lines) }
end
substitute_within(index_or_range, *lines) click to toggle source
# File lib/ellipses/client/lines.rb, line 137
def substitute_within(index_or_range, *lines)
  slice!(range = Support.to_range(index_or_range)).tap { insert(range.begin, *lines) }
end