Archive for October, 2008

running system commands with ruby

# notice the backticks
`git config --global user.email`

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

dm-is-protectable

I just released dm-is-protectable which is an attempt to make it easier for datamapper users on merb or on rails to implement permission checking in their models, instead of in their controller.s Yes, I know that sounds weird! but too many permission checks are done solely in controllers these days …

debugging the call stack using puts and caller in ruby


# call this anywhere inside a method
# it will print out the call stack for the current execution
# play with the numbers to narrow down the area of interest

def print_call_stack(method_name, from, to)
  from.upto(to) { |i| p "#{method_name}[#{i}]: #{caller[i]}" }
end