<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Mkovacs's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 02:30:59 GMT</pubDate>
    <description>DZone Snippets: Mkovacs's Code Snippets</description>
    <item>
      <title>Multi-part parameter extractor</title>
      <link>http://snippets.dzone.com/posts/show/2476</link>
      <description>For those times when you want to get a date object out of the multi-part parameters without having to do a mass assignment to a model object. I extracted this code directly from AR so I think it should be pretty solid but I have not tested it so your mileage may vary.&lt;br /&gt;&lt;br /&gt;Take the following and place it into your application helper or whatever helper you'd like. To use it you simply pass in an instance of the object on which the multi-part parameter exists. This is so that the type to instantiate can be reflected from the column information. Then you specify the attribute name of the parameter you're looking to create from the next parameter which is the hash containing the multi-part parameter value.&lt;br /&gt;&lt;br /&gt;example:&lt;br /&gt;for a form containing dude[birth_date] you'd call the method like this:&lt;br /&gt;&lt;br /&gt;extract_date(@the_dude, 'birth_date', params[:dude])&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;    def extract_date(parent_object, param_name, atts)&lt;br /&gt;      atts.stringify_keys!&lt;br /&gt;&lt;br /&gt;      multi_parameter_attributes = []&lt;br /&gt;      atts.each do |k, v|&lt;br /&gt;        multi_parameter_attributes &lt;&lt; [ k, v ] if k.include?(param_name+"(")&lt;br /&gt;      end&lt;br /&gt;      &lt;br /&gt;      attributes = { }&lt;br /&gt;&lt;br /&gt;      for pair in multi_parameter_attributes&lt;br /&gt;        multiparameter_name, value = pair&lt;br /&gt;        attribute_name = multiparameter_name.split("(").first&lt;br /&gt;        attributes[attribute_name] = [] unless attributes.include?(attribute_name)&lt;br /&gt;&lt;br /&gt;        unless value.empty?&lt;br /&gt;          attributes[attribute_name] &lt;&lt;&lt;br /&gt;            [ find_parameter_position(multiparameter_name), type_cast_attribute_value(multiparameter_name, value) ]&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;&lt;br /&gt;      attributes.each { |name, values| attributes[name] = values.sort_by{ |v| v.first }.collect { |v| v.last } }&lt;br /&gt;      errors = []&lt;br /&gt;      result = nil&lt;br /&gt;      attributes.each do |name, values|  &lt;br /&gt;        klass = (parent_object.class.reflect_on_aggregation(name.to_sym) || parent_object.column_for_attribute(name)).klass&lt;br /&gt;        result = Time == klass ? klass.local(*values) : klass.new(*values)&lt;br /&gt;      end&lt;br /&gt;&lt;br /&gt;      return result&lt;br /&gt;    end&lt;br /&gt;    &lt;br /&gt;    def find_parameter_position(multiparameter_name)&lt;br /&gt;      multiparameter_name.scan(/\(([0-9]*).*\)/).first.first&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    def type_cast_attribute_value(multiparameter_name, value)&lt;br /&gt;      multiparameter_name =~ /\([0-9]*([a-z])\)/ ? value.send("to_" + $1) : value&lt;br /&gt;    end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 27 Aug 2006 07:12:24 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2476</guid>
      <author>mkovacs ()</author>
    </item>
    <item>
      <title>parameterized instance variables</title>
      <link>http://snippets.dzone.com/posts/show/1082</link>
      <description>// useful when you want to obtain more than one instance of the same type of variable on the &lt;br /&gt;// same page. i.e. two different addresses. credit goes to argv[0] in #rubyonrails on freenode &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;instance_variable_get("@#{address_type}_address").country.name&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 10 Jan 2006 04:24:03 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1082</guid>
      <author>mkovacs ()</author>
    </item>
  </channel>
</rss>
