lib/utilrb/models/inherited_enumerable.rb
# 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(name, attribute_name = name, options = Hash.new, &init) 
	attribute(attribute_name, &init)
	class_eval { private "#{attribute_name}=" }
        promote = method_defined?("promote_#{name}")
	options[:enum_with] ||= :each
        class_eval <<-EOF, __FILE__, __LINE__+1
        def all_#{name}; each_#{name}.to_a end
        def self_#{name}; @#{attribute_name} end
        EOF
        if options[:map]
            class_eval <<-EOF, __FILE__, __LINE__+1
            def find_#{name}(key)
                raise ArgumentError, "nil cannot be used as a key in find_#{name}" if !key
                each_#{name}(key, true) do |value|
                    return value
                end
                nil
            end
            def has_#{name}?(key)
                ancestors = self.ancestors
                if ancestors.first != self
                    ancestors.unshift self
                end
                for klass in ancestors
                    if klass.instance_variable_defined?(:@#{attribute_name})
                        return true if klass.#{attribute_name}.has_key?(key)
                    end
                end
                false
            end
            EOF
        end
        class_eval <<-EOF, __FILE__, __LINE__+1
        def clear_#{attribute_name}
            #{attribute_name}.clear
            for klass in ancestors
                if klass.instance_variable_defined?(:@#{attribute_name})
                    klass.#{attribute_name}.clear
                end
            end
        end
        EOF
        if !promote
            if options[:map]
                define_inherited_enumerable_map_without_promotion(name, attribute_name, options)
            else
                define_inherited_enumerable_nomap_without_promotion(name, attribute_name, options)
            end
        else
            if options[:map]
                define_inherited_enumerable_map_with_promotion(name, attribute_name, options)
            else
                define_inherited_enumerable_nomap_with_promotion(name, attribute_name, options)
            end
        end
    end
def define_inherited_enumerable ( name , attribute_name = name , options = Hash . new , & init ) attribute ( attribute_name , & init ) class_eval { private "#{attribute_name}=" } promote = method_defined? ( "promote_#{name}" ) options [ :enum_with ] ||= :each class_eval <<-EOF , __FILE__ , __LINE__ + 1 
        def all_ #{ name } ; each_ #{ name } .to_a end
        def self_ #{ name } ; @ #{ attribute_name }  end
         EOF if options [ :map ] class_eval <<-EOF , __FILE__ , __LINE__ + 1 
            def find_ #{ name } (key)
                raise ArgumentError, "nil cannot be used as a key in find_ #{ name } " if !key
                each_ #{ name } (key, true) do |value|
                    return value
                end
                nil
            end
            def has_ #{ name } ?(key)
                ancestors = self.ancestors
                if ancestors.first != self
                    ancestors.unshift self
                end
                for klass in ancestors
                    if klass.instance_variable_defined?(:@ #{ attribute_name } )
                        return true if klass. #{ attribute_name } .has_key?(key)
                    end
                end
                false
            end
             EOF end class_eval <<-EOF , __FILE__ , __LINE__ + 1 
        def clear_ #{ attribute_name } #{ attribute_name } .clear
            for klass in ancestors
                if klass.instance_variable_defined?(:@ #{ attribute_name } )
                    klass. #{ attribute_name } .clear
                end
            end
        end
         EOF if ! promote if options [ :map ] define_inherited_enumerable_map_without_promotion ( name , attribute_name , options ) else define_inherited_enumerable_nomap_without_promotion ( name , attribute_name , options ) end else if options [ :map ] define_inherited_enumerable_map_with_promotion ( name , attribute_name , options ) else define_inherited_enumerable_nomap_with_promotion ( name , attribute_name , options ) end end 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
# :nodoc: # Set up the attribute accessor
name
<not_specific>
attribute_name
<not_specific>
options
<not_specific>
new
<not_specific>
init
<not_specific>