lib/utilrb/models/inherited_enumerable.rb
def define_inherited_enumerable_map_with_promotion(name, attribute_name, options)
        class_eval <<-EOF, __FILE__, __LINE__+1
        def each_#{name}(key = nil, uniq = true)
            if !block_given?
                return enum_for(:each_#{name}, key, uniq)
            end

            ancestors = self.ancestors
            if ancestors.first != self
                ancestors.unshift self
            end
            if key
                promotions = []
                for klass in ancestors
                    if klass.instance_variable_defined?(:@#{attribute_name})
                        if klass.#{attribute_name}.has_key?(key)
                            value = klass.#{attribute_name}[key]
                            for p in promotions
                                value = p.promote_#{name}(key, value)
                            end
                            yield(value)
                            return self if uniq
                        end
                    end
                    promotions.unshift(klass) if klass.respond_to?("promote_#{name}")
                end
            elsif !uniq
                promotions = []
                for klass in ancestors
                    if klass.instance_variable_defined?(:@#{attribute_name})
                        klass.#{attribute_name}.#{options[:enum_with]} do |key, value|
                            for p in promotions
                                value = p.promote_#{name}(key, value)
                            end
                            yield(key, value)
                        end
                    end
                    promotions.unshift(klass) if klass.respond_to?("promote_#{name}")
                end
            else
                seen = Set.new
                promotions = []
                for klass in ancestors
                    if klass.instance_variable_defined?(:@#{attribute_name})
                        klass.#{attribute_name}.#{options[:enum_with]} do |key, value|
                            unless seen.include?(key)
                                for p in promotions
                                    value = p.promote_#{name}(key, value)
                                end
                                seen << key
                                yield(key, value)
                            end
                        end
                    end
                    promotions.unshift(klass) if klass.respond_to?("promote_#{name}")
                end
            end
            self
        end
        EOF
    end
# Helper method for inherited_enumerable
#
# It is called in the context of the singleton class of the module/class on
# which inherited_enumerable is called
Helper method for inherited_enumerable
It is called in the context of the singleton class of the module/class on
which inherited_enumerable is called
Helper method for inherited_enumerable It is called in the context of the singleton class of the module / class on which inherited_enumerable is called
def define_inherited_enumerable_map_with_promotion(name, attribute_name, options)
        class_eval <<-EOF, __FILE__, __LINE__+1
        def each_#{name}(key = nil, uniq = true)
            if !block_given?
                return enum_for(:each_#{name}, key, uniq)
            end
            ancestors = self.ancestors
            if ancestors.first != self
                ancestors.unshift self
            end
            if key
                promotions = []
                for klass in ancestors
                    if klass.instance_variable_defined?(:@#{attribute_name})
                        if klass.#{attribute_name}.has_key?(key)
                            value = klass.#{attribute_name}[key]
                            for p in promotions
                                value = p.promote_#{name}(key, value)
                            end
                            yield(value)
                            return self if uniq
                        end
                    end
                    promotions.unshift(klass) if klass.respond_to?("promote_#{name}")
                end
            elsif !uniq
                promotions = []
                for klass in ancestors
                    if klass.instance_variable_defined?(:@#{attribute_name})
                        klass.#{attribute_name}.#{options[:enum_with]} do |key, value|
                            for p in promotions
                                value = p.promote_#{name}(key, value)
                            end
                            yield(key, value)
                        end
                    end
                    promotions.unshift(klass) if klass.respond_to?("promote_#{name}")
                end
            else
                seen = Set.new
                promotions = []
                for klass in ancestors
                    if klass.instance_variable_defined?(:@#{attribute_name})
                        klass.#{attribute_name}.#{options[:enum_with]} do |key, value|
                            unless seen.include?(key)
                                for p in promotions
                                    value = p.promote_#{name}(key, value)
                                end
                                seen << key
                                yield(key, value)
                            end
                        end
                    end
                    promotions.unshift(klass) if klass.respond_to?("promote_#{name}")
                end
            end
            self
        end
        EOF
    end
def define_inherited_enumerable_map_with_promotion ( name , attribute_name , options ) class_eval <<-EOF , __FILE__ , __LINE__ + 1 
        def each_ #{ name } (key = nil, uniq = true)
            if !block_given?
                return enum_for(:each_ #{ name } , key, uniq)
            end

            ancestors = self.ancestors
            if ancestors.first != self
                ancestors.unshift self
            end
            if key
                promotions = []
                for klass in ancestors
                    if klass.instance_variable_defined?(:@ #{ attribute_name } )
                        if klass. #{ attribute_name } .has_key?(key)
                            value = klass. #{ attribute_name } [key]
                            for p in promotions
                                value = p.promote_ #{ name } (key, value)
                            end
                            yield(value)
                            return self if uniq
                        end
                    end
                    promotions.unshift(klass) if klass.respond_to?("promote_ #{ name } ")
                end
            elsif !uniq
                promotions = []
                for klass in ancestors
                    if klass.instance_variable_defined?(:@ #{ attribute_name } )
                        klass. #{ attribute_name } . #{ options [ :enum_with ] }  do |key, value|
                            for p in promotions
                                value = p.promote_ #{ name } (key, value)
                            end
                            yield(key, value)
                        end
                    end
                    promotions.unshift(klass) if klass.respond_to?("promote_ #{ name } ")
                end
            else
                seen = Set.new
                promotions = []
                for klass in ancestors
                    if klass.instance_variable_defined?(:@ #{ attribute_name } )
                        klass. #{ attribute_name } . #{ options [ :enum_with ] }  do |key, value|
                            unless seen.include?(key)
                                for p in promotions
                                    value = p.promote_ #{ name } (key, value)
                                end
                                seen << key
                                yield(key, value)
                            end
                        end
                    end
                    promotions.unshift(klass) if klass.respond_to?("promote_ #{ name } ")
                end
            end
            self
        end
         EOF end
Helper method for inherited_enumerable
It is called in the context of the singleton class of the module/class on
which inherited_enumerable is called
Helper method for inherited_enumerable It is called in the context of the singleton class of the module / class on which inherited_enumerable is called
name
attribute_name
options