<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Mixonic's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 03:09:04 GMT</pubDate>
    <description>DZone Snippets: Mixonic's Code Snippets</description>
    <item>
      <title>vimrc for 2 space tab replacements</title>
      <link>http://snippets.dzone.com/posts/show/1374</link>
      <description>~/.vimrc:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;set softtabstop=2&lt;br /&gt;set shiftwidth=2&lt;br /&gt;set tabstop=2&lt;br /&gt;set expandtab&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 08 Feb 2006 19:14:49 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1374</guid>
      <author>mixonic (Matthew Beale)</author>
    </item>
    <item>
      <title>Ruby FPDF PDF_MC_Table port</title>
      <link>http://snippets.dzone.com/posts/show/1081</link>
      <description>My old direct port of FPDF MultiCell tables to Ruby from PHP.  I'm sure there are at least 30 better ways to do this :-) (PDF::Writer is more popular now anyway)&lt;br /&gt;&lt;br /&gt;FPDF: http://fpdf.org/en/script/script3.php&lt;br /&gt;Ruby FPDF: http://brian.imxcc.com/fpdf/&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'fpdf'&lt;br /&gt;&lt;br /&gt;class PDF_MC_Table &lt; FPDF&lt;br /&gt;  attr_reader :widths, :aligns&lt;br /&gt;&lt;br /&gt;  def SetWidths (w)&lt;br /&gt;    # Set the array of column widths&lt;br /&gt;    @widths=w;&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def SetAligns (a)&lt;br /&gt;    # Set the array of column alignments&lt;br /&gt;    @aligns=a;&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def Row (data)&lt;br /&gt;    # Calculate the height of the row&lt;br /&gt;    nb = 0&lt;br /&gt;    0.upto(data.length - 1) do |i|&lt;br /&gt;      num_lines = NbLines(@widths[i],data[i])&lt;br /&gt;      nb = num_lines if num_lines &gt; nb&lt;br /&gt;    end&lt;br /&gt;    h = 5 * nb&lt;br /&gt;    # Issue a page break first if needed&lt;br /&gt;    CheckPageBreak(h)&lt;br /&gt;    # Draw the cells of the row&lt;br /&gt;    0.upto(data.length - 1) do |i|&lt;br /&gt;        w = @widths[i]&lt;br /&gt;        a = ( (@aligns &amp;&amp; @aligns[i]) ? @aligns[i] : 'L' )&lt;br /&gt;        # Save the current position&lt;br /&gt;        x = GetX()&lt;br /&gt;        y = GetY()&lt;br /&gt;        # Draw the border&lt;br /&gt;        Rect(x,y,w,h)&lt;br /&gt;        # Print the text&lt;br /&gt;        MultiCell(w,5,data[i],0,a)&lt;br /&gt;        # Put the position to the right of the cell&lt;br /&gt;        SetXY(x+w,y)&lt;br /&gt;    end&lt;br /&gt;    # Go to the next line&lt;br /&gt;    Ln(h)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def CheckPageBreak(h)&lt;br /&gt;    # If the height h would cause an overflow, add a new page immediately&lt;br /&gt;    AddPage(@CurOrientation) if GetY()+h &gt; @PageBreakTrigger&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def NbLines(w,txt)&lt;br /&gt;    # Computes the number of lines a MultiCell of width w will take&lt;br /&gt;    cw = @CurrentFont['cw']&lt;br /&gt;    w = w - @rMargin - @x if ( w==0 )&lt;br /&gt;    wmax = ( w - 2 * @cMargin ) * 1000 / @FontSize&lt;br /&gt;    s = txt ? txt.gsub(/\r/,'') : ''&lt;br /&gt;    nb = s.length&lt;br /&gt;    nb -= 1 if ( nb &gt; 0 and s[nb-1]==10 ) # strip a trailing \n&lt;br /&gt;    sep = -1&lt;br /&gt;    i = 0&lt;br /&gt;    j = 0&lt;br /&gt;    l = 0&lt;br /&gt;    nl = 1&lt;br /&gt;    while(i &lt; nb) do&lt;br /&gt;      c=s[i] #go through the string character by character&lt;br /&gt;      if(c==10) #if we find a newline just skip the rest&lt;br /&gt;	i += 1&lt;br /&gt;        sep = -1&lt;br /&gt;        j = i&lt;br /&gt;        l = 0&lt;br /&gt;        nl += 1&lt;br /&gt;        next&lt;br /&gt;      end&lt;br /&gt;      sep = i if (c==' ')&lt;br /&gt;      l = l + cw[c]&lt;br /&gt;      if(l&gt;wmax)&lt;br /&gt;        if(sep==-1)&lt;br /&gt;          if(i==j)&lt;br /&gt;	    i += 1&lt;br /&gt;	  end&lt;br /&gt;        else&lt;br /&gt;	  i = sep+1&lt;br /&gt;	end&lt;br /&gt;        sep = -1&lt;br /&gt;        j = i&lt;br /&gt;        l = 0&lt;br /&gt;        nl += 1&lt;br /&gt;      else&lt;br /&gt;	i += 1&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;    return nl&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 09 Jan 2006 22:26:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1081</guid>
      <author>mixonic (Matthew Beale)</author>
    </item>
    <item>
      <title>Barebones xHTML and CSS template pages</title>
      <link>http://snippets.dzone.com/posts/show/1025</link>
      <description>Ok, so this guy already did this: http://www.bigbold.com/snippets/posts/show/583 but I needed a bit more.  So this is my xHTML template page (it doesn't declare xml since it makes IE kick out of standards mode):&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"&lt;br /&gt;  "DTD/xhtml1-transitional.dtd"&gt;&lt;br /&gt;&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt;&lt;br /&gt;&lt;head&gt;&lt;br /&gt;  &lt;title&gt;XHTML TEMPLATE&lt;/title&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;&lt;br /&gt;  &lt;meta name="description" content="THIS IS A TEMPLATE XHTML PAGE" /&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;link rel="stylesheet" href="main.css" type="text/css" media="screen" /&gt;&lt;br /&gt;  &lt;br /&gt;  &lt;!-- &lt;script type="text/javascript" src="prototype.js"&gt;&lt;br /&gt;  &lt;/script&gt; --&gt;&lt;br /&gt;  &lt;script type="text/javascript" language="JavaScript"&gt;&lt;!-- &lt;![CDATA[&lt;br /&gt;  // ]]&gt; --&gt;&lt;/script&gt;&lt;br /&gt;&lt;/head&gt;&lt;br /&gt;&lt;body&gt;&lt;br /&gt;  &lt;div id="container"&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/div&gt;&lt;br /&gt;&lt;/body&gt;&lt;br /&gt;&lt;/html&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;This is the accomponying CSS:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;body {&lt;br /&gt;  font-family :Verdana, Arial, Sans;&lt;br /&gt;  font-size   :76%;&lt;br /&gt;  margin      :0px; }&lt;br /&gt;div#container {&lt;br /&gt;  font-size   :1.0em; }&lt;br /&gt;&lt;br /&gt;.clearfix:after {&lt;br /&gt;    content: "."; &lt;br /&gt;    display: block; &lt;br /&gt;    height: 0; &lt;br /&gt;    clear: both; &lt;br /&gt;    visibility: hidden;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.clearfix {display: inline-table;}&lt;br /&gt;&lt;br /&gt;/* Hides from IE-mac \*/&lt;br /&gt;* html .clearfix {height: 1%;}&lt;br /&gt;.clearfix {display: block;}&lt;br /&gt;/* End hide from IE-mac */&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 28 Dec 2005 06:52:15 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1025</guid>
      <author>mixonic (Matthew Beale)</author>
    </item>
    <item>
      <title>Backup MySQL databases into seperate files</title>
      <link>http://snippets.dzone.com/posts/show/988</link>
      <description>A cronable script for backing up all your databases as seperate files.  I'd suggest limiting the backup user's access to the IP of the computer backing up, and using some sort of encryption if you're on a capable version of mysql.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/bin/sh&lt;br /&gt;#&lt;br /&gt;# MySQL backups from the Data&lt;br /&gt;&lt;br /&gt;MOUNTED=`grep /etc/mtab -e \/mnt\/backup`&lt;br /&gt;if [ "$MOUNTED" = '' ]; then&lt;br /&gt;        echo "/mnt/backup is not mounted, there is no drive to backup to"&lt;br /&gt;        exit 1&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;mkdir -p /mnt/backup/cluster_sql&lt;br /&gt;&lt;br /&gt;for i in $(echo 'SHOW DATABASES;' | mysql -ubackup -pSUPERSECRET -hData|grep -v '^Database$'); do&lt;br /&gt;  mysqldump -ubackup -pSUPERSECRET -hData --opt $i &gt; /mnt/backup/cluster_sql/$i.sql;&lt;br /&gt;done;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Tue, 20 Dec 2005 12:50:04 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/988</guid>
      <author>mixonic (Matthew Beale)</author>
    </item>
    <item>
      <title>Model.find_or_create_by_title - rails &amp;lt; 0.14</title>
      <link>http://snippets.dzone.com/posts/show/978</link>
      <description>Check it!&lt;br /&gt;&lt;br /&gt;In our model for rails we can have a need to get the id for a title, but sometimes want to create the title on the fly (like adding a tag to something, if it's there we want the id, if it's not we want to make it and get back the id).  That'd be messy in the controller, this is much nicer:&lt;br /&gt;&lt;br /&gt;*Note - only for &lt; 0.14 rails.  Something like this became standard in rails after that.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  def Project.find_or_create_by_title(title)&lt;br /&gt;    return nil if title.empty?&lt;br /&gt;&lt;br /&gt;    if (project = Project.find( :first, :conditions =&gt; [ 'title = ?', title ] ))&lt;br /&gt;      return project.id&lt;br /&gt;    else&lt;br /&gt;      project = Project.new&lt;br /&gt;      project.title = title&lt;br /&gt;      if project.save&lt;br /&gt;        return project.id&lt;br /&gt;      else&lt;br /&gt;        @flash[:notice] = 'Sorry, a project could not be created'&lt;br /&gt;        return nil&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 15 Dec 2005 03:36:07 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/978</guid>
      <author>mixonic (Matthew Beale)</author>
    </item>
    <item>
      <title>ajax_pagination_links</title>
      <link>http://snippets.dzone.com/posts/show/386</link>
      <description>Put in a helper somewhere, and use like this:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;%= ajax_pagination_links @client_pages, {:update =&gt; 'search_results', :params =&gt; {:search_query =&gt; @params[:search_query]} } %&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;and just update the div your results (and the above line) are in.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;       def ajax_pagination_links(paginator, options={})&lt;br /&gt;         &lt;br /&gt;         options.merge!(ActionView::Helpers::PaginationHelper::DEFAULT_OPTIONS) {|key, old, new| old}&lt;br /&gt;         &lt;br /&gt;         window_pages = paginator.current.window(options[:window_size]).pages&lt;br /&gt; &lt;br /&gt;         return if window_pages.length &lt;= 1 unless&lt;br /&gt;           options[:link_to_current_page]&lt;br /&gt;         &lt;br /&gt;         first, last = paginator.first, paginator.last&lt;br /&gt;         &lt;br /&gt;         returning html = '' do&lt;br /&gt;           if options[:always_show_anchors] and not window_pages[0].first?&lt;br /&gt;             html &lt;&lt; link_to_remote(first.number, :update =&gt; options[:update], :url =&gt; { options[:name] =&gt; first }.update(options[:params] ))&lt;br /&gt;             html &lt;&lt; ' ... ' if window_pages[0].number - first.number &gt; 1&lt;br /&gt;             html &lt;&lt; ' '&lt;br /&gt;           end&lt;br /&gt;           &lt;br /&gt;           window_pages.each do |page|&lt;br /&gt;             if paginator.current == page &amp;&amp; !options[:link_to_current_page]&lt;br /&gt;               html &lt;&lt; page.number.to_s&lt;br /&gt;             else&lt;br /&gt;               html &lt;&lt; link_to_remote(page.number, :update =&gt; options[:update], :url =&gt; { options[:name] =&gt; page }.update(options[:params] ))&lt;br /&gt;             end&lt;br /&gt;             html &lt;&lt; ' '&lt;br /&gt;           end&lt;br /&gt;           &lt;br /&gt;           if options[:always_show_anchors] &amp;&amp; !window_pages.last.last?&lt;br /&gt;             html &lt;&lt; ' ... ' if last.number - window_pages[-1].number &gt; 1&lt;br /&gt;             html &lt;&lt; link_to_remote(paginator.last.number, :update =&gt; options[:update], :url =&gt; { options[:name] =&gt; last }.update( options[:params]))&lt;br /&gt;           end&lt;br /&gt;         end&lt;br /&gt;       end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 15 Jun 2005 02:13:35 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/386</guid>
      <author>mixonic (Matthew Beale)</author>
    </item>
  </channel>
</rss>
