support for speccing datamapper models with rspec

# Put this in spec_helper.rb
module SpecSupport

  def self.unload_class(*classes)
    classes.each do |c|
      Object.send(:remove_const, c) if Object.const_defined?(c)
    end
  end

  def self.fresh_class(name, superclass = nil, &block)
    unload_class(name)
    args = superclass ? [ superclass ] : []
    Object.const_set(name, Class.new(*args))
    Object.const_get(name).class_eval(&block)
  end

  def self.fresh_model(name)
    fresh_class(name) do
      include DataMapper::Resource
      property :id,         DataMapper::Types::Serial
      property :created_at, DateTime
      property :updated_at, DateTime
    end
  end

end


Leave a Reply

Formatting: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>