<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Fauxparse's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Wed, 20 Aug 2008 05:28:40 GMT</pubDate>
    <description>DZone Snippets: Fauxparse's Code Snippets</description>
    <item>
      <title>Obfuscate email addresses in TextMate</title>
      <link>http://snippets.dzone.com/posts/show/3918</link>
      <description>// Textmate / e command for obfuscating email addresses&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/env ruby&lt;br /&gt;&lt;br /&gt;email = STDIN.read&lt;br /&gt;url_email = email.gsub(/./) { |c| '%' + c.unpack('H2' * c.size).join('%').upcase }&lt;br /&gt;html_email = url_email[1..-1].split(/%/).collect { |c| sprintf("&amp;#%03d;", c.to_i(16)) }.join&lt;br /&gt;&lt;br /&gt;print "&lt;a href=\"mailto:#{url_email}\"&gt;#{html_email}&lt;/a&gt;"&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 27 Apr 2007 01:24:07 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3918</guid>
      <author>fauxparse (Matt Powell)</author>
    </item>
    <item>
      <title>Partial iCal RECUR analogue in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/3855</link>
      <description>// Implements salient bits of section 4.3.10 of the iCal spec (RFC2445)&lt;br /&gt;// Doesn't support recurrence granularities of less than a day,&lt;br /&gt;// or BYYEARDAY or BYWEEKNO, because... well, I couldn't be bothered ;)&lt;br /&gt;// See also http://www.ietf.org/rfc/rfc2445.txt&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'date'&lt;br /&gt;require 'profiler' # for testing&lt;br /&gt;&lt;br /&gt;class RecurringEvent&lt;br /&gt;  def initialize(options = {})&lt;br /&gt;    @options = check_options(options)&lt;br /&gt;    @options[:freq] ||= :daily&lt;br /&gt;    @options[:interval] ||= 1&lt;br /&gt;    @options[:start_time] ||= Time.now - Time.now.sec&lt;br /&gt;    @options[:by_month] = [ @options[:by_month] ].flatten if @options[:by_month]&lt;br /&gt;    @options[:by_month_day] = [ @options[:by_month_day] ].flatten if @options[:by_month_day]&lt;br /&gt;    @options[:by_day] = [ @options[:by_day] ].flatten if @options[:by_day]&lt;br /&gt;    @options[:by_set_pos] = [ @options[:by_set_pos] ].flatten if @options[:by_set_pos]&lt;br /&gt;    Date::DAYNAMES&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def start_time&lt;br /&gt;    @options[:start_time]&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def start_date&lt;br /&gt;    @start_date ||= Date.civil(start_time.year, start_time.month, start_time.day)&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def frequency&lt;br /&gt;    @options[:freq]&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def each(from = nil, to = nil)&lt;br /&gt;    from ||= Date.today - Date.today.mday&lt;br /&gt;    to ||= (from &gt;&gt; 1) - 1&lt;br /&gt;    times = in_range(from, to)&lt;br /&gt;    &lt;br /&gt;    times.each { |t| yield t } if block_given?&lt;br /&gt;    times&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  alias_method :each_in, :each&lt;br /&gt;  &lt;br /&gt;private&lt;br /&gt;  def in_range(from, to)&lt;br /&gt;    window_from = from &lt;&lt; 1&lt;br /&gt;    window_to = to &gt;&gt; 1&lt;br /&gt;    &lt;br /&gt;    start = Date.civil(start_time.year, start_time.month, start_time.day)&lt;br /&gt;    interval = @options[:interval] || 1&lt;br /&gt;&lt;br /&gt;    s_year = start.year % interval&lt;br /&gt;    s_month = (start.year * 12 + start.month) % interval&lt;br /&gt;    s_week = start.week % interval&lt;br /&gt;    s_day = start.julian_day % interval&lt;br /&gt;    &lt;br /&gt;    dates = (window_from..window_to).select { |d|&lt;br /&gt;      in_interval = (interval == 1) or case frequency&lt;br /&gt;      when :yearly  then d.year % interval == s_year&lt;br /&gt;      when :monthly then (d.year * 12 + d.month) % interval == s_month&lt;br /&gt;      when :weekly  then d.week % interval == s_week&lt;br /&gt;      when :daily   then d.julian_day % interval == s_day&lt;br /&gt;      end&lt;br /&gt;    &lt;br /&gt;      if in_interval&lt;br /&gt;        match_by_parts(d)&lt;br /&gt;      else&lt;br /&gt;        false&lt;br /&gt;      end&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;    hours = @options[:by_hour] ? @options[:by_hour] : [ start_time.hour ]&lt;br /&gt;    minutes = @options[:by_minute] ? @options[:by_minute] : [ start_time.min ]&lt;br /&gt;    times = dates.collect { |d|&lt;br /&gt;      hours.collect { |h|&lt;br /&gt;        minutes.collect { |m|&lt;br /&gt;          DateTime.civil(d.year, d.month, d.day, h, m, 0)&lt;br /&gt;        }&lt;br /&gt;      }&lt;br /&gt;    }.flatten&lt;br /&gt;    &lt;br /&gt;    if @options[:by_set_pos]&lt;br /&gt;      grouped_times = case frequency&lt;br /&gt;      when :yearly then times.group_by { |t| t.year }&lt;br /&gt;      when :monthly then times.group_by { |t| t.month }&lt;br /&gt;      when :weekly then times.group_by { |t| t.week }&lt;br /&gt;      when :daily then times.group_by { |t| t.day }&lt;br /&gt;      end&lt;br /&gt;      &lt;br /&gt;      times = grouped_times.collect { |group|&lt;br /&gt;        @options[:by_set_pos].collect { |i| group.at(i) }.compact&lt;br /&gt;      }.flatten&lt;br /&gt;    end&lt;br /&gt;    &lt;br /&gt;    times = times.first(@options[:count]) if @options[:count]&lt;br /&gt;    to = @options[:until] if @options[:until] and @options[:until] &lt; to&lt;br /&gt;    range = (from..to)&lt;br /&gt;    times.select { |t| range.include? t }&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def match_by_parts(d)&lt;br /&gt;    case frequency&lt;br /&gt;    when :yearly, :daily then&lt;br /&gt;      begin&lt;br /&gt;        (@options[:by_month] ? @options[:by_month].include?(d.month) : d.month == start.month) and &lt;br /&gt;        (@options[:by_month_day] ? @options[:by_month_day].include?(d.day) : d.day == start.day) and&lt;br /&gt;        (@options[:by_day].nil? or @options[:by_day].include?(d.week_day))&lt;br /&gt;      end&lt;br /&gt;    when :monthly then&lt;br /&gt;      begin&lt;br /&gt;        if @options[:by_month_day]&lt;br /&gt;          @options[:by_month_day].include?(d.day)&lt;br /&gt;        elsif @options[:by_day]&lt;br /&gt;          @options[:by_day].include?(d.week_day)&lt;br /&gt;        else&lt;br /&gt;          d.day == start.day&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;    when :weekly then&lt;br /&gt;      begin&lt;br /&gt;        (@options[:by_month].nil? or @options[:by_month].include?(d.month)) and &lt;br /&gt;        (@options[:by_day] ? @options[:by_day].include?(d.week_day) : d.week_day == start.week_day)&lt;br /&gt;      end&lt;br /&gt;    else&lt;br /&gt;      true&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def check_options(options)&lt;br /&gt;    options&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;class Date&lt;br /&gt;  def week&lt;br /&gt;    @week ||= julian_day / 7&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def week_day&lt;br /&gt;    @week_day ||= wday&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;  def julian_day&lt;br /&gt;    @julian_day ||= mjd&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;class Array&lt;br /&gt;  def group_by # =&gt; Array&lt;br /&gt;    current_key = nil&lt;br /&gt;    results = []&lt;br /&gt;    current_set = []&lt;br /&gt;    each do |item|&lt;br /&gt;      key = yield(item)&lt;br /&gt;      if key != current_key&lt;br /&gt;        results &lt;&lt; current_set unless current_set.empty?&lt;br /&gt;        current_set = []&lt;br /&gt;        current_key = key&lt;br /&gt;      end&lt;br /&gt;      current_set &lt;&lt; item unless key === false&lt;br /&gt;    end&lt;br /&gt;    results &lt;&lt; current_set unless current_set.empty?&lt;br /&gt;    results&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;Profiler__::start_profile&lt;br /&gt;e = RecurringEvent.new :start_time =&gt; Time.local(2007, 1, 1, 12, 34), :freq =&gt; :monthly, :by_day =&gt; [1], :by_set_pos =&gt; [ 1 ]&lt;br /&gt;e.each(Date.today, Date.today + 365) do |d|&lt;br /&gt;  print "#{d.strftime('%I:%M %a %d %b %Y')}\n"&lt;br /&gt;end&lt;br /&gt;Profiler__::stop_profile&lt;br /&gt;Profiler__::print_profile($stdout)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 23 Apr 2007 05:28:37 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3855</guid>
      <author>fauxparse (Matt Powell)</author>
    </item>
    <item>
      <title>One-line shuffle in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/3713</link>
      <description>&lt;code&gt;&lt;br /&gt;  class Enumerable&lt;br /&gt;    def shuffle&lt;br /&gt;      sort { rand(3) - 1 }&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 22 Mar 2007 00:50:43 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3713</guid>
      <author>fauxparse (Matt Powell)</author>
    </item>
  </channel>
</rss>
