class Statefully::Diff::Builder

Public Class Methods

new(current:, previous:) click to toggle source

Constructor for the {Builder} object

@param current [State] current {State} @param previous [State] previous {State} @api private

# File lib/statefully/diff.rb, line 279
def initialize(current:, previous:)
  @current = current
  @previous = previous
end

Public Instance Methods

build() click to toggle source

Build a Hash of added and changed {State} fields

@return [Hash] @api private

# File lib/statefully/diff.rb, line 288
def build
  empty? ? {} : { added: added, changed: changed }
end

Private Instance Methods

added() click to toggle source

List added fields

@return [Hash] @api private

# File lib/statefully/diff.rb, line 298
def added
  @added ||=
    (current_keys - previous_keys)
    .map { |key| [key, @current.fetch(key)] }
    .to_h
end
change_for(key) click to toggle source

Change for individual key

@param [Symbol] key name

@return [Change] @api private

# File lib/statefully/diff.rb, line 323
def change_for(key)
  Change.new(
    current: @current.fetch(key),
    previous: @previous.fetch(key),
  ).freeze
end
changed() click to toggle source

List changed fields

@return [Hash] @api private

# File lib/statefully/diff.rb, line 309
def changed
  @changed ||=
    (current_keys & previous_keys)
    .map { |key| [key, change_for(key)] }
    .to_h
    .reject { |_, val| val.none? }
end
current_keys() click to toggle source

Return the set of keys for the current {State}

@return [Set<Symbol>] @api private

# File lib/statefully/diff.rb, line 342
def current_keys
  Set.new(@current.keys)
end
empty?() click to toggle source

Check if the nothing has changed

@return [Boolean] @api private

# File lib/statefully/diff.rb, line 334
def empty?
  added.empty? && changed.empty?
end
previous_keys() click to toggle source

Return the set of keys for previous {State}

@return [Set<Symbol>] @api private

# File lib/statefully/diff.rb, line 350
def previous_keys
  Set.new(@previous.keys)
end