class SuperDiff::DiffFormatters::Collection

Constants

ICONS
STYLES

Public Instance Methods

call() click to toggle source
# File lib/super_diff/diff_formatters/collection.rb, line 21
def call
  lines.join("\n")
end

Private Instance Methods

build_chunk_by_inspecting(value, prefix:, icon:) click to toggle source
# File lib/super_diff/diff_formatters/collection.rb, line 96
def build_chunk_by_inspecting(value, prefix:, icon:)
  inspection = SuperDiff.inspect_object(
    value,
    as_single_line: false,
  )
  build_chunk_from_string(inspection, prefix: prefix, icon: icon)
end
build_chunk_for(operation, prefix:, icon:) click to toggle source
# File lib/super_diff/diff_formatters/collection.rb, line 80
def build_chunk_for(operation, prefix:, icon:)
  if operation.value.equal?(operation.collection)
    build_chunk_from_string(
      SuperDiff::RecursionGuard::PLACEHOLDER,
      prefix: build_item_prefix.call(operation),
      icon: icon,
    )
  else
    build_chunk_by_inspecting(
      operation.value,
      prefix: build_item_prefix.call(operation),
      icon: icon,
    )
  end
end
build_chunk_from_string(value, prefix:, icon:) click to toggle source
# File lib/super_diff/diff_formatters/collection.rb, line 104
def build_chunk_from_string(value, prefix:, icon:)
  value.split("\n").
    map.with_index { |line, index|
      [
        icon,
        " ",
        indentation(offset: 1),
        (index == 0 ? prefix : ""),
        line,
      ].join
    }.
    join("\n")
end
comma() click to toggle source
# File lib/super_diff/diff_formatters/collection.rb, line 129
def comma
  if add_comma?
    ","
  else
    ""
  end
end
contents() click to toggle source
# File lib/super_diff/diff_formatters/collection.rb, line 37
def contents
  operation_tree.map do |operation|
    if operation.name == :change
      handle_change_operation(operation)
    else
      handle_non_change_operation(operation)
    end
  end
end
handle_change_operation(operation) click to toggle source
# File lib/super_diff/diff_formatters/collection.rb, line 47
def handle_change_operation(operation)
  SuperDiff::RecursionGuard.guarding_recursion_of(
    operation.left_collection,
    operation.right_collection,
  ) do |already_seen|
    if already_seen
      raise "Infinite recursion!"
    else
      operation.child_operations.to_diff(
        indent_level: indent_level + 1,
        collection_prefix: build_item_prefix.call(operation),
        add_comma: operation.should_add_comma_after_displaying?,
      )
    end
  end
end
handle_non_change_operation(operation) click to toggle source
# File lib/super_diff/diff_formatters/collection.rb, line 64
def handle_non_change_operation(operation)
  icon = ICONS.fetch(operation.name, " ")
  style_name = STYLES.fetch(operation.name, :normal)
  chunk = build_chunk_for(
    operation,
    prefix: build_item_prefix.call(operation),
    icon: icon,
  )

  if operation.should_add_comma_after_displaying?
    chunk << ","
  end

  style_chunk(style_name, chunk)
end
indentation(offset: 0) click to toggle source
# File lib/super_diff/diff_formatters/collection.rb, line 125
def indentation(offset: 0)
  "  " * (indent_level + offset)
end
lines() click to toggle source
# File lib/super_diff/diff_formatters/collection.rb, line 29
def lines
  [
    "  #{indentation}#{collection_prefix}#{open_token}",
    *contents,
    "  #{indentation}#{close_token}#{comma}",
  ]
end
style_chunk(style_name, chunk) click to toggle source
# File lib/super_diff/diff_formatters/collection.rb, line 118
def style_chunk(style_name, chunk)
  chunk.
    split("\n").
    map { |line| Helpers.style(style_name, line) }.
    join("\n")
end