<commit_before>require "view_model/utils/collections"
class ViewModel::Changes
  using ViewModel::Utils::Collections

  attr_reader :new, :changed_attributes, :changed_associations, :deleted

  alias new? new
  alias deleted? deleted

  def initialize(new: false, changed_attributes: [], changed_associations: [], deleted: false)
    @new                  = new
    @changed_attributes   = changed_attributes.map(&:to_s)
    @changed_associations = changed_associations.map(&:to_s)
    @deleted              = deleted
  end

  def contained_to?(associations: [], attributes: [])
    !deleted? &&
      changed_associations.all? { |assoc| associations.include?(assoc.to_s) } &&
      changed_attributes.all? { |attr| attributes.include?(attr.to_s) }
  end

  def ==(other)
    return false unless other.is_a?(ViewModel::Changes)

    self.new? == other.new? &&
      self.changed_attributes.contains_exactly?(other.changed_attributes) &&
      self.changed_associations.contains_exactly?(other.changed_associations) &&
      self.deleted? == other.deleted?
  end

  alias eql? ==
end
<commit_msg>Changes: provide method to check for specific changed associations/attributes.<commit_after>require "view_model/utils/collections"
class ViewModel::Changes
  using ViewModel::Utils::Collections

  attr_reader :new, :changed_attributes, :changed_associations, :deleted

  alias new? new
  alias deleted? deleted

  def initialize(new: false, changed_attributes: [], changed_associations: [], deleted: false)
    @new                  = new
    @changed_attributes   = changed_attributes.map(&:to_s)
    @changed_associations = changed_associations.map(&:to_s)
    @deleted              = deleted
  end

  def contained_to?(associations: [], attributes: [])
    !deleted? &&
      changed_associations.all? { |assoc| associations.include?(assoc.to_s) } &&
      changed_attributes.all? { |attr| attributes.include?(attr.to_s) }
  end

  def changed_any?(associations: [], attributes: [])
    associations.any? { |assoc| changed_associations.include?(assoc.to_s) } ||
      attributes.any? { |attr| changed_attributes.include?(attr.to_s) }
  end

  def ==(other)
    return false unless other.is_a?(ViewModel::Changes)

    self.new? == other.new? &&
      self.changed_attributes.contains_exactly?(other.changed_attributes) &&
      self.changed_associations.contains_exactly?(other.changed_associations) &&
      self.deleted? == other.deleted?
  end

  alias eql? ==
end

{'fraction_non_alphanumeric': 0.12368207623682076, 'fraction_numerical': 0.0, 'mean_word_length': 4.044989775051125, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3187892', 'n_tokens_mistral': 741, 'n_tokens_neox': 747, 'n_words': 138}