Sha256: c33f3a4d4590e3506ecabafaf0d3016290d6cd2eac808f9b5d45f0c92cf757d9

Contents?: true

Size: 1.06 KB

Versions: 4

Compression:

Stored size: 1.06 KB

Contents

module Bmg
  module Operator
    #
    # Transform operator.
    #
    # Transforms existing attributes through computations
    #
    # Example:
    #
    #   [{ a: 1 }] transform { a: ->(t){ t[:a]*2 } } => [{ a: 4 }]
    #
    class Transform
      include Operator::Unary

      DEFAULT_OPTIONS = {}

      def initialize(type, operand, transformation, options = {})
        @type = type
        @operand = operand
        @transformation = transformation
        @options = DEFAULT_OPTIONS.merge(options)
      end

    protected

      attr_reader :transformation

    public

      def each
        t = transformer
        @operand.each do |tuple|
          yield t.call(tuple)
        end
      end

      def to_ast
        [ :transform, operand.to_ast, transformation.dup ]
      end

    protected ### optimization

    protected ### inspect

      def args
        [ transformation ]
      end

    private

      def transformer
        @transformer ||= TupleTransformer.new(transformation)
      end

    end # class Transform
  end # module Operator
end # module Bmg

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
bmg-0.18.0 lib/bmg/operator/transform.rb
bmg-0.17.8 lib/bmg/operator/transform.rb
bmg-0.17.7 lib/bmg/operator/transform.rb
bmg-0.17.6 lib/bmg/operator/transform.rb