Sha256: 0bd8a52f7e29804a90864c189d45bb8e6e31d5cfc2f100feae2e2b05f8cb12e3

Contents?: true

Size: 1.17 KB

Versions: 5

Compression:

Stored size: 1.17 KB

Contents

module Alf
  module Engine
    #
    # Replace missing attributes and nil by default, computed values.
    #
    # Example:
    #
    #     rel = [
    #       {:id => 1, :name => "Jones"},
    #       {:id => 2, :name => nil}
    #     ]
    #     Defaults.new(rel, TupleComputation[:name => "Smith"]).to_a
    #     # => [
    #     #      {:id => 1, :name => "Jones"},
    #     #      {:id => 2, :name => "Smith"}
    #     #    ]
    #
    class Defaults
      include Cog

      # @return [Enumerable] The operand
      attr_reader :operand

      # @return [TupleComputation] The default values
      attr_reader :defaults

      # Creates a Defaults instance
      def initialize(operand, defaults, expr = nil, compiler = nil)
        super(expr, compiler)
        @operand = operand
        @defaults = defaults
      end

      # (see Cog#each)
      def _each
        scope = tuple_scope
        operand.each do |tuple|
          defs = @defaults.evaluate(scope.__set_tuple(tuple))
          yield tuple.merge(defs){|k,v1,v2| (v1.nil? ? v2 : v1)}
        end
      end

      def arguments
        [ defaults ]
      end

    end # class Defaults
  end # module Engine
end # module Alf

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
alf-core-0.16.3 lib/alf/engine/defaults.rb
alf-core-0.16.2 lib/alf/engine/defaults.rb
alf-core-0.16.1 lib/alf/engine/defaults.rb
alf-core-0.16.0 lib/alf/engine/defaults.rb
alf-core-0.15.0 lib/alf/engine/defaults.rb