Class: ASTUtils::PartialMap

Inherits:
Object
  • Object
show all
Defined in:
lib/ast_utils/partial_map.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enumerable) ⇒ PartialMap

Returns a new instance of PartialMap.



6
7
8
9
# File 'lib/ast_utils/partial_map.rb', line 6

def initialize(enumerable)
  @enumerable = enumerable
  @updaters = Array.new(enumerable.count)
end

Instance Attribute Details

#enumerableObject (readonly)

Returns the value of attribute enumerable.



3
4
5
# File 'lib/ast_utils/partial_map.rb', line 3

def enumerable
  @enumerable
end

#updatersObject (readonly)

Returns the value of attribute updaters.



4
5
6
# File 'lib/ast_utils/partial_map.rb', line 4

def updaters
  @updaters
end

Class Method Details

.apply(enumerable) {|map| ... } ⇒ Object

Yields:

  • (map)


11
12
13
14
15
# File 'lib/ast_utils/partial_map.rb', line 11

def self.apply(enumerable)
  map = new(enumerable)
  yield map
  map.apply
end

Instance Method Details

#applyObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ast_utils/partial_map.rb', line 29

def apply
  enumerable.map.with_index do |value, index|
    updater = updaters[index]

    if updater
      case updater&.first
      when :on
        updater.last[value]
      when :on!
        raise if value.nil?
        updater.last[value]
      when :on?
        unless value.nil?
          updater.last[value]
        else
          value
        end
      end
    else
      value
    end
  end
end

#on(index, &block) ⇒ Object



17
18
19
# File 'lib/ast_utils/partial_map.rb', line 17

def on(index, &block)
  updaters[index] = [:on, block]
end

#on!(index, &block) ⇒ Object



21
22
23
# File 'lib/ast_utils/partial_map.rb', line 21

def on!(index, &block)
  updaters[index] = [:on!, block]
end

#on?(index, &block) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/ast_utils/partial_map.rb', line 25

def on?(index, &block)
  updaters[index] = [:on?, block]
end