Module: Depth::Enumeration::Enumerable
- Included in:
- ComplexHash
- Defined in:
- lib/depth/enumeration/enumerable.rb
Instance Method Summary collapse
-
#base ⇒ Object
:nocov:.
- #each(&block) ⇒ Object
-
#each_with_object(object, &block) ⇒ Object
:nocov:.
- #map(&block) ⇒ Object
- #map!(&block) ⇒ Object
- #map_keys(&block) ⇒ Object
- #map_keys!(&block) ⇒ Object
- #map_values(&block) ⇒ Object
- #map_values!(&block) ⇒ Object
- #reduce(memo, &block) ⇒ Object
- #reject(&block) ⇒ Object
- #select(&block) ⇒ Object
Instance Method Details
#base ⇒ Object
:nocov:
5 6 7 |
# File 'lib/depth/enumeration/enumerable.rb', line 5 def base raise NoMethodError.new('should be overridden') end |
#each(&block) ⇒ Object
83 84 85 |
# File 'lib/depth/enumeration/enumerable.rb', line 83 def each(&block) enumerate { |node, route| block.call(node.parent_key, node.fragment, route) } end |
#each_with_object(object, &block) ⇒ Object
:nocov:
10 11 12 13 14 15 16 |
# File 'lib/depth/enumeration/enumerable.rb', line 10 def each_with_object(object, &block) object.tap do |obj| each do |key, fragment, route| block.call(key, fragment, obj, route) end end end |
#map(&block) ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/depth/enumeration/enumerable.rb', line 74 def map(&block) node_map do |node, new_q, route| orig_key = node.parent_key existing = new_q.find(node.route) orig_fragment = existing.nil? ? node.fragment : existing block.call(orig_key, orig_fragment, route) end end |
#map!(&block) ⇒ Object
57 58 59 60 |
# File 'lib/depth/enumeration/enumerable.rb', line 57 def map!(&block) @base = map(&block).base self end |
#map_keys(&block) ⇒ Object
62 63 64 65 66 |
# File 'lib/depth/enumeration/enumerable.rb', line 62 def map_keys(&block) map do |key, fragment, route| [block.call(key, route), fragment] end end |
#map_keys!(&block) ⇒ Object
47 48 49 50 |
# File 'lib/depth/enumeration/enumerable.rb', line 47 def map_keys!(&block) @base = map_keys(&block).base self end |
#map_values(&block) ⇒ Object
68 69 70 71 72 |
# File 'lib/depth/enumeration/enumerable.rb', line 68 def map_values(&block) map do |key, fragment, route| [key, block.call(fragment, route)] end end |
#map_values!(&block) ⇒ Object
52 53 54 55 |
# File 'lib/depth/enumeration/enumerable.rb', line 52 def map_values!(&block) @base = map_values(&block).base self end |
#reduce(memo, &block) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/depth/enumeration/enumerable.rb', line 40 def reduce(memo, &block) each do |key, fragment, route| memo = block.call(memo, key, fragment, route) end memo end |
#reject(&block) ⇒ Object
36 37 38 |
# File 'lib/depth/enumeration/enumerable.rb', line 36 def reject(&block) select{ |key, fragment, route| !block.call(key, fragment, route) } end |
#select(&block) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/depth/enumeration/enumerable.rb', line 18 def select(&block) new_q = self.class.new(base.class.new) routes_to_delete = [] enumerate do |node, route| key = node.parent_key existing = new_q.find(node.route) fragment = existing.nil? ? node.fragment : existing keep = block.call(key, fragment, route) if keep new_q.alter(node.route, key: key, value: fragment) else routes_to_delete << node.route end end routes_to_delete.each { |r| new_q.delete(r) } new_q end |