Sha256: 2a7b44027458880e90860e7d7890c1970718bdaa01d115da2e1bd11ae6b0c3b3

Contents?: true

Size: 1.09 KB

Versions: 5

Compression:

Stored size: 1.09 KB

Contents

module SurveyGizmoSpec
  # global model cleanup. Mostly stolen from DataMapper.
  def self.cleanup_models
   descendants = SurveyGizmo::Resource.descendants.to_a

   while model = descendants.shift
     model_name = model.name.to_s.strip

     unless model_name.empty? || model_name[0] == ?#
       parts         = model_name.split('::')
       constant_name = parts.pop.to_sym
       base          = parts.empty? ? Object : SurveyGizmoSpec.full_const_get(parts.join('::'))

       base.class_eval { remove_const(constant_name) if const_defined?(constant_name) }
     end

     model.instance_methods(false).each { |method| model.send(:undef_method, method) }

   end

   SurveyGizmo::Resource.descendants.clear
  end
  
end

class Object
  def full_const_get(name)
    list = name.split("::")
    list.shift if list.first.blank?
    obj = self
    list.each do |x|
      # This is required because const_get tries to look for constants in the
      # ancestor chain, but we only want constants that are HERE
      obj = obj.const_defined?(x) ? obj.const_get(x) : obj.const_missing(x)
    end
    obj
  end  
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
survey-gizmo-ruby-0.9.6 spec/support/model_cleanup.rb
survey-gizmo-ruby-0.7.1 spec/support/model_cleanup.rb
survey-gizmo-ruby-0.7.0 spec/support/model_cleanup.rb
survey-gizmo-ruby-0.6.0 spec/support/model_cleanup.rb
survey-gizmo-ruby-0.5.0 spec/support/model_cleanup.rb