<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Demimismo's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 21 Aug 2008 15:00:31 GMT</pubDate>
    <description>DZone Snippets: Demimismo's Code Snippets</description>
    <item>
      <title>Setting user locale in Globalize, the working solution</title>
      <link>http://snippets.dzone.com/posts/show/5917</link>
      <description>&lt;a href="http://www.globalize-rails.org/pages/example-application"&gt;This example application&lt;/a&gt;, provided as Globalize's, documentation doesn't work at all.&lt;br /&gt;&lt;br /&gt;This is what worked for me:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;code&gt;&lt;br /&gt;    before_filter :set_locale&lt;br /&gt;&lt;br /&gt;    def set_locale&lt;br /&gt;      request_language = request.env['HTTP_ACCEPT_LANGUAGE']&lt;br /&gt;      request_language = request_language.nil? ? nil : request_language[/[^,;]+/].split('-')[0]&lt;br /&gt;&lt;br /&gt;      @locale = params[:locale] || request_language || Locale.base_language.code&lt;br /&gt;&lt;br /&gt;      if !params[:locale].nil? &amp;&amp; LOCALES.keys.include?(params[:locale].to_sym)&lt;br /&gt;        Locale.set LOCALES[@locale.to_sym]&lt;br /&gt;      else&lt;br /&gt;        redirect_to params.merge('locale' =&gt; @locale)&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;</description>
      <pubDate>Thu, 14 Aug 2008 10:19:07 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5917</guid>
      <author>demimismo (David Arango)</author>
    </item>
    <item>
      <title>Sharing has_many extensions</title>
      <link>http://snippets.dzone.com/posts/show/5810</link>
      <description>Sometimes you extend an ActiveRecord association this way:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;has_many :things do&lt;br /&gt;  def active&lt;br /&gt;    find :all, :conditions =&gt; ['active = ?', true]&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;You can share the same extensions using a lambda:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;extensions = lambda {&lt;br /&gt;  def active&lt;br /&gt;    find :all, :conditions =&gt; ['active = ?', true]&lt;br /&gt;  end&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;has_many :things, &amp;extensions&lt;br /&gt;has_many :more_things, &amp;extensions&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Wed, 23 Jul 2008 12:21:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5810</guid>
      <author>demimismo (David Arango)</author>
    </item>
    <item>
      <title>Processing large CSV files</title>
      <link>http://snippets.dzone.com/posts/show/5627</link>
      <description>&lt;code&gt;&lt;br /&gt;  # this method allows fast CSV processing, it reads file_name, generates a new&lt;br /&gt;  # CSV file and inserts all data in a mysql table using LOAD DATA INFILE&lt;br /&gt;&lt;br /&gt;    # you can use it like this:&lt;br /&gt;&lt;br /&gt;    load_csv_data(PRELOAD_DIR+'socios.csv', 'users') do |csv, thing_id, row|&lt;br /&gt;      csv &lt;&lt; [thing_id,&lt;br /&gt;        row['NOMBRE'].to_s.to_permalink+'.'+row['APELLIDOS'].to_s.to_permalink, #  login&lt;br /&gt;        row['E_MAIL'], #  email&lt;br /&gt;        row['SEXO'],  #  gender&lt;br /&gt;        (row['NO_DATOS'] == 'S' || row['NO_DATOS'] == 'VERDADERO' ? 1 : 0 ), #  no_datos&lt;br /&gt;      ]&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;  def load_csv_data(file_name, table_name)&lt;br /&gt;    STDERR.print("\nParsing '#{file_name}' to insert data in '#{table_name}'\n")&lt;br /&gt;&lt;br /&gt;    n_lines = (%x[wc -l #{file_name}]).split(' ')[0].to_f&lt;br /&gt;    time_start = Time.now&lt;br /&gt;    begin&lt;br /&gt;      require 'fastercsv'&lt;br /&gt;      count = 0.0&lt;br /&gt;&lt;br /&gt;      csv_string = FasterCSV.open("data.csv", "w", { :col_sep =&gt; ";", :force_quotes =&gt; true })  do |csv|&lt;br /&gt;      FasterCSV.open("#{file_name}", { :col_sep =&gt; "\t", :headers =&gt; :first_row }).each do |row|&lt;br /&gt;        thing_id = row[0].to_i&lt;br /&gt;        if thing_id &amp;&amp; thing_id &gt; 0&lt;br /&gt;          &lt;br /&gt;          yield csv, thing_id, row&lt;br /&gt;&lt;br /&gt;          percentage = ((count += 1) * 100.0)/n_lines&lt;br /&gt;          STDERR.print "%.0f%%..." % percentage if (percentage.modulo(2.0) &lt; 0.001)&lt;br /&gt;        end&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;    rescue EOFError&lt;br /&gt;      f.close&lt;br /&gt;    end&lt;br /&gt;    total_lines = (%x[wc -l data.csv]).split(' ')[0].to_i&lt;br /&gt;    STDERR.print("\nInserting #{total_lines} lines in table '#{table_name}'\n")&lt;br /&gt;    ActiveRecord::Base.connection.execute("LOAD DATA INFILE '#{File.join(File.dirname(__FILE__), '..', '..', 'data.csv')}' IGNORE INTO TABLE #{table_name} FIELDS TERMINATED BY ';' ENCLOSED BY '\"';")&lt;br /&gt;    warn("Finished importing '#{file_name}'.\nLines: #{n_lines.to_i.to_s} (#{(n_lines.to_i - total_lines.to_i).to_s} lost)\nTime: %.2f minutes\n" % ((Time.now - time_start)/60))&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 10 Jun 2008 11:17:48 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5627</guid>
      <author>demimismo (David Arango)</author>
    </item>
    <item>
      <title>Merry xmas</title>
      <link>http://snippets.dzone.com/posts/show/3139</link>
      <description>&lt;code&gt;&lt;br /&gt;3.times  {|i| puts 'h'+'o'*(i+1)+"!"}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 12 Dec 2006 21:49:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3139</guid>
      <author>demimismo (David Arango)</author>
    </item>
    <item>
      <title>Extracting web page title</title>
      <link>http://snippets.dzone.com/posts/show/3060</link>
      <description>Easy extracting html 'title' element from an url&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'open-uri'&lt;br /&gt;&lt;br /&gt;URI.parse('http://google.es').open do |f|&lt;br /&gt;	f.each {|l| if md = (/&lt;title&gt;\s*(.*)\s*&lt;\/title&gt;/iu).match(l) then print md[1] end }&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Wed, 29 Nov 2006 21:24:59 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3060</guid>
      <author>demimismo (David Arango)</author>
    </item>
  </channel>
</rss>
