Class: Accessory::Accessors::AllAccessor

Inherits:
Accessory::Accessor show all
Defined in:
lib/accessory/accessors/all_accessor.rb

Overview

Traverses all elements of an Enumerable.

Aliases

Equivalents in Elixir’s Access module

Default constructor used by predecessor accessor

  • Array.new

Instance Method Summary collapse

Methods inherited from Accessory::Accessor

#traverse, #traverse_or_default

Instance Method Details

#get(data, &succ) ⇒ Array

Feeds each element of data down the accessor chain, and returns the results.

Parameters:

  • data (Enumerable)

    the Enumerable to iterate through

Returns:

  • (Array)

    the values derived from the rest of the accessor chain



35
36
37
38
39
40
41
# File 'lib/accessory/accessors/all_accessor.rb', line 35

def get(data, &succ)
  if succ
    (data || []).map(&succ)
  else
    data
  end
end

#get_and_update(data) ⇒ Array

Feeds each element of data down the accessor chain, overwriting data with the results.

If :pop is returned from the accessor chain, the element is dropped from the new data.

Parameters:

  • data (Enumerable)

    the Enumerable to iterate through

Returns:

  • (Array)

    a two-element array containing 1. the original values found during iteration; and 2. the new data



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/accessory/accessors/all_accessor.rb', line 50

def get_and_update(data)
  results = []
  new_data = []
  dirty = false

  (data || []).each do |pos|
    case yield(pos)
    in [:clean, result, _]
      results.push(result)
      new_data.push(pos)
      # ok
    in [:dirty, result, new_value]
      results.push(result)
      new_data.push(new_value)
      dirty = true
    in :pop
      results.push(pos)
      dirty = true
    end
  end

  if dirty
    [:dirty, results, new_data]
  else
    [:clean, results, data]
  end
end