Sha256: 903a113be59d13ed00fa5017ad0093f2253834e575a995d9adda58d6596c3016

Contents?: true

Size: 1.49 KB

Versions: 5

Compression:

Stored size: 1.49 KB

Contents

module DeadlySerious
  module Processes
    class Lambda
      def run(block, readers:, writers:)
        params = block.parameters
        writer_param = params.any? { |(k, n)| k == :keyreq && n == :writer }
        reader_param = params.any? { |(k, n)| k == :keyreq && n == :reader }

        reader = JsonIo.new(readers.first) if readers.size == 1 && File.exist?(readers.first.filename)
        writer = JsonIo.new(writers.first) if writers.size == 1 && File.exist?(writers.first.filename)

        if reader_param && writer_param
          unless reader
            fail %(Missing "#{readers.first.filename}", did you provide a reader to lambda?)
          end
          block.call(reader: reader, writer: writer)
        elsif writer_param && reader
          reader.each do |data|
            block.call(*data, writer: writer)
          end
        elsif writer_param && !reader
          block.call(writer: writer)
        elsif reader
          # This is a little "too smarty" for my taste,
          # however, it's awesomely useful. =\
          reader.each do |data|
            result = block.call(*data)
            if result == true # really TRUE, not thruthy
              # Acts as filter
              writer << data
            elsif result == false || result.nil?
              # Acts as filter
              next
            else
              # Acts as transformation
              writer << result
            end
          end
        else
          block.call
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
deadly_serious-1.0.2 lib/deadly_serious/processes/lambda.rb
deadly_serious-1.0.1 lib/deadly_serious/processes/lambda.rb
deadly_serious-1.0.0 lib/deadly_serious/processes/lambda.rb
deadly_serious-1.0.0.rc1 lib/deadly_serious/processes/lambda.rb
deadly_serious-1.0.0.pre lib/deadly_serious/processes/lambda.rb