Class: Statefully::Diff::Changed

Inherits:
Statefully::Diff show all
Defined in:
lib/statefully/diff.rb

Overview

Changed is a Statefully::Diff which contains changes between two successful States.

Direct Known Subclasses

Created

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Statefully::Diff

#added?, #changed?, create

Constructor Details

#initialize(added: {}, changed: {}) ⇒ Changed

Constructor for Statefully::Diff::Changed

Examples:

Statefully::Diff::Changed.new(added: {key: 7})
=> #<Statefully::Diff::Changed added={key: 7}>

Parameters:

  • added (Hash<Symbol, Object>) (defaults to: {})

    added fields

  • changed (Hash<Symbol, Change>) (defaults to: {})
    changed fields


129
130
131
132
# File 'lib/statefully/diff.rb', line 129

def initialize(added: {}, changed: {})
  @added = added.freeze
  @changed = changed.freeze
end

Instance Attribute Details

#addedHash<Symbol, Object> (readonly)

Hash of added properties and their values

Examples:

Statefully::Diff::Changed.new(added: {key: 7}).added
=> {:key => 7}

Returns:

  • (Hash<Symbol, Object>)


108
109
110
# File 'lib/statefully/diff.rb', line 108

def added
  @added
end

#changedHash<Symbol, Change> (readonly)

Hash of changed properties and their current and previous values

Examples:

Statefully::Diff::Changed.new(
  changed: {key: Statefully::Change.new(current: 7, previous: 8)},
)
=> {:key=>#<Statefully::Change current=7, previous=8>}

Returns:



119
120
121
# File 'lib/statefully/diff.rb', line 119

def changed
  @changed
end

Instance Method Details

#created?Boolean

Check if a Statefully::Diff resulted from creating a State

Examples:

Stateful::State.created.created?
=> true

Stateful::State.created.succeed.created?
=> false

Returns:

  • (Boolean)


144
145
146
# File 'lib/statefully/diff.rb', line 144

def created?
  false
end

#empty?Boolean

Check if a Statefully::Diff is empty

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

Examples:

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

Returns:

  • (Boolean)


158
159
160
# File 'lib/statefully/diff.rb', line 158

def empty?
  added.empty? && changed.empty?
end

#inspectString

Human-readable representation of the Change for console inspection

Examples:

Statefully::Diff::Changed.new(added: {key: 7})
=> #<Statefully::Diff::Changed added={key: 7}>

Returns:

  • (String)


169
170
171
172
173
# File 'lib/statefully/diff.rb', line 169

def inspect
  details = [self.class.name]
  details << inspect_details unless empty?
  "#<#{details.join(' ')}>"
end