<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: rubyonrails code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 07:21:41 GMT</pubDate>
    <description>DZone Snippets: rubyonrails code</description>
    <item>
      <title>Javascript Rails-like inflector</title>
      <link>http://snippets.dzone.com/posts/show/3205</link>
      <description>I was using Ajax to do some DOM manipulation in a Rails app I was working on.  None of what I was doing needed to involve a round trip to server, the only reason I hit the server at all was to take advantage of Rails' (well, ActiveSupport's) pluralize function.  Also, this technique required an extra action that I decided to eliminate as I try to move to a more RESTful architecture.&lt;br /&gt;&lt;br /&gt;The only part of ActiveSupport's Inflector class I implemented are the ones I needed.  I implemented even fewer of Rails' Inflector-based functions.&lt;br /&gt;&lt;br /&gt;Stick all this in a file, such as "inflector.js".&lt;br /&gt;&lt;code&gt;/*&lt;br /&gt; * This script depends on no outside libraries.&lt;br /&gt; */&lt;br /&gt;&lt;br /&gt;Inflector = {&lt;br /&gt;    /*&lt;br /&gt;     * The order of all these lists has been reversed from the way &lt;br /&gt;     * ActiveSupport had them to keep the correct priority.&lt;br /&gt;     */&lt;br /&gt;    Inflections: {&lt;br /&gt;        plural: [&lt;br /&gt;            [/(quiz)$/i,               "$1zes"  ],&lt;br /&gt;            [/^(ox)$/i,                "$1en"   ],&lt;br /&gt;            [/([m|l])ouse$/i,          "$1ice"  ],&lt;br /&gt;            [/(matr|vert|ind)ix|ex$/i, "$1ices" ],&lt;br /&gt;            [/(x|ch|ss|sh)$/i,         "$1es"   ],&lt;br /&gt;            [/([^aeiouy]|qu)y$/i,      "$1ies"  ],&lt;br /&gt;            [/(hive)$/i,               "$1s"    ],&lt;br /&gt;            [/(?:([^f])fe|([lr])f)$/i, "$1$2ves"],&lt;br /&gt;            [/sis$/i,                  "ses"    ],&lt;br /&gt;            [/([ti])um$/i,             "$1a"    ],&lt;br /&gt;            [/(buffal|tomat)o$/i,      "$1oes"  ],&lt;br /&gt;            [/(bu)s$/i,                "$1ses"  ],&lt;br /&gt;            [/(alias|status)$/i,       "$1es"   ],&lt;br /&gt;            [/(octop|vir)us$/i,        "$1i"    ],&lt;br /&gt;            [/(ax|test)is$/i,          "$1es"   ],&lt;br /&gt;            [/s$/i,                    "s"      ],&lt;br /&gt;            [/$/,                      "s"      ]&lt;br /&gt;        ],&lt;br /&gt;        singular: [&lt;br /&gt;            [/(quiz)zes$/i,                                                    "$1"     ],&lt;br /&gt;            [/(matr)ices$/i,                                                   "$1ix"   ],&lt;br /&gt;            [/(vert|ind)ices$/i,                                               "$1ex"   ],&lt;br /&gt;            [/^(ox)en/i,                                                       "$1"     ],&lt;br /&gt;            [/(alias|status)es$/i,                                             "$1"     ],&lt;br /&gt;            [/(octop|vir)i$/i,                                                 "$1us"   ],&lt;br /&gt;            [/(cris|ax|test)es$/i,                                             "$1is"   ],&lt;br /&gt;            [/(shoe)s$/i,                                                      "$1"     ],&lt;br /&gt;            [/(o)es$/i,                                                        "$1"     ],&lt;br /&gt;            [/(bus)es$/i,                                                      "$1"     ],&lt;br /&gt;            [/([m|l])ice$/i,                                                   "$1ouse" ],&lt;br /&gt;            [/(x|ch|ss|sh)es$/i,                                               "$1"     ],&lt;br /&gt;            [/(m)ovies$/i,                                                     "$1ovie" ],&lt;br /&gt;            [/(s)eries$/i,                                                     "$1eries"],&lt;br /&gt;            [/([^aeiouy]|qu)ies$/i,                                            "$1y"    ],&lt;br /&gt;            [/([lr])ves$/i,                                                    "$1f"    ],&lt;br /&gt;            [/(tive)s$/i,                                                      "$1"     ],&lt;br /&gt;            [/(hive)s$/i,                                                      "$1"     ],&lt;br /&gt;            [/([^f])ves$/i,                                                    "$1fe"   ],&lt;br /&gt;            [/(^analy)ses$/i,                                                  "$1sis"  ],&lt;br /&gt;            [/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i, "$1$2sis"],&lt;br /&gt;            [/([ti])a$/i,                                                      "$1um"   ],&lt;br /&gt;            [/(n)ews$/i,                                                       "$1ews"  ],&lt;br /&gt;            [/s$/i,                                                            ""       ]&lt;br /&gt;        ],&lt;br /&gt;        irregular: [&lt;br /&gt;            ['move',   'moves'   ],&lt;br /&gt;            ['sex',    'sexes'   ],&lt;br /&gt;            ['child',  'children'],&lt;br /&gt;            ['man',    'men'     ],&lt;br /&gt;            ['person', 'people'  ]&lt;br /&gt;        ],&lt;br /&gt;        uncountable: [&lt;br /&gt;            "sheep",&lt;br /&gt;            "fish",&lt;br /&gt;            "series",&lt;br /&gt;            "species",&lt;br /&gt;            "money",&lt;br /&gt;            "rice",&lt;br /&gt;            "information",&lt;br /&gt;            "equipment"&lt;br /&gt;        ]&lt;br /&gt;    },&lt;br /&gt;    ordinalize: function(number) {&lt;br /&gt;        if (11 &lt;= parseInt(number) % 100 &amp;&amp; parseInt(number) % 100 &lt;= 13) {&lt;br /&gt;            return number + "th";&lt;br /&gt;        } else {&lt;br /&gt;            switch (parseInt(number) % 10) {&lt;br /&gt;                case  1: return number + "st";&lt;br /&gt;                case  2: return number + "nd";&lt;br /&gt;                case  3: return number + "rd";&lt;br /&gt;                default: return number + "th";&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    },&lt;br /&gt;    pluralize: function(word) {&lt;br /&gt;        for (var i = 0; i &lt; Inflector.Inflections.uncountable.length; i++) {&lt;br /&gt;            var uncountable = Inflector.Inflections.uncountable[i];&lt;br /&gt;            if (word.toLowerCase == uncountable) {&lt;br /&gt;                return uncountable;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        for (var i = 0; i &lt; Inflector.Inflections.irregular.length; i++) {&lt;br /&gt;            var singular = Inflector.Inflections.irregular[i][0];&lt;br /&gt;            var plural   = Inflector.Inflections.irregular[i][1];&lt;br /&gt;            if ((word.toLowerCase == singular) || (word == plural)) {&lt;br /&gt;                return plural;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        for (var i = 0; i &lt; Inflector.Inflections.plural.length; i++) {&lt;br /&gt;            var regex          = Inflector.Inflections.plural[i][0];&lt;br /&gt;            var replace_string = Inflector.Inflections.plural[i][1];&lt;br /&gt;            if (regex.test(word)) {&lt;br /&gt;                return word.replace(regex, replace_string);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function ordinalize(number) {&lt;br /&gt;    return Inflector.ordinalize(number);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/*&lt;br /&gt; * Javascript doesn't have optional parameters or overloading so I had to use&lt;br /&gt; * the ever popular pseudo options hash object technique.&lt;br /&gt; * required properties:&lt;br /&gt; *     count    Number of objects to pluralize&lt;br /&gt; *     singular Singular noun for the objects&lt;br /&gt; * optional property:&lt;br /&gt; *     plural   Plural (probably irregular) noun for the objects&lt;br /&gt; * examples:&lt;br /&gt; *      pluralize({ count: total_count, singular: "Issue" })&lt;br /&gt; *      pluralize({ count: total_count, singular: "Goose", plural: "Geese" })&lt;br /&gt; */&lt;br /&gt;function pluralize(options) {&lt;br /&gt;    return options.count + " " + (1 == parseInt(options.count) ?&lt;br /&gt;        options.singular :&lt;br /&gt;        options.plural || Inflector.pluralize(options.singular));&lt;br /&gt;}&lt;/code&gt;</description>
      <pubDate>Tue, 26 Dec 2006 07:37:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3205</guid>
      <author>sporkyy (Todd Sayre)</author>
    </item>
    <item>
      <title>Number to Currency with Cents</title>
      <link>http://snippets.dzone.com/posts/show/2581</link>
      <description>A slight alteration to the default Rails currency formatting helper to show numbers in cents if the number is less than $1.00.  For example $0.99 would instead become 99&amp;cent;.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def number_to_currency_with_cents(number, options = {})&lt;br /&gt;    options = options.stringify_keys&lt;br /&gt;    precision = options.delete('precision') { 2 }&lt;br /&gt;    unit = options.delete('unit') { '$' }&lt;br /&gt;    fractional_unit = options.delete('fractional_unit') { '&amp;cent;' }&lt;br /&gt;    separator = options.delete('separator') { '.' }&lt;br /&gt;    delimiter = options.delete('delimiter') { ',' }&lt;br /&gt;    separator = '' unless precision &gt; 0&lt;br /&gt;    begin&lt;br /&gt;        fraction = number.abs % 1.0&lt;br /&gt;        body = number.floor&lt;br /&gt;        if body != 0 || body == 0 &amp;&amp; fraction == 0 then&lt;br /&gt;            parts = number_with_precision(number, precision).split('.')&lt;br /&gt;            unit + number_with_delimiter(parts[0], delimiter) + separator + parts[1].to_s&lt;br /&gt;        else&lt;br /&gt;            (fraction * 100).to_i.to_s + fractional_unit&lt;br /&gt;        end&lt;br /&gt;    rescue&lt;br /&gt;        number&lt;br /&gt;    end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;I'm really tempted to go through and replace that whole thing with my own code, but it works, so I'm happy.</description>
      <pubDate>Mon, 11 Sep 2006 05:58:03 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2581</guid>
      <author>sporkyy (Todd Sayre)</author>
    </item>
    <item>
      <title>PrototypeHelper :with helper</title>
      <link>http://snippets.dzone.com/posts/show/2216</link>
      <description>This method is for use in conjuction with the Ruby on Rails Module &lt;a href="http://api.rubyonrails.com/classes/ActionView/Helpers/PrototypeHelper.html"&gt;ActionView::Helpers::PrototypeHelper&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;The options hash takes any number of key/value pairs the key becomes the name of the parameter passed in the with value and the value becomes a javascript expression&lt;br /&gt;&lt;br /&gt;key &#8212; name of the parameter&lt;br /&gt;value &#8212; a fragment of javascript that will be the value of the parameter&lt;br /&gt;&lt;br /&gt;Example&lt;br /&gt;&lt;pre&gt;&gt;&gt; params_for_with(:clean_range =&gt; 'clean_range', :raw_range =&gt; 'raw_range', :total_count =&gt; 'total_count')&lt;br /&gt;=&gt; 'total_count=' + total_count + '&amp;' + 'clean_range=' + clean_range + '&amp;' + 'raw_range=' + raw_range&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Don&#8216;t forget you won&#8217;t necessarily get the parameters out in the same order you put them in, such is the nature of hashes&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def params_for_with(options = {})&lt;br /&gt;    options.collect { |param_name, js_fragment| "'#{param_name}='+#{js_fragment}" }.join("+'&amp;'+")&lt;br /&gt;    # or this one&lt;br /&gt;    #options.stringify_keys.collect { |param_name, js_fragment| '"' &lt;&lt; param_name &lt;&lt; '="+' &lt;&lt; js_fragment }.join('+"&amp;"+')&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 18 Jun 2006 20:52:07 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2216</guid>
      <author>sporkyy (Todd Sayre)</author>
    </item>
  </channel>
</rss>
