Sha256: 97153a8d0523cc0453250e908e56d04b29f01d946a11b1c7b65f6a11a8accda8

Contents?: true

Size: 1.05 KB

Versions: 5

Compression:

Stored size: 1.05 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 < Cog

      # @return [Enumerable] The operand
      attr_reader :operand

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

      # Creates a Defaults instance
      def initialize(operand, defaults)
        @operand = operand
        @defaults = defaults
      end

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

    end # class Defaults
  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/defaults.rb
alf-0.12.1 lib/alf-engine/alf/engine/defaults.rb
alf-0.12.0 lib/alf-engine/alf/engine/defaults.rb
alf-0.11.1 lib/alf-engine/alf/engine/defaults.rb
alf-0.11.0 lib/alf-engine/alf/engine/defaults.rb