<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: ruby code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Tue, 22 Jul 2008 18:15:22 GMT</pubDate>
    <description>DZone Snippets: ruby code</description>
    <item>
      <title>Removing empty directories (non-recursive)</title>
      <link>http://snippets.dzone.com/posts/show/4564</link>
      <description>// Simple class to remove empty directories.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'fileutils'&lt;br /&gt;&lt;br /&gt;class ReorgBot&lt;br /&gt;&lt;br /&gt;  attr_accessor :path&lt;br /&gt;  attr_accessor :dir&lt;br /&gt;&lt;br /&gt;  def initialize(path)&lt;br /&gt;    raise ArgumentError, "path '#{path}' does not exist!" unless File.exist?(path)&lt;br /&gt;    raise ArgumentError, "path '#{path} is not a directory you dolt!" unless File.directory?(path)&lt;br /&gt;    @path = path&lt;br /&gt;    @dir = Dir.new(path)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def dirs&lt;br /&gt;    @dir.select { |f| File.directory?(File.join(@path, f)) }&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def empty_dirs&lt;br /&gt;    dirs.select { |d| Dir[File.join(@path, d, '*')].empty? }&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def non_empty_dirs&lt;br /&gt;    dirs - empty_dirs&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def remove_empty_dirs&lt;br /&gt;    empty_dirs.each do |d|&lt;br /&gt;      FileUtils.rm_rf(File.join(@path, d))&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;bot = ReorgBot.new('/tmp/reorg_test')&lt;br /&gt;bot.remove_empty_dirs&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 21 Sep 2007 03:49:21 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4564</guid>
      <author>chuyeow (Chu Yeow)</author>
    </item>
    <item>
      <title>Helper for displaying flash</title>
      <link>http://snippets.dzone.com/posts/show/3978</link>
      <description>&lt;code&gt;&lt;br /&gt;// Add this to your application_helper.rb.&lt;br /&gt;&lt;br /&gt;  def display_standard_flashes&lt;br /&gt;    known_levels = [:error, :warning, :notice] # highest priority to lowest&lt;br /&gt;    level = known_levels.find { |level| flash.has_key?(level) }&lt;br /&gt;    level ? content_tag('div', flash[level], :class =&gt; "flash #{level}") : nil&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 10 May 2007 06:21:42 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3978</guid>
      <author>chuyeow (Chu Yeow)</author>
    </item>
    <item>
      <title>WEBrick servlet with HTTP authentication</title>
      <link>http://snippets.dzone.com/posts/show/3830</link>
      <description>// HTTP authentication for a directory listing&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'webrick'&lt;br /&gt;include WEBrick&lt;br /&gt;&lt;br /&gt;dir = Dir::pwd&lt;br /&gt;port = 1234&lt;br /&gt;&lt;br /&gt;authenticate = Proc.new do |req, res|&lt;br /&gt;  HTTPAuth.basic_auth(req, res, '') do |user, password|&lt;br /&gt;    user == 'foo' &amp;&amp; password == 'bar'&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;s = HTTPServer.new(:Port =&gt; port, :ServerType =&gt; Daemon)&lt;br /&gt;s.mount('/', HTTPServlet::FileHandler, dir,&lt;br /&gt;  :FancyIndexing =&gt; true,&lt;br /&gt;  :HandlerCallback =&gt; authenticate # Hook up the authentication proc.&lt;br /&gt;)&lt;br /&gt;&lt;br /&gt;trap('INT') { s.shutdown }&lt;br /&gt;s.start&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 16 Apr 2007 15:12:46 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3830</guid>
      <author>chuyeow (Chu Yeow)</author>
    </item>
    <item>
      <title>WEBrick servlet skeleton</title>
      <link>http://snippets.dzone.com/posts/show/3829</link>
      <description>// Skeleton code for a WEBrick servlet.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'webrick'&lt;br /&gt;include WEBrick&lt;br /&gt;class UberServlet &lt; HTTPServlet::AbstractServlet&lt;br /&gt;&lt;br /&gt;  def do_GET(req, res)&lt;br /&gt;    id = req.query['id'] # Get GET/POST params like that.&lt;br /&gt;    res['content-type'] = 'text/html'&lt;br /&gt;    res.status = 200&lt;br /&gt;    res.body = some_text&lt;br /&gt;  end&lt;br /&gt;  alias :do_POST :do_GET&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;# "Mount" the servlet.&lt;br /&gt;server = HTTPServer.new(:Port =&gt; 1234)&lt;br /&gt;server.mount('/some/path', UberServlet)&lt;br /&gt;&lt;br /&gt;# Handle signals.&lt;br /&gt;%w(INT TERM).each do |signal|&lt;br /&gt;  trap(signal) { server.shutdown }&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;server.start&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 16 Apr 2007 15:08:46 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3829</guid>
      <author>chuyeow (Chu Yeow)</author>
    </item>
    <item>
      <title>Struct "magic" - creating objects from CSV file</title>
      <link>http://snippets.dzone.com/posts/show/3828</link>
      <description>&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'csv'&lt;br /&gt;&lt;br /&gt;csv = CSV.open('some_file.csv', 'r')&lt;br /&gt;Post = Struct.new(*(csv.shift.map { |f| f.to_sym })) # Nice! Read in CSV header, turns them into symbols, and creates a new Struct.&lt;br /&gt;posts = csv.inject([]) do |posts, row|&lt;br /&gt;  posts &lt;&lt; Post[*row]&lt;br /&gt;end&lt;br /&gt;csv.close&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 16 Apr 2007 15:04:15 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3828</guid>
      <author>chuyeow (Chu Yeow)</author>
    </item>
    <item>
      <title>Ruby Struct example</title>
      <link>http://snippets.dzone.com/posts/show/3827</link>
      <description>&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Post = Struct.new(:id, :title, :content, :created_at)&lt;br /&gt;&lt;br /&gt;# Some database access, maybe via DBI.&lt;br /&gt;while row = @stmt.fetch do&lt;br /&gt;  posts &lt;&lt; Post.new(*row)&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 16 Apr 2007 15:00:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3827</guid>
      <author>chuyeow (Chu Yeow)</author>
    </item>
    <item>
      <title>Add a to_conditions method to ActiveRecord::Base for converting models to finder :conditions hash.</title>
      <link>http://snippets.dzone.com/posts/show/3732</link>
      <description>// Mixes in a to_conditions method to ActiveRecord::Base. Converts the attributes of an AR object to a&lt;br /&gt;// ActiveRecord::Base#find :conditions hash. Useful for comparing AR objects, especially when looking for&lt;br /&gt;// duplicates.&lt;br /&gt;// E.g.&lt;br /&gt;//   &lt;br /&gt;//   if not Post.find(:all, :conditions =&gt; my_post.conditions).empty?&lt;br /&gt;//     puts "Duplicate found"&lt;br /&gt;//   end&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;module Bezurk #:nodoc:&lt;br /&gt;  module ActiveRecord #:nodoc:&lt;br /&gt;    module Extensions&lt;br /&gt;      def to_conditions&lt;br /&gt;        attributes.inject({}) do |hash, (name, value)|&lt;br /&gt;          hash.merge(name.intern =&gt; value)&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;      alias :to_conditions_hash :to_conditions&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;ActiveRecord::Base.send(:include, Bezurk::ActiveRecord::Extensions)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 26 Mar 2007 10:54:25 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3732</guid>
      <author>chuyeow (Chu Yeow)</author>
    </item>
    <item>
      <title>Updating Rubygems and gems</title>
      <link>http://snippets.dzone.com/posts/show/3550</link>
      <description>// Update Rubygems to the latest version, and getting the latest gems, and then cleaning up.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;sudo gem update --system  # Update to latest Rubygems (if any)&lt;br /&gt;gem outdated  # Get list of outdated gems and update as necessary&lt;br /&gt;sudo gem install XXX --include-dependencies&lt;br /&gt;sudo gem clean  # Clean up outdated gems&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 20 Feb 2007 09:14:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3550</guid>
      <author>chuyeow (Chu Yeow)</author>
    </item>
    <item>
      <title>Running a single test method with ZenTest</title>
      <link>http://snippets.dzone.com/posts/show/2749</link>
      <description>// HOWTO run a single test method.&lt;br /&gt;// You need ZenTest installed (gem install ZenTest).&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# Run ruby_fork in the background.&lt;br /&gt;ruby_fork -rubygems -e 'require_gem "rails"' &amp;&lt;br /&gt;&lt;br /&gt;# Run a single test method.&lt;br /&gt;ruby_fork_client -r test/functional/account_controller_test.rb -- -n test_unactivated_user_should_activate&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 01 Oct 2006 18:05:41 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2749</guid>
      <author>chuyeow (Chu Yeow)</author>
    </item>
  </channel>
</rss>
