Sha256: ce8b4f4c72f1ade624e979e462bac577c0f7c0bc8436a1fbf667730eb4b640d7

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

require_relative './composition'

module Draught
  module Transformations
    class Composer
      def self.compose(*transforms)
        new(transforms).composition
      end

      attr_reader :transforms

      def initialize(transforms)
        @transforms = transforms
      end

      def composition
        Composition.new(coalesced_transforms)
      end

      def coalesced_transforms
        return [] if transforms.empty?
        start_transforms = flattened_transforms
        finished = start_transforms.shift(1)
        return finished if start_transforms.empty?

        start_transforms.each do |next_transform|
          coalesce_pair(finished.pop, next_transform).each do |coalesced_transform|
            finished << coalesced_transform
          end
        end
        finished
      end

      def flattened_transforms
        transforms.flat_map { |transform|
          transform.to_transform.transforms
        }
      end

      private

      def coalesce_pair(first, second)
        begin
          [first.coalesce(second)]
        rescue TypeError
          [first, second]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
draught-0.1.0 lib/draught/transformations/composer.rb