class SuperDiff::OperationTreeBuilders::DefaultObject

Attributes

actual_attributes[R]
expected_attributes[R]

Public Class Methods

applies_to?(_expected, _actual) click to toggle source
# File lib/super_diff/operation_tree_builders/default_object.rb, line 4
def self.applies_to?(_expected, _actual)
  true
end
new(*args) click to toggle source
Calls superclass method
# File lib/super_diff/operation_tree_builders/default_object.rb, line 8
def initialize(*args)
  super(*args)

  establish_expected_and_actual_attributes
end

Protected Instance Methods

attribute_names() click to toggle source
# File lib/super_diff/operation_tree_builders/default_object.rb, line 35
def attribute_names
  (expected.instance_variables.sort & actual.instance_variables.sort).
    map { |variable_name| variable_name[1..-1] }
end
build_operation_tree() click to toggle source
# File lib/super_diff/operation_tree_builders/default_object.rb, line 25
def build_operation_tree
  # XXX This assumes that `expected` and `actual` are the same
  # TODO: Does this need to be find_operation_tree_for?
  OperationTrees::DefaultObject.new([], underlying_object: actual)
end
find_operation_tree_for(value) click to toggle source
# File lib/super_diff/operation_tree_builders/default_object.rb, line 31
def find_operation_tree_for(value)
  OperationTrees::Main.call(value)
end
unary_operations() click to toggle source
# File lib/super_diff/operation_tree_builders/default_object.rb, line 16
def unary_operations
  attribute_names.reduce([]) do |operations, name|
    possibly_add_noop_operation_to(operations, name)
    possibly_add_delete_operation_to(operations, name)
    possibly_add_insert_operation_to(operations, name)
    operations
  end
end

Private Instance Methods

establish_expected_and_actual_attributes() click to toggle source
# File lib/super_diff/operation_tree_builders/default_object.rb, line 44
def establish_expected_and_actual_attributes
  @expected_attributes = attribute_names.reduce({}) do |hash, name|
    hash.merge(name => expected.instance_variable_get("@#{name}"))
  end

  @actual_attributes = attribute_names.reduce({}) do |hash, name|
    hash.merge(name => actual.instance_variable_get("@#{name}"))
  end
end
possibly_add_delete_operation_to(operations, attribute_name) click to toggle source
# File lib/super_diff/operation_tree_builders/default_object.rb, line 72
def possibly_add_delete_operation_to(operations, attribute_name)
  if should_add_delete_operation?(attribute_name)
    operations << Operations::UnaryOperation.new(
      name: :delete,
      collection: expected_attributes,
      key: attribute_name,
      index: attribute_names.index(attribute_name),
      value: expected_attributes[attribute_name],
    )
  end
end
possibly_add_insert_operation_to(operations, attribute_name) click to toggle source
# File lib/super_diff/operation_tree_builders/default_object.rb, line 91
def possibly_add_insert_operation_to(operations, attribute_name)
  if should_add_insert_operation?(attribute_name)
    operations << Operations::UnaryOperation.new(
      name: :insert,
      collection: actual_attributes,
      key: attribute_name,
      index: attribute_names.index(attribute_name),
      value: actual_attributes[attribute_name],
    )
  end
end
possibly_add_noop_operation_to(operations, attribute_name) click to toggle source
# File lib/super_diff/operation_tree_builders/default_object.rb, line 54
def possibly_add_noop_operation_to(operations, attribute_name)
  if should_add_noop_operation?(attribute_name)
    operations << Operations::UnaryOperation.new(
      name: :noop,
      collection: actual_attributes,
      key: attribute_name,
      index: attribute_names.index(attribute_name),
      value: actual_attributes[attribute_name],
    )
  end
end
should_add_delete_operation?(attribute_name) click to toggle source
# File lib/super_diff/operation_tree_builders/default_object.rb, line 84
def should_add_delete_operation?(attribute_name)
  expected_attributes.include?(attribute_name) && (
    !actual_attributes.include?(attribute_name) ||
    expected_attributes[attribute_name] != actual_attributes[attribute_name]
  )
end
should_add_insert_operation?(attribute_name) click to toggle source
# File lib/super_diff/operation_tree_builders/default_object.rb, line 103
def should_add_insert_operation?(attribute_name)
  !expected_attributes.include?(attribute_name) || (
    actual_attributes.include?(attribute_name) &&
    expected_attributes[attribute_name] != actual_attributes[attribute_name]
  )
end
should_add_noop_operation?(attribute_name) click to toggle source
# File lib/super_diff/operation_tree_builders/default_object.rb, line 66
def should_add_noop_operation?(attribute_name)
  expected_attributes.include?(attribute_name) &&
    actual_attributes.include?(attribute_name) &&
    expected_attributes[attribute_name] == actual_attributes[attribute_name]
end