Class: Accessory::TraversalPosition::EnumerableBeforeOffset

Inherits:
Object
  • Object
show all
Defined in:
lib/accessory/traversal_position/enumerable_before_offset.rb

Overview

Represents the empty intervals between and surrounding the elements of an Enumerable#each traversal.

Examples to build intuition:

  • An EnumerableBeforeOffset with an .offset of 0 represents the position directly before the first result from #each, i.e. “the beginning.” Using Lens#put_in at this position will prepend to the Enumerable.

  • An EnumerableBeforeOffset with an .offset equal to the #length of the Enumerable (recognizable by EnumerableBeforeOffset#last? returning true) represents represents the position directly before the end of the enumeration, i.e. “the end” of the Enumerable. Using Lens#put_in at this position will append to the Enumerable.

  • In general, using Lens#put_in with an EnumerableBeforeOffset with an .offset of n will insert an element between elements n - 1 and n in the enumeration sequence.

  • Returning :pop from Lens#get_and_update_in for an EnumerableBeforeOffset-terminated Lens will have no effect, as you’re removing an empty slice.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#elem_afterObject (readonly)

Returns the element after the cursor, if applicable.

Returns:

  • (Object)

    the element after the cursor, if applicable



48
49
50
# File 'lib/accessory/traversal_position/enumerable_before_offset.rb', line 48

def elem_after
  @elem_after
end

#elem_beforeObject (readonly)

Returns the element before the cursor, if applicable.

Returns:

  • (Object)

    the element before the cursor, if applicable



45
46
47
# File 'lib/accessory/traversal_position/enumerable_before_offset.rb', line 45

def elem_before
  @elem_before
end

#offsetInteger (readonly)

Returns the offset of elem_after in the Enumerable.

Returns:

  • (Integer)

    the offset of elem_after in the Enumerable



42
43
44
# File 'lib/accessory/traversal_position/enumerable_before_offset.rb', line 42

def offset
  @offset
end

Instance Method Details

#first?Boolean

Returns true when #elem_after is the first element of the Enumerable.

Returns:

  • (Boolean)

    true when #elem_after is the first element of the Enumerable



51
# File 'lib/accessory/traversal_position/enumerable_before_offset.rb', line 51

def first?; @is_first; end

#last?Boolean

Returns true when #elem_before is the last element of the Enumerable.

Returns:

  • (Boolean)

    true when #elem_before is the last element of the Enumerable



54
# File 'lib/accessory/traversal_position/enumerable_before_offset.rb', line 54

def last?; @is_last; end