Sha256: 36d73c0b6f606e677c075aa8cdbebc63a1ae510507ec76e4388724f905dfb184

Contents?: true

Size: 499 Bytes

Versions: 3

Compression:

Stored size: 499 Bytes

Contents

module Depth
  class Traverser < Struct.new(:object)
    def array?
      object.is_a?(Array)
    end

    def hash?
      object.is_a?(Hash)
    end

    def next(key_or_index)
      return Traverser.new(nil) if object.nil? 
      Traverser.new(object[key_or_index])
    end

    def next_or_create(key_or_index, &block)
      return Traverser.new(nil) if object.nil? 
      object[key_or_index] = block.call if object[key_or_index].nil?
      Traverser.new(object[key_or_index])
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
depth-0.1.0 lib/depth/traverser.rb
depth-0.0.2 lib/depth/traverser.rb
depth-0.0.1 lib/depth/traverser.rb