Class: Statefully::Diff Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/statefully/diff.rb

Overview

This class is abstract.

Diff is a difference between two neighboring instances of State.

Direct Known Subclasses

Changed, Failed, Finished, Unchanged

Defined Under Namespace

Classes: Changed, Created, Failed, Finished, Unchanged

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create(current:, previous:) ⇒ Statefully::Diff

Create is the only public interface to the Diff class

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

Examples:

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

23
24
25
26
27
28
# 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

Instance Method Details

#addedHash<Symbol, Object>

Hash of added properties and their values

Examples:

Statefully::Diff::Unchanged.instance.added
=> {}

51
52
53
# File 'lib/statefully/diff.rb', line 51

def added
  {}
end

#added?(key) ⇒ Boolean

Check if a key has been added

Examples:

diff = Statefully::Diff::Changed.new(added: {key: 7})
diff.added?(:key)
=> true
diff.added?(:other)
=> false

77
78
79
# File 'lib/statefully/diff.rb', line 77

def added?(key)
  added.key?(key)
end

#changedHash<Symbol, Statefully::Diff::Change>

Hash of changed properties and their current and previous values

Examples:

Statefully::Diff::Unchanged.instance.added.changed
=> {}

62
63
64
# File 'lib/statefully/diff.rb', line 62

def changed
  {}
end

#changed?(key) ⇒ Boolean

Check if a key has been changed

Examples:

diff = Statefully::Diff::Changed.new(
  changed: {key: Statefully::Change.new(current: 7, previous: 8)},
)
diff.changed?(:key)
=> true
diff.changed?(:other)
=> false

94
95
96
# File 'lib/statefully/diff.rb', line 94

def changed?(key)
  changed.key?(key)
end

#empty?Boolean

Check if a Statefully::Diff is empty

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

Examples:

Statefully::Diff::Unchanged.instance.empty?
=> true

40
41
42
# File 'lib/statefully/diff.rb', line 40

def empty?
  true
end