Module: Modernize::MapMethods
- Defined in:
- lib/modernizer/map_methods.rb
Class Method Summary collapse
-
.add(struct, field, block) ⇒ Object
Adds a field based on the result of the block if the field doesn’t already exist.
-
.compute(struct, field, block) ⇒ Object
Adds or updates a field passing the current value (if any) as a parameter to the block.
-
.map(struct, field, block) ⇒ Object
Maps an existing field passing the current value as a parameter to the block.
-
.remove(struct, field, block) ⇒ Object
Removes a field.
Class Method Details
.add(struct, field, block) ⇒ Object
Adds a field based on the result of the block if the field doesn’t already exist.
7 8 9 10 |
# File 'lib/modernizer/map_methods.rb', line 7 def add(struct, field, block) h = struct.hash h[field] = struct.instance_exec(&block) if h[field].nil? end |
.compute(struct, field, block) ⇒ Object
Adds or updates a field passing the current value (if any) as a parameter to the block.
29 30 31 32 |
# File 'lib/modernizer/map_methods.rb', line 29 def compute(struct, field, block) h = struct.hash h[field] = struct.instance_exec(h[field], &block) end |
.map(struct, field, block) ⇒ Object
Maps an existing field passing the current value as a parameter to the block.
21 22 23 24 |
# File 'lib/modernizer/map_methods.rb', line 21 def map(struct, field, block) h = struct.hash h[field] = struct.instance_exec(h[field], &block) unless h[field].nil? end |
.remove(struct, field, block) ⇒ Object
Removes a field.
14 15 16 |
# File 'lib/modernizer/map_methods.rb', line 14 def remove(struct, field, block) struct.hash.delete(field) end |