<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: hook code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 01:33:40 GMT</pubDate>
    <description>DZone Snippets: hook code</description>
    <item>
      <title>Ruby subversion pre-commit hook to prevent conflicting Rails migrations</title>
      <link>http://snippets.dzone.com/posts/show/2908</link>
      <description>This is a subversion pre-commit hook that prevents a Ruby on Rails migration being committed that has the same version as an existing migration. To install you place this in a file called pre-commit in the hooks directory of your subversion repository. You can read about hooks here: http://svnbook.red-bean.com/en/1.0/ch05s02.html&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/env ruby&lt;br /&gt;&lt;br /&gt;repo_path = ARGV[0]&lt;br /&gt;transaction = ARGV[1]&lt;br /&gt;svnlook = '/usr/bin/svnlook'&lt;br /&gt;&lt;br /&gt;commit_dirs_changed = `#{svnlook} dirs-changed #{repo_path} -t #{transaction}`&lt;br /&gt;commit_changed = `#{svnlook} changed #{repo_path} -t #{transaction}`&lt;br /&gt;#commit_author = `#{svnlook} author #{repo_path} -t #{transaction}`.chop&lt;br /&gt;commit_log = `#{svnlook} log #{repo_path} -t #{transaction}`&lt;br /&gt;#commit_diff = `#{svnlook} diff #{repo_path} -t #{transaction}`&lt;br /&gt;#commit_date = `#{svnlook} date #{repo_path} -t #{transaction}`&lt;br /&gt;&lt;br /&gt;# ******* Migration check ********&lt;br /&gt;# if this is a migration then check that there is not already a migration with the same version number in the repository&lt;br /&gt;files = commit_changed.split(/\n/)&lt;br /&gt;current_migrations = nil&lt;br /&gt;for file in files&lt;br /&gt;  if(file =~ /A\s*(.*?\/migrate\/)(\d+)(.*)/)&lt;br /&gt;    migration_path = $1&lt;br /&gt;    migration_version = $2&lt;br /&gt;    &lt;br /&gt;    if(current_migrations == nil)&lt;br /&gt;      current_migrations = {}&lt;br /&gt;      migration_files = `#{svnlook} tree #{repo_path} #{migration_path}`&lt;br /&gt;      for migration in migration_files&lt;br /&gt;        current_migrations[$1] = true if(migration =~ /\s*(\d+)_(.*)/)&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;    &lt;br /&gt;    if(current_migrations[migration_version])&lt;br /&gt;     STDERR.puts("The is a pre-existing migration with version #{migration_version} in #{migration_path}")&lt;br /&gt;     exit(1)&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 26 Oct 2006 18:19:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2908</guid>
      <author>conorh (Conor Hunt)</author>
    </item>
    <item>
      <title>Example of Detecting Subclasses</title>
      <link>http://snippets.dzone.com/posts/show/158</link>
      <description>class A&lt;br /&gt;  def A.inherited(clazz)&lt;br /&gt;    puts "Hey, #{clazz} is subclassing me"&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt; &lt;br /&gt;class B &lt; A; end&lt;br /&gt; &lt;br /&gt; &lt;br /&gt;Produces:&lt;br /&gt; &lt;br /&gt;$ ruby x.rb&lt;br /&gt;Hey, B is subclassing me</description>
      <pubDate>Wed, 13 Apr 2005 11:03:57 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/158</guid>
      <author>jimweirich (Jim Weirich)</author>
    </item>
  </channel>
</rss>
