Module: Gecode::Constraints::IntEnum::Element::AdditionalEnumMethods

Included in:
FixnumEnumMethods, IntEnumMethods
Defined in:
lib/gecoder/interface/constraints/int_enum/element.rb

Overview

Methods needed to add support for element constraints to enums.

Class Method Summary collapse

Class Method Details

.included(enum_mod) ⇒ Object

This adds the adder for the methods in the modules including it. The reason for doing it so indirect is that the first #[] won’t be defined before the module that this is mixed into is mixed into an enum.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/gecoder/interface/constraints/int_enum/element.rb', line 43

def self.included(enum_mod)
  enum_mod.module_eval do
    # Now we enter the module AdditionalEnumMethods is mixed into.
    class << self
      alias_method :pre_element_included, :included
      def included(mod)
        mod.module_eval do
          # Now we enter the module that the module possibly defining #[] 
          # is mixed into.
          if instance_methods.include? '[]'
            alias_method :pre_element_access, :[]
          end
        
          def [](*vars)
            # Hook in an element constraint if a variable is used for array 
            # access.
            if vars.first.kind_of? Gecode::FreeIntVar
              params = {:lhs => self, :position => vars.first}
              return Gecode::Constraints::IntEnum::Element::ExpressionStub.new(
                @model, params)
            else
              pre_element_access(*vars) if respond_to? :pre_element_access
            end
          end
        end
        pre_element_included(mod)
      end
    end
  end
end