Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Andy Vanasse

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

Tabs based on definition list

// description of your code here

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
dl.tabstrip {
float:left;
width:60em;
font-size:120%;
line-height:normal;
background: url("images/tabstripbottom.png") repeat-x bottom;
margin:0;
padding:8px 8px 0;
}

dl.tabstrip dt {
float:left;
padding:0;
margin:0 2px 0 0;
}

dl.tabstrip a {
display:block;
padding: 4px 12px 4px;
color:black;
font-family:verdana, sans-serif;
text-decoration:none;
}

dt.selected {
background: url("images/tabright.png") top right no-repeat;
}

dt.selected a {
background: url("images/tableft.png") top left no-repeat;
padding-bottom:5px;
}


dt.unselected {
background: url("images/unselectedtabright.png") top right no-repeat;
}

dt.unselected a {
background: url("images/unselectedtableft.png") top left no-repeat;
}

.tabstrip dd {
position:fixed;
left:8px;
top: 2.5em;
border: 1px solid #6290d2;
border-top:none;
background:#fff;
margin:0;
height:10em;
width: 60em;
padding:8px 8px 8px 6px;
}

dd.selected {
display:block;
}

dd.unselected {
display:none;
}
</style>
</head>

<body>
<dl class="tabstrip">
	<dt class="unselected"><a href="#">Tracy</a></dt>
	<dd class="unselected">
		<p>Position: President</p>
	</dd>

	<dt class="selected"><a href="#">Andy</a></dt>
	<dd class="selected">
		<p>Position: Architect</p>
	</dd>

	<dt class="unselected"><a href="#">Ron</a></dt>
	<dd class="unselected">
		<p>Position: Operations Manager</p>
	</dd>
</dl>
</body>
</html>

Metaphone calculation

// Calculate's Lawrence Phillip's basic metaphone (a more advanced code for matching names)

class Metaphone
  TRANSFORMATIONS = [[/\A[gkp]n/  ,  'n'],   # gn, kn, or pn at the start turns into 'n'
                     [/\Ax/       ,  's'],   # x at the start turns into 's'
                     [/\Awh/      ,  'w'],   # wh at the start turns into 'w'
                     [/mb\z/      ,  'm'],   # mb at the end turns into 'm'
                     [/sch/       ,  'sk'],  # sch sounds like 'sk'
                     [/x/         ,  'ks'],
                     [/cia/       ,  'xia'], # the 'c' -cia- and -ch- sounds like 'x'
                     [/ch/        ,  'xh'],
                     [/c([iey])/  ,  's\1'], # the 'c' -ce-, -ci-, or -cy- sounds like 's'
                     [/ck/        ,  'k'],
                     [/c/         ,  'k'],
                     [/dg([eiy])/ ,  'j\1'], # the 'dg' in -dge-, -dgi-, or -dgy- sounds like 'j'
                     [/d/         ,  't'],
                     [/gh/        ,  ''],
                     [/gned/      ,  'ned'],
                     [/gn((?![aeiou])|(\z))/ ,  'n'],
                     [/g[eiy]/    ,  'j'],
                     [/ph/        ,  'f'],
                     [/[aeiou]h(?![aeoiu])/ ,  '\1'], # 'h' is silent after a vowel unless it's between vowels
                     [/q/         ,  'k'],
                     [/s(h|(ia)|(io))/ ,  'x\1'],
                     [/t((ia)|(io))/,  'x\1'],
                     [/th/        ,  '0'],
                     [/v/         ,  'f'],
                     [/w(?![aeiou])/ ,  ''],
                     [/y(?![aeiou])/ ,  ''],
                     [/z/         ,  's']
                    ]
                    
  def self.create_metaphone(aWord)
    word = aWord.to_s.downcase
    TRANSFORMATIONS.each{|transform| word.gsub!(transform.first, transform.last)}
    'a'.upto('z'){|letter| word.gsub!(letter*2, letter)}
    return (word[0].chr + word[1..word.length-1].gsub(/[aeiou]/, '')).upcase
  end
end

Soundex calculation

// Calculates a soundex for a name in (basic) conformance to the
// algorithm discussed at http://en.wikipedia.org/wiki/Soundex
// It's wrapped in a class primarily to contain the ALPHA_MAP.

class Soundex
  ALPHA_MAP = { 'a' => nil, 'b' => '1', 'c' => '2', 'd' => '3', 'e' => nil, 'f' => '1',
                'g' => '2', 'h' => nil, 'i' => nil, 'j' => '2', 'k' => '2', 'l' => '4',
                'm' => '5', 'n' => '5', 'o' => nil, 'p' => '1', 'q' => '2', 'r' => '6',
                's' => '2', 't' => '3', 'u' => nil, 'v' => '1', 'w' => nil, 'x' => '2',
                'y' => nil, 'z' => 2
              }
              
  def self.calculate(aName)
    ary = []
    # map all the letters in the supplied name
    aName.downcase.each_byte{|ltr| ary.push(ALPHA_MAP[ltr.chr])}
    # now drop out repeated values
    ary.length.downto(1){ |idx| ary[idx] = nil if ary[idx]==ary[idx-1] }
    # remove the nil elements
    ary.compact!
    # pad with zeroes
    0.upto(2){ ary.push('0')}
    # Replace the first value with the first letter of the supplied name
    ary[0] = aName[0].chr
    # return the first four elements of the array as a string
    return ary[0..3].to_s
  end
end

Toggling associations for Many-to-Many relationships

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.

def toggle_child_assoc(child_object)
    # check to see if the parent recognizes the collection_name
    collection_name = child_object.class.to_s.downcase.pluralize
    aCollection = self.send(collection_name) if self.respond_to?(collection_name)
    unless aCollection
      return false
    end

    # If the parent recognizes the collection_name then check to see if the child_object
    # appears in the collection.  If so then remove it, otherwise add it.
    if aCollection.include?(child_object)
      self.send("remove_%s" % collection_name, child_object)
    else
      self.send("add_%s" % collection_name, child_object)
    end
    true
end


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.

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.
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS