<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: S0nspark's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 22:40:09 GMT</pubDate>
    <description>DZone Snippets: S0nspark's Code Snippets</description>
    <item>
      <title>Memcached StartupItem for Mac OS X</title>
      <link>http://snippets.dzone.com/posts/show/2618</link>
      <description>&lt;br /&gt;Here is an OS X StartupItem for memcached...&lt;br /&gt;&lt;br /&gt;/Library/StartupItems/Memcached/StartupParameters.plist &lt;br /&gt;&lt;code&gt;&lt;br /&gt;{&lt;br /&gt;  Provides        = ("Memcached");&lt;br /&gt;  Description     = "Memcache Daemon";&lt;br /&gt;  Uses            = ("Network");&lt;br /&gt;  OrderPreference = "None";&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;/Library/StartupItems/Memcached/Memcached&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/bin/bash&lt;br /&gt;#&lt;br /&gt;# /Library/StartupItems/Memcached/Memcached&lt;br /&gt;#&lt;br /&gt;# Script to startup memcached with OS X. Tested on 10.4 Tiger&lt;br /&gt;#&lt;br /&gt;# To enable, copy this file and StartupParameters.plist to&lt;br /&gt;# /Library/StartupItems/Memcached and add "MEMCACHED=-YES-"&lt;br /&gt;# to /etc/hostconfig ... You can then reboot or execute &lt;br /&gt;# "sudo /sbin/SystemStarter start Memcached" from a terminal&lt;br /&gt;# to start it up.&lt;br /&gt;#&lt;br /&gt;# I should mention that this file uses my preferred development &lt;br /&gt;# settings. You can edit them before copying this script over &lt;br /&gt;# or they can be overriden by creating a config file named &lt;br /&gt;# memcached.conf in /etc, /opt/etc, or /usr/local/etc with the &lt;br /&gt;# following contents (modified to suit, of course):&lt;br /&gt;#&lt;br /&gt;#   MEMCACHED_EXE=/usr/local/bin/memcached&lt;br /&gt;#   MEMCACHED_PIDFILE=/var/run/memcached.pid&lt;br /&gt;#   MEMCACHED_MAX_MEM=128&lt;br /&gt;#   MEMCACHED_INTERFACE=127.0.0.1&lt;br /&gt;#   MEMCACHED_PORT=1121&lt;br /&gt;#   MEMCACHED_RUN_AS=nobody &lt;br /&gt;#&lt;br /&gt;# ------------------------------------------------------------------- &lt;br /&gt;# NOTE: Memcached &lt; 1.2.0 has issues specifying the interface via the&lt;br /&gt;# '-l' switch on the Mac for some reason so if the service fails to &lt;br /&gt;# start with the following error:&lt;br /&gt;#&lt;br /&gt;#    bind(): Can't assign requested address&lt;br /&gt;#    failed to listen&lt;br /&gt;#&lt;br /&gt;# then you will need to either upgrade to version 1.2.0+ or forgo&lt;br /&gt;# specifying an interface to listen on by removing the references&lt;br /&gt;# to A_INTERFACE on lines 65 and 75 of this script. &lt;br /&gt;# ------------------------------------------------------------------- &lt;br /&gt;#&lt;br /&gt;# Tim Ferrell &lt;s0nspark@gmail.com&gt;&lt;br /&gt;#&lt;br /&gt;&lt;br /&gt;# Suppress the annoying "$1: unbound variable" error &lt;br /&gt;if [ -z $1 ] ; then&lt;br /&gt;  echo "Usage: $0 [start|stop|restart] "&lt;br /&gt;  exit 1&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;# Source the common setup functions for startup scripts&lt;br /&gt;test -r /etc/rc.common || exit 1&lt;br /&gt;source /etc/rc.common&lt;br /&gt;&lt;br /&gt;NAME=memcached&lt;br /&gt;DESC="Memcached server"&lt;br /&gt;&lt;br /&gt;# look for a config file name ${NAME}.conf in /etc, /opt/etc, &lt;br /&gt;# and /usr/local/etc ... in that order.&lt;br /&gt;[ -r /etc/${NAME}.conf ] &amp;&amp; source /etc/${NAME}.conf&lt;br /&gt;[ -r /opt/etc/${NAME}.conf ] &amp;&amp; source /opt/etc/${NAME}.conf&lt;br /&gt;[ -r /usr/local/etc/${NAME}.conf ] &amp;&amp; source /usr/local/etc/${NAME}.conf&lt;br /&gt;&lt;br /&gt;# these are _my_ defaults... &lt;br /&gt;DAEMON="${MEMCACHED_EXE:=/usr/local/bin/memcached}"&lt;br /&gt;A_PIDFILE="-P ${MEMCACHED_PIDFILE:=/var/run/memcached.pid}"&lt;br /&gt;A_MAXMEM="-m ${MEMCACHED_MAX_MEM:=128}"&lt;br /&gt;A_INTERFACE="-l ${MEMCACHED_INTERFACE:=127.0.0.1}"&lt;br /&gt;A_PORT="-p ${MEMCACHED_PORT:=11211}"&lt;br /&gt;A_RUNAS="-u ${MEMCACHED_RUN_AS:=nobody}"&lt;br /&gt;&lt;br /&gt;StartService () &lt;br /&gt;{&lt;br /&gt;  if [ "${MEMCACHED:=-NO-}" = "-YES-" ] &amp;&amp; ! GetPID ${NAME} &gt; /dev/null; then&lt;br /&gt;    if [ -f /var/run/${NAME}.StartupItem ] ; then exit ; fi&lt;br /&gt;    touch /var/run/${NAME}.StartupItem&lt;br /&gt;    echo "Starting ${DESC}"&lt;br /&gt;    ${DAEMON} -d ${A_PIDFILE} ${A_MAXMEM} ${A_INTERFACE} ${A_PORT} ${A_RUNAS} &lt;br /&gt;  fi&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;StopService ()&lt;br /&gt;{&lt;br /&gt;  if PID=$(GetPID ${NAME}); then&lt;br /&gt;    echo "Stopping ${DESC}, (PID ${PID})"&lt;br /&gt;    kill -TERM "${PID}"&lt;br /&gt;    rm -f ${A_PIDFILE}&lt;br /&gt;  else&lt;br /&gt;    echo "${DESC} not running."&lt;br /&gt;  fi&lt;br /&gt;  rm -f /var/run/${NAME}.StartupItem&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;RestartService () { StopService; StartService; }&lt;br /&gt;&lt;br /&gt;if test -x $DAEMON ; then&lt;br /&gt;  RunService "$1"&lt;br /&gt;else&lt;br /&gt;  echo "Could not find ${DAEMON}!"&lt;br /&gt;fi&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt; &lt;br /&gt;</description>
      <pubDate>Sun, 17 Sep 2006 22:15:16 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2618</guid>
      <author>s0nspark (Tim Ferrell)</author>
    </item>
    <item>
      <title>Capitalizing titles in Ruby - another take...</title>
      <link>http://snippets.dzone.com/posts/show/557</link>
      <description>This code was inspired by Peter's code from May 17... &lt;br /&gt;&lt;br /&gt;I basically extended his idea to be more flexible and added ! methods to modify the string in place. I chose to use a different regex selector because using "\b" breaks on apostrophes and leave you with words like "You'Re" instead of "You're"...&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;class String&lt;br /&gt;&lt;br /&gt;  def titlecase()&lt;br /&gt;    ignore_list = %w{of etc and by the for on is at to but nor or a via}&lt;br /&gt;    capitalize_all_ex(ignore_list)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def titlecase!()&lt;br /&gt;    ignore_list = %w{of etc and by the for on is at to but nor or a via}&lt;br /&gt;    capitalize_all_ex!(ignore_list)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def capitalize_all(force_downcase = true)&lt;br /&gt;    ignore_list = %w{}&lt;br /&gt;    capitalize_all_ex(ignore_list, force_downcase)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def capitalize_all!(force_downcase = true)&lt;br /&gt;    ignore_list = %w{}&lt;br /&gt;    capitalize_all_ex!(ignore_list, force_downcase)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def capitalize_all_ex(ignore_list, force_downcase = true)&lt;br /&gt;    # if force_downcase is true then the &lt;br /&gt;    # string is, um, downcased first :-)&lt;br /&gt;    if force_downcase&lt;br /&gt;      self.downcase.gsub(/[\w\']+/){ |w| &lt;br /&gt;        ignore_list.include?(w) ? w : w.capitalize  &lt;br /&gt;      }&lt;br /&gt;    else&lt;br /&gt;      self.gsub(/[\w\']+/){ |w| &lt;br /&gt;        ignore_list.include?(w) ? w : w.capitalize  &lt;br /&gt;      }&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def capitalize_all_ex!(ignore_list, force_downcase = true)&lt;br /&gt;    if force_downcase&lt;br /&gt;      self.replace(self.downcase.gsub(/[\w\']+/){ |w| &lt;br /&gt;        ignore_list.include?(w) ? w : w.capitalize  &lt;br /&gt;      })&lt;br /&gt;    else&lt;br /&gt;      self.replace(self.gsub(/[\w\']+/){ |w| &lt;br /&gt;        ignore_list.include?(w) ? w : w.capitalize  &lt;br /&gt;      })&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Sat, 06 Aug 2005 00:09:31 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/557</guid>
      <author>s0nspark (Tim Ferrell)</author>
    </item>
  </channel>
</rss>
