<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Andyv's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 20:55:19 GMT</pubDate>
    <description>DZone Snippets: Andyv's Code Snippets</description>
    <item>
      <title>Tabs based on definition list</title>
      <link>http://snippets.dzone.com/posts/show/4113</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;&lt;br /&gt;&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;br /&gt;&lt;head&gt;&lt;br /&gt;&lt;style type="text/css"&gt;&lt;br /&gt;dl.tabstrip {&lt;br /&gt;float:left;&lt;br /&gt;width:60em;&lt;br /&gt;font-size:120%;&lt;br /&gt;line-height:normal;&lt;br /&gt;background: url("images/tabstripbottom.png") repeat-x bottom;&lt;br /&gt;margin:0;&lt;br /&gt;padding:8px 8px 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;dl.tabstrip dt {&lt;br /&gt;float:left;&lt;br /&gt;padding:0;&lt;br /&gt;margin:0 2px 0 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;dl.tabstrip a {&lt;br /&gt;display:block;&lt;br /&gt;padding: 4px 12px 4px;&lt;br /&gt;color:black;&lt;br /&gt;font-family:verdana, sans-serif;&lt;br /&gt;text-decoration:none;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;dt.selected {&lt;br /&gt;background: url("images/tabright.png") top right no-repeat;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;dt.selected a {&lt;br /&gt;background: url("images/tableft.png") top left no-repeat;&lt;br /&gt;padding-bottom:5px;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;dt.unselected {&lt;br /&gt;background: url("images/unselectedtabright.png") top right no-repeat;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;dt.unselected a {&lt;br /&gt;background: url("images/unselectedtableft.png") top left no-repeat;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.tabstrip dd {&lt;br /&gt;position:fixed;&lt;br /&gt;left:8px;&lt;br /&gt;top: 2.5em;&lt;br /&gt;border: 1px solid #6290d2;&lt;br /&gt;border-top:none;&lt;br /&gt;background:#fff;&lt;br /&gt;margin:0;&lt;br /&gt;height:10em;&lt;br /&gt;width: 60em;&lt;br /&gt;padding:8px 8px 8px 6px;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;dd.selected {&lt;br /&gt;display:block;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;dd.unselected {&lt;br /&gt;display:none;&lt;br /&gt;}&lt;br /&gt;&lt;/style&gt;&lt;br /&gt;&lt;/head&gt;&lt;br /&gt;&lt;br /&gt;&lt;body&gt;&lt;br /&gt;&lt;dl class="tabstrip"&gt;&lt;br /&gt;	&lt;dt class="unselected"&gt;&lt;a href="#"&gt;Tracy&lt;/a&gt;&lt;/dt&gt;&lt;br /&gt;	&lt;dd class="unselected"&gt;&lt;br /&gt;		&lt;p&gt;Position: President&lt;/p&gt;&lt;br /&gt;	&lt;/dd&gt;&lt;br /&gt;&lt;br /&gt;	&lt;dt class="selected"&gt;&lt;a href="#"&gt;Andy&lt;/a&gt;&lt;/dt&gt;&lt;br /&gt;	&lt;dd class="selected"&gt;&lt;br /&gt;		&lt;p&gt;Position: Architect&lt;/p&gt;&lt;br /&gt;	&lt;/dd&gt;&lt;br /&gt;&lt;br /&gt;	&lt;dt class="unselected"&gt;&lt;a href="#"&gt;Ron&lt;/a&gt;&lt;/dt&gt;&lt;br /&gt;	&lt;dd class="unselected"&gt;&lt;br /&gt;		&lt;p&gt;Position: Operations Manager&lt;/p&gt;&lt;br /&gt;	&lt;/dd&gt;&lt;br /&gt;&lt;/dl&gt;&lt;br /&gt;&lt;/body&gt;&lt;br /&gt;&lt;/html&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 07 Jun 2007 18:18:00 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4113</guid>
      <author>AndyV (Andy Vanasse)</author>
    </item>
    <item>
      <title>Metaphone calculation</title>
      <link>http://snippets.dzone.com/posts/show/4112</link>
      <description>// Calculate's Lawrence Phillip's basic metaphone (a more advanced code for matching names)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Metaphone&lt;br /&gt;  TRANSFORMATIONS = [[/\A[gkp]n/  ,  'n'],   # gn, kn, or pn at the start turns into 'n'&lt;br /&gt;                     [/\Ax/       ,  's'],   # x at the start turns into 's'&lt;br /&gt;                     [/\Awh/      ,  'w'],   # wh at the start turns into 'w'&lt;br /&gt;                     [/mb\z/      ,  'm'],   # mb at the end turns into 'm'&lt;br /&gt;                     [/sch/       ,  'sk'],  # sch sounds like 'sk'&lt;br /&gt;                     [/x/         ,  'ks'],&lt;br /&gt;                     [/cia/       ,  'xia'], # the 'c' -cia- and -ch- sounds like 'x'&lt;br /&gt;                     [/ch/        ,  'xh'],&lt;br /&gt;                     [/c([iey])/  ,  's\1'], # the 'c' -ce-, -ci-, or -cy- sounds like 's'&lt;br /&gt;                     [/ck/        ,  'k'],&lt;br /&gt;                     [/c/         ,  'k'],&lt;br /&gt;                     [/dg([eiy])/ ,  'j\1'], # the 'dg' in -dge-, -dgi-, or -dgy- sounds like 'j'&lt;br /&gt;                     [/d/         ,  't'],&lt;br /&gt;                     [/gh/        ,  ''],&lt;br /&gt;                     [/gned/      ,  'ned'],&lt;br /&gt;                     [/gn((?![aeiou])|(\z))/ ,  'n'],&lt;br /&gt;                     [/g[eiy]/    ,  'j'],&lt;br /&gt;                     [/ph/        ,  'f'],&lt;br /&gt;                     [/[aeiou]h(?![aeoiu])/ ,  '\1'], # 'h' is silent after a vowel unless it's between vowels&lt;br /&gt;                     [/q/         ,  'k'],&lt;br /&gt;                     [/s(h|(ia)|(io))/ ,  'x\1'],&lt;br /&gt;                     [/t((ia)|(io))/,  'x\1'],&lt;br /&gt;                     [/th/        ,  '0'],&lt;br /&gt;                     [/v/         ,  'f'],&lt;br /&gt;                     [/w(?![aeiou])/ ,  ''],&lt;br /&gt;                     [/y(?![aeiou])/ ,  ''],&lt;br /&gt;                     [/z/         ,  's']&lt;br /&gt;                    ]&lt;br /&gt;                    &lt;br /&gt;  def self.create_metaphone(aWord)&lt;br /&gt;    word = aWord.to_s.downcase&lt;br /&gt;    TRANSFORMATIONS.each{|transform| word.gsub!(transform.first, transform.last)}&lt;br /&gt;    'a'.upto('z'){|letter| word.gsub!(letter*2, letter)}&lt;br /&gt;    return (word[0].chr + word[1..word.length-1].gsub(/[aeiou]/, '')).upcase&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 07 Jun 2007 17:07:38 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4112</guid>
      <author>AndyV (Andy Vanasse)</author>
    </item>
    <item>
      <title>Soundex calculation</title>
      <link>http://snippets.dzone.com/posts/show/3900</link>
      <description>// Calculates a soundex for a name in (basic) conformance to the &lt;br /&gt;// algorithm discussed at http://en.wikipedia.org/wiki/Soundex&lt;br /&gt;// It's wrapped in a class primarily to contain the ALPHA_MAP.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Soundex&lt;br /&gt;  ALPHA_MAP = { 'a' =&gt; nil, 'b' =&gt; '1', 'c' =&gt; '2', 'd' =&gt; '3', 'e' =&gt; nil, 'f' =&gt; '1',&lt;br /&gt;                'g' =&gt; '2', 'h' =&gt; nil, 'i' =&gt; nil, 'j' =&gt; '2', 'k' =&gt; '2', 'l' =&gt; '4',&lt;br /&gt;                'm' =&gt; '5', 'n' =&gt; '5', 'o' =&gt; nil, 'p' =&gt; '1', 'q' =&gt; '2', 'r' =&gt; '6',&lt;br /&gt;                's' =&gt; '2', 't' =&gt; '3', 'u' =&gt; nil, 'v' =&gt; '1', 'w' =&gt; nil, 'x' =&gt; '2',&lt;br /&gt;                'y' =&gt; nil, 'z' =&gt; 2&lt;br /&gt;              }&lt;br /&gt;              &lt;br /&gt;  def self.calculate(aName)&lt;br /&gt;    ary = []&lt;br /&gt;    # map all the letters in the supplied name&lt;br /&gt;    aName.downcase.each_byte{|ltr| ary.push(ALPHA_MAP[ltr.chr])}&lt;br /&gt;    # now drop out repeated values&lt;br /&gt;    ary.length.downto(1){ |idx| ary[idx] = nil if ary[idx]==ary[idx-1] }&lt;br /&gt;    # remove the nil elements&lt;br /&gt;    ary.compact!&lt;br /&gt;    # pad with zeroes&lt;br /&gt;    0.upto(2){ ary.push('0')}&lt;br /&gt;    # Replace the first value with the first letter of the supplied name&lt;br /&gt;    ary[0] = aName[0].chr&lt;br /&gt;    # return the first four elements of the array as a string&lt;br /&gt;    return ary[0..3].to_s&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 25 Apr 2007 16:44:43 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3900</guid>
      <author>AndyV (Andy Vanasse)</author>
    </item>
    <item>
      <title>Toggling associations for Many-to-Many relationships</title>
      <link>http://snippets.dzone.com/posts/show/2813</link>
      <description>I have a rather greedy object that needs to be associated with most of the other objects in my domain.  To compensate, I've written the following code to take care of toggling the association (adding a record in the join table if it does not exist or removing it if it does).  I've called it toggle because it's physically represented by toggling the state of a checkbox on a form.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def toggle_child_assoc(child_object)&lt;br /&gt;    # check to see if the parent recognizes the collection_name&lt;br /&gt;    collection_name = child_object.class.to_s.downcase.pluralize&lt;br /&gt;    aCollection = self.send(collection_name) if self.respond_to?(collection_name)&lt;br /&gt;    unless aCollection&lt;br /&gt;      return false&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    # If the parent recognizes the collection_name then check to see if the child_object&lt;br /&gt;    # appears in the collection.  If so then remove it, otherwise add it.&lt;br /&gt;    if aCollection.include?(child_object)&lt;br /&gt;      self.send("remove_%s" % collection_name, child_object)&lt;br /&gt;    else&lt;br /&gt;      self.send("add_%s" % collection_name, child_object)&lt;br /&gt;    end&lt;br /&gt;    true&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;First the code determines if the "parent" of the association actually supports a collection of the child object's class by testing to see if it responds to the method corresponding to the name of the collection.  If not the method returns false.  Otherwise, the method checks to see if the parent object already has the child in its collection.  If it does it builds a call to remove_collection_name to remove it and if it does not it builds a call to add_collection_name to add it.&lt;br /&gt;&lt;br /&gt;The code could be improved by adding a means by which to override the collection_name.  This override would be used in the event that the child is a subclass of a class with which the parent has a habtm relationship.</description>
      <pubDate>Fri, 13 Oct 2006 21:24:26 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2813</guid>
      <author>AndyV (Andy Vanasse)</author>
    </item>
  </channel>
</rss>
