title: Anonymous subclass meta-programming causing unwanted behaviour (Virtus)
body: <p>I'm using Virtus (basically the Property API from DataMapper) to build around a remote API, however I don't <em>think</em> Virtus is the problem here, I think it's my lack of understanding of what Ruby is doing.</p>
  
  <p>I want to allow attributes that coerce to a given type, by using the Syntax:</p>
  
  <pre><code>Node[AnotherClass]
  </code></pre>
  
  <p>Which simply generates on-the-fly a subclass of Node and returns that new class.  That part is working.  But for some reason, it has an undesirable side-effect.  All other objects that descend from <code>Virtus::Attribute::Object</code> are actually also Node subclasses themselves.  I can't explain it, but I think it must be an expected ruby behaviour related to the inheritance model.  Can anybody point me in the right direction?</p>
  
  <p>(Note the following code runs without modification if you <code>gem install virtus</code>).</p>
  
  <pre><code>require "virtus"
  
  class JsonModel
    include Virtus
  end
  
  class Node &lt; Virtus::Attribute::Object
    attr_reader :type
  
    class &lt;&lt; self
      def [](type)
        raise ArgumentError, "Child nodes may only be other JsonModel classes" unless type &lt;= JsonModel
  
        @generated_class_map       ||= {}
        @generated_class_map[type] ||= Class.new(self) do
          default lambda { |m, a| type.new }
  
          define_method :type do
            type
          end
  
          define_method :coerce do |value|
            value.kind_of?(Hash) ? type.new(value) : value
          end
        end
      end
    end
  end
  
  class ChildModel &lt; JsonModel
  end
  
  class ParentModel &lt; JsonModel
    attribute :child,  Node[ChildModel]
    attribute :string, String
  end
  
  # This should be String, but it's a descendant of Node??
  puts ParentModel.attributes[:string].class.ancestors.inspect
  </code></pre>