class Delta::SetOperator::Enumerable

Public Class Methods

compatible?(_a, _b) click to toggle source
# File lib/delta/set_operator/enumerable.rb, line 4
def self.compatible?(_a, _b)
  true
end
new(a:, b:, identifiers: nil, changes:) click to toggle source
Calls superclass method Delta::SetOperator::new
# File lib/delta/set_operator/enumerable.rb, line 8
def initialize(a:, b:, identifiers: nil, changes:)
  super

  self.identifiers = identifiers || [:object_id]
  self.a = a.lazy
  self.b = b.lazy
end

Private Instance Methods

attributes(object) click to toggle source
# File lib/delta/set_operator/enumerable.rb, line 51
def attributes(object)
  cache(:attributes, object) do
    changes.map { |m| object.public_send(m) }
  end
end
cache(name, key) { || ... } click to toggle source
# File lib/delta/set_operator/enumerable.rb, line 57
def cache(name, key)
  @cache ||= {}
  @cache[name] ||= {}

  if @cache[name].key?(key)
    @cache[name][key]
  else
    @cache[name][key] = yield
  end
end
identity(object) click to toggle source
# File lib/delta/set_operator/enumerable.rb, line 45
def identity(object)
  cache(:identity, object) do
    identifiers.map { |m| object.public_send(m) }
  end
end
intersect(a, b) click to toggle source
# File lib/delta/set_operator/enumerable.rb, line 22
def intersect(a, b)
  return [] if changes.empty?

  pairs = pairs(a, b).reject do |a_object, b_object|
    attributes(a_object) == attributes(b_object)
  end

  pairs.map { |_, b_object| b_object }
end
other_object(collection, object) click to toggle source
# File lib/delta/set_operator/enumerable.rb, line 37
def other_object(collection, object)
  identity = identity(object)

  collection.find do |other_object|
    identity == identity(other_object)
  end
end
pairs(a, b) click to toggle source
# File lib/delta/set_operator/enumerable.rb, line 32
def pairs(a, b)
  a.map { |a_object| [a_object, other_object(b, a_object)] }
    .select { |_, b_object| b_object }
end
subtract(a, b) click to toggle source
# File lib/delta/set_operator/enumerable.rb, line 18
def subtract(a, b)
  a.reject { |a_object| other_object(b, a_object) }
end