Sha256: 0c1d5c2d2466b47d116f710663ab0af4a1eb3fc3a1272bc4e7d092d964bc4e9a
Contents?: true
Size: 1.65 KB
Versions: 2
Compression:
Stored size: 1.65 KB
Contents
module Depth class Traverser attr_reader :object def initialize(object, key_transformer:, next_proc:, creation_proc:) @object = object @next_proc = next_proc @creation_proc = creation_proc @key_transformer = key_transformer end def array? object.is_a?(Array) end def hash? object.is_a?(Hash) end def next(key_or_index) original_key = key_or_index key_or_index = key_transformer.call(object, key_or_index) next_object = if object.nil? nil else next_proc.call(object, key_or_index, original_key) end Traverser.new( next_object, key_transformer: key_transformer, next_proc: next_proc, creation_proc: creation_proc ) end def next_or_create(key_or_index, &block) original_key = key_or_index key_or_index = key_transformer.call(object, key_or_index) return Traverser.new( nil, key_transformer: key_transformer, next_proc: next_proc, creation_proc: creation_proc ) if object.nil? next_object = next_proc.call(object, key_or_index, original_key) creation_proc.call(object, key_or_index, block.call, original_key) if next_object.nil? Traverser.new( # Refetch it to allow for creation next_proc.call(object, key_or_index, original_key), key_transformer: key_transformer, next_proc: next_proc, creation_proc: creation_proc ) end private attr_reader :next_proc, :creation_proc, :key_transformer end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
depth-0.4.1 | lib/depth/traverser.rb |
depth-0.4.0 | lib/depth/traverser.rb |