class Micro::Attributes::Diff::Changes

Constants

FROM_TO_ERROR
FROM_TO_STR
FROM_TO_SYM

Attributes

differences[R]
from[R]
to[R]

Public Class Methods

new(from:, to:) click to toggle source
# File lib/micro/attributes/diff.rb, line 12
def initialize(from:, to:)
  @from_class = from.class

  @from, @to = from, Kind.of(@from_class, to)

  @from_key, @to_key =
    @from_class.attributes_access == :symbol ? FROM_TO_SYM : FROM_TO_STR

  @differences = diff(from.attributes, to.attributes).freeze
end

Public Instance Methods

blank?()
Alias for: empty?
changed?(name = nil, from: nil, to: nil) click to toggle source
# File lib/micro/attributes/diff.rb, line 32
def changed?(name = nil, from: nil, to: nil)
  if name.nil?
    return present? if from.nil? && to.nil?

    raise ArgumentError, FROM_TO_ERROR
  elsif from.nil? && to.nil?
    differences.has_key?(key_transform(name))
  else
    result = @differences[key_transform(name)]
    result ? result[@from_key] == from && result[@to_key] == to : false
  end
end
empty?() click to toggle source
# File lib/micro/attributes/diff.rb, line 23
def empty?
  @differences.empty?
end
Also aliased as: blank?
present?() click to toggle source
# File lib/micro/attributes/diff.rb, line 28
def present?
  !empty?
end

Private Instance Methods

diff(from_attributes, to_attributes) click to toggle source
# File lib/micro/attributes/diff.rb, line 51
def diff(from_attributes, to_attributes)
  @from_attributes, @to_attributes = from_attributes, to_attributes

  @from_attributes.each_with_object({}) do |(from_key, from_val), acc|
    to_value = @to_attributes[from_key]

    acc[from_key] = {@from_key => from_val, @to_key => to_value}.freeze if from_val != to_value
  end
end
key_transform(key) click to toggle source
# File lib/micro/attributes/diff.rb, line 47
def key_transform(key)
  @from_class.__attribute_key_transform__(key)
end