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.22 lib/arc-furnace/unfold.rb
arc-furnace-0.1.21 lib/arc-furnace/unfold.rb
arc-furnace-0.1.20 lib/arc-furnace/unfold.rb
arc-furnace-0.1.19 lib/arc-furnace/unfold.rb
arc-furnace-0.1.18 lib/arc-furnace/unfold.rb
arc-furnace-0.1.16 lib/arc-furnace/unfold.rb
arc-furnace-0.1.15 lib/arc-furnace/unfold.rb
arc-furnace-0.1.14 lib/arc-furnace/unfold.rb
arc-furnace-0.1.13 lib/arc-furnace/unfold.rb
arc-furnace-0.1.12 lib/arc-furnace/unfold.rb
arc-furnace-0.1.11 lib/arc-furnace/unfold.rb
arc-furnace-0.1.10 lib/arc-furnace/unfold.rb
arc-furnace-0.1.9 lib/arc-furnace/unfold.rb
arc-furnace-0.1.8 lib/arc-furnace/unfold.rb
arc-furnace-0.1.7 lib/arc-furnace/unfold.rb
arc-furnace-0.1.6 lib/arc-furnace/unfold.rb
arc-furnace-0.1.5 lib/arc-furnace/unfold.rb
arc-furnace-0.1.4 lib/arc-furnace/unfold.rb