Sha256: 1d9eb3fbbdae7bf6b4184fa9f02182f58aaba8209fca43e9a484e176ddfbe42e

Contents?: true

Size: 1.11 KB

Versions: 38

Compression:

Stored size: 1.11 KB

Contents

require 'arc-furnace/source'

# An unfold is the reverse of a fold--it takes a single value an blows it out
# into an enumeration of values. Useful for splitting up rows into multiple output
# rows and whatnot. Only #unfold is required for implementation.
module ArcFurnace
  class Unfold < Source

    private_attr_reader :source, :unfolded

    def initialize(source:)
      @source = source
      @value = nil
    end

    def value
      if @value.nil? && !empty?
        advance
      end
      @value
    end

    # Given a row from the source, produce the unfolded rows as a result. This method must return
    # an array.
    def unfold(row)
      raise "Unimplemented!"
    end

    def empty?
      @value.nil? && source.empty?
    end

    def advance
      while (unfolded.nil? || unfolded.empty?) && !source.empty?
        # Use reverse since we want to process in-order, but, #pop is much faster than #unshift
        @unfolded = unfold(source.row.deep_dup)
        unfolded.reverse!
      end
      if unfolded && !unfolded.empty?
        @value = unfolded.pop
      else
        @value = nil
      end
    end

  end
end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
arc-furnace-0.1.42 lib/arc-furnace/unfold.rb
arc-furnace-0.1.41 lib/arc-furnace/unfold.rb
arc-furnace-0.1.40 lib/arc-furnace/unfold.rb
arc-furnace-0.1.39 lib/arc-furnace/unfold.rb
arc-furnace-0.1.38 lib/arc-furnace/unfold.rb
arc-furnace-0.1.37 lib/arc-furnace/unfold.rb
arc-furnace-0.1.36 lib/arc-furnace/unfold.rb
arc-furnace-0.1.35 lib/arc-furnace/unfold.rb
arc-furnace-0.1.34 lib/arc-furnace/unfold.rb
arc-furnace-0.1.33 lib/arc-furnace/unfold.rb
arc-furnace-0.1.32 lib/arc-furnace/unfold.rb
arc-furnace-0.1.31 lib/arc-furnace/unfold.rb
arc-furnace-0.1.30 lib/arc-furnace/unfold.rb
arc-furnace-0.1.29 lib/arc-furnace/unfold.rb
arc-furnace-0.1.28 lib/arc-furnace/unfold.rb
arc-furnace-0.1.27 lib/arc-furnace/unfold.rb
arc-furnace-0.1.26 lib/arc-furnace/unfold.rb
arc-furnace-0.1.25 lib/arc-furnace/unfold.rb
arc-furnace-0.1.24 lib/arc-furnace/unfold.rb
arc-furnace-0.1.23 lib/arc-furnace/unfold.rb