Sha256: cb1d5f46483383bd9fb3c42c55393c8392b26a2cd82aca163b545c0dff9de54e

Contents?: true

Size: 1.19 KB

Versions: 5

Compression:

Stored size: 1.19 KB

Contents

module Alf
  module Engine
    #
    # Set computed tuples attributes. This cog allows implementing both
    # EXTEND and UPDATE relational operators.
    # 
    # Example:
    #
    #     rel = [
    #       {:name => "Jones", :city => "London"}
    #     ]
    #     comp = TupleComputation[
    #       :city   => lambda{ city.upcase },
    #       :concat => lambda{ "#{name} #{city}" }
    #     ]
    #     SetAttr.new(rel, comp).to_a
    #     # => [
    #     #      {:name => "Jones", :city => "LONDON", :concat => "Jones LONDON"}
    #     #    ]
    #
    class SetAttr < Cog

      # @return [Enumerable] The operand
      attr_reader :operand

      # @return [TupleComputation] Computed attributes as a computation
      attr_reader :computation

      # Creates a SetAttr instance
      def initialize(operand, computation)
        @operand = operand
        @computation = computation
      end

      # (see Cog#each)
      def each
        handle = Tools::TupleHandle.new
        operand.each do |tuple|
          computed = @computation.evaluate(handle.set(tuple))
          yield tuple.merge(computed){|k,v1,v2| v2}
        end
      end

    end # class SetAttr
  end # module Engine
end # module Alf

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
alf-0.12.2 lib/alf-engine/alf/engine/set_attr.rb
alf-0.12.1 lib/alf-engine/alf/engine/set_attr.rb
alf-0.12.0 lib/alf-engine/alf/engine/set_attr.rb
alf-0.11.1 lib/alf-engine/alf/engine/set_attr.rb
alf-0.11.0 lib/alf-engine/alf/engine/set_attr.rb