Sha256: 7b4b6807990e1a1302420ff52926b5a72a118059d1067952c5c5863e9e8505d3

Contents?: true

Size: 1.62 KB

Versions: 5

Compression:

Stored size: 1.62 KB

Contents

module Alf
  module Lang
    module ObjectOriented
      module AlgebraMethods

        def self.def_operator_method(name, clazz)
          define_method(name) do |*args|
            # add self operands at begining of args
            args.unshift(_self_operand)

            # split operands vs. arguments
            operands, arguments = args[0...clazz.arity], args[clazz.arity..-1]

            # build the new expression
            expr = clazz.new(operands, *arguments)

            # let the abstraction have a chance to of decorating it
            _operator_output(expr)
          end
        end

        Algebra::Operator.listen do |name, clazz|
          def_operator_method(name, clazz)
        end

        def +(other)
          union(other)
        end
        alias :| :+

        def -(other)
          minus(other)
        end

        def *(other)
          join(other)
        end

        def &(other)
          intersect(other)
        end

        def =~(other)
          matching(other)
        end

        def !~(other)
          not_matching(other)
        end

        def tuple_extract
          tuple = nil
          each do |t|
            raise NoSuchTupleError if tuple
            tuple = t
          end
          tuple ||= yield if block_given?
          raise NoSuchTupleError unless tuple or block_given?
          Tuple(tuple)
        end
        alias_method :'tuple!', :tuple_extract

      private

        def _self_operand
          self
        end

        def _operator_output(op)
          op
        end

      end # module AlgebraMethods
    end # module ObjectOriented
  end # module Lang
end # module Alf

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
alf-core-0.16.3 lib/alf/lang/oo/algebra_methods.rb
alf-core-0.16.2 lib/alf/lang/oo/algebra_methods.rb
alf-core-0.16.1 lib/alf/lang/oo/algebra_methods.rb
alf-core-0.16.0 lib/alf/lang/oo/algebra_methods.rb
alf-core-0.15.0 lib/alf/lang/oo/algebra_methods.rb