Sha256: 16e56ce5de0d098881e9f9dd905eb4b12d2221f7ae14a336cb86cd01948f7a6d

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

module Alf
  module Tools
    #
    # Provides a handle, implementing a flyweight design pattern on tuples.
    #
    class TupleHandle
    
      # Creates an handle instance
      def initialize
        @tuple = nil
      end
    
      #
      # Sets the next tuple to use.
      #
      # This method installs the handle as a side effect 
      # on first call. 
      #
      def set(tuple)
        build(tuple) if @tuple.nil?
        @tuple = tuple
        self
      end
    
      #
      # Evaluates a tuple expression on the current tuple.
      # 
      def evaluate(expr)
        TupleExpression.coerce(expr).evaluate(self)
      end
    
      private
    
      #
      # Builds this handle with a tuple.
      #
      # This method should be called only once and installs 
      # instance methods on the handle with keys of _tuple_.
      #
      def build(tuple)
        tuple.keys.each do |k|
          (class << self; self; end).send(:define_method, k) do
            @tuple[k]
          end
        end
      end
    
    end # class TupleHandle
  end # module Tools
end # module Alf

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
alf-0.10.1 lib/alf/tools/tuple_handle.rb
alf-0.10.0 lib/alf/tools/tuple_handle.rb