class Statefully::Diff

{Diff} is a difference between two neighboring instances of {State}.

@abstract

Public Class Methods

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

Create is the only public interface to the Diff class

@param current [Statefully::State] current state @param previous [Statefully::State] previous state

@return [Statefully::Diff] Difference between states. @api public @example

previous = Statefully::State.create
current = previus.succeed(key: 'val')
Statefully::Diff.create(current, previous)
=> #<Statefully::Diff::Changed added={key: "val"}>

This method reeks of :reek:FeatureEnvy (of current).

# File lib/statefully/diff.rb, line 23
def self.create(current:, previous:)
  return current.diff if current.failed? || current.finished?
  changes = Builder.new(current: current, previous: previous).build
  return Created.new(**changes).freeze if previous.none?
  changes.empty? ? Unchanged.instance : Changed.new(**changes).freeze
end

Public Instance Methods

added() click to toggle source

Hash of added properties and their values

@return [Hash<Symbol, Object>] @api public @example

Statefully::Diff::Unchanged.instance.added
=> {}
# File lib/statefully/diff.rb, line 51
def added
  {}
end
added?(key) click to toggle source

Check if a key has been added

@param key [Symbol] @return [Boolean] @api public @example

diff = Statefully::Diff::Changed.new(added: {key: 7})
diff.added?(:key)
=> true
diff.added?(:other)
=> false
# File lib/statefully/diff.rb, line 77
def added?(key)
  added.key?(key)
end
changed() click to toggle source

Hash of changed properties and their current and previous values

@return [Hash<Symbol, Statefully::Change>] @api public @example

Statefully::Diff::Unchanged.instance.added.changed
=> {}
# File lib/statefully/diff.rb, line 62
def changed
  {}
end
changed?(key) click to toggle source

Check if a key has been changed

@param key [Symbol] @return [Boolean] @api public @example

diff = Statefully::Diff::Changed.new(
  changed: {key: Statefully::Change.new(current: 7, previous: 8)},
)
diff.changed?(:key)
=> true
diff.changed?(:other)
=> false
# File lib/statefully/diff.rb, line 94
def changed?(key)
  changed.key?(key)
end
empty?() click to toggle source

Check if a {Diff} is empty

An empty {Diff} means that is there are no changes in properties between current and previous {State}.

@return [Boolean] @api public @example

Statefully::Diff::Unchanged.instance.empty?
=> true
# File lib/statefully/diff.rb, line 40
def empty?
  true
end