Class: ASTUtils::PartialMap
- Inherits:
-
Object
- Object
- ASTUtils::PartialMap
- Defined in:
- lib/ast_utils/partial_map.rb
Instance Attribute Summary collapse
-
#enumerable ⇒ Object
readonly
Returns the value of attribute enumerable.
-
#updaters ⇒ Object
readonly
Returns the value of attribute updaters.
Class Method Summary collapse
Instance Method Summary collapse
- #apply ⇒ Object
-
#initialize(enumerable) ⇒ PartialMap
constructor
A new instance of PartialMap.
- #on(index, &block) ⇒ Object
- #on!(index, &block) ⇒ Object
- #on?(index, &block) ⇒ Boolean
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
#enumerable ⇒ Object (readonly)
Returns the value of attribute enumerable.
3 4 5 |
# File 'lib/ast_utils/partial_map.rb', line 3 def enumerable @enumerable end |
#updaters ⇒ Object (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
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
#apply ⇒ Object
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
25 26 27 |
# File 'lib/ast_utils/partial_map.rb', line 25 def on?(index, &block) updaters[index] = [:on?, block] end |