Class: Immutability::Object Private

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/immutability/object.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Describes the continuous object as a sequence of immutable snapshots with an option of searching the past state of the object

Author:

Instance Method Summary collapse

Constructor Details

#initialize(current) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes the object from the current state (snapshot)

Parameters:



19
20
21
# File 'lib/immutability/object.rb', line 19

def initialize(current)
  @current = current
end

Instance Method Details

#at(point) ⇒ Object?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the state of the object at some point in the past

Parameters:

  • point (#to_i)

    Either a positive number of target version, or a negative number of version (snapshots) before the current one 0 stands for the first version.

Returns:



55
56
57
58
59
60
61
# File 'lib/immutability/object.rb', line 55

def at(point)
  ipoint = point.to_i
  target = (ipoint < 0) ? (version + ipoint) : ipoint
  return unless (0..version).include? target

  detect { |state| target.equal? state.version }
end

#eachEnumerator

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Iterates via object’s snapshots from the current state to the past

Returns:

  • (Enumerator)


37
38
39
40
41
42
43
44
# File 'lib/immutability/object.rb', line 37

def each
  return to_enum unless block_given?
  state = @current
  while state
    yield(state)
    state = state.parent
  end
end

#versionInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The current (last) version of the object

The object knows nothing about its future

Returns:

  • (Integer)


29
30
31
# File 'lib/immutability/object.rb', line 29

def version
  @current.version
end