<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users1062friends</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/1062/friends" rel="self"/>
  <title>Dmitry Polushkin friends</title>
  <updated>Wed May 27 17:38:58 -0700 2009</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor159551</id>
    <published>2009-05-27T17:38:58-07:00</published>
    <title>[JavaScript] On RESTful jQuery PUT/DELETE links</title>
    <content type="html">

&lt;pre&gt;jQuery.fn.restForm = function(type, options) {
  var defaults = {
    method: 'post',
    action: this.attr('href'),
    confirm: false,
    confirm_message: 'Are you sure?',
    trigger_on: 'click'
  };
  var opts = $.extend(defaults, options);

  this.bind(opts.trigger_on, function() {
    var $form = $('&amp;lt;form&amp;gt;&amp;lt;/form&amp;gt;').attr({
      method: opts.method,
      action: opts.action
    }).append('&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;_method&amp;quot; value=&amp;quot;' + type + '&amp;quot; /&amp;gt;');

    if (opts.confirm &amp;amp;&amp;amp; !confirm(opts.confirm_message)) {
      return false;
    }

    this.after($form);
    $form.submit();
    return false;
  });
};

$(function() {
  $('.put').restForm('PUT');
  $('.delete').restForm('DELETE', {confirm: true});
});&lt;/pre&gt;</content>
    <author>
      <name>Simo Niemel&#195;&#164;</name>
      <email>simo.niemela@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/889-restful-jquery-put-delete-links/refactors/159551" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor147375</id>
    <published>2009-02-16T09:52:21-08:00</published>
    <title>[JavaScript] On Perform AJAX request when three form fields have been filled</title>
    <content type="html">

&lt;pre&gt;// form fields  
var address = $(&amp;quot;#address&amp;quot;);
var zip = $(&amp;quot;#zip&amp;quot;);
var city = $(&amp;quot;#city&amp;quot;);

function validateAddress() {
  return (address.val().length &amp;gt; 1);
}

function validateZip() {
  var pattern = new RegExp('[^0-9]+', 'g');
	var zipReplaced = zip.val().replace(pattern, '');
	zip.val(zipReplaced);
	
  return (zip.val().length == 5);
}

function validateCity() {
  return (city.val().length &amp;gt; 1);
}

// Show indicator
function showIndicator() {
  $(&amp;quot;img#indicator&amp;quot;).show();
}

// Hide indicator
function hideIndicator() {
  $(&amp;quot;img#indicator&amp;quot;).hide();
}

function validateFields() {
  return (validateAddress() &amp;amp;&amp;amp; validateZip() &amp;amp;&amp;amp; validateCity());
}

// On blur
$(&amp;quot;#address,#zip,#city&amp;quot;).blur(function() {
  if (validateFields()) {
    // Show indicator
    showIndicator();

    var req = $(&amp;quot;form#new&amp;quot;).serialize();
    $.ajax({
      type: &amp;quot;POST&amp;quot;,
      url: &amp;quot;checkDuplicate/&amp;quot;,
      data: req,
      complete: hideIndicator
    });
  }
});&lt;/pre&gt;</content>
    <author>
      <name>Simo Niemel&#195;&#164;</name>
      <email>simo.niemela@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/760-perform-ajax-request-when-three-form-fields-have-been-filled/refactors/147375" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor144734</id>
    <published>2009-01-27T18:05:44-08:00</published>
    <title>[JavaScript] On Get the id of a &lt;select&gt; with jQuery</title>
    <content type="html">

&lt;pre&gt;$('.answer_filter').change(function() {
  var filter = $(this);
  var select = $('#' + filter.attr('id'));
});&lt;/pre&gt;</content>
    <author>
      <name>Simo Niemel&#195;&#164;</name>
      <email>simo.niemela@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/722-get-the-id-of-a-select-with-jquery/refactors/144734" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor144523</id>
    <published>2009-01-25T21:10:55-08:00</published>
    <title>[JavaScript] On Check next option in select tag</title>
    <content type="html">

&lt;pre&gt;jQuery.fn.checkNext = function() {
  if (this.is(':last-child') &amp;amp;&amp;amp; this.is(':selected')) {
    this.attr('selected', false).parent().children(':first').attr('selected', true);
  } else if (this.is(':selected')) {
    this.attr('selected', false).next().attr('selected', true);
  }
}

$(function(){
  $('select#el &amp;gt; option[selected]').checkNext();
});&lt;/pre&gt;</content>
    <author>
      <name>Simo Niemel&#195;&#164;</name>
      <email>simo.niemela@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/716-check-next-option-in-select-tag/refactors/144523" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor144495</id>
    <published>2009-01-25T15:16:31-08:00</published>
    <title>[PHP] On From array to list</title>
    <content type="html">

&lt;pre&gt;&amp;lt;?php

function array_to_list($array, $separator = ',')
{
  return join($separator, $array);
}
&lt;/pre&gt;</content>
    <author>
      <name>Simo Niemel&#195;&#164;</name>
      <email>simo.niemela@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/711-from-array-to-list/refactors/144495" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor128247</id>
    <published>2008-12-18T14:01:18-08:00</published>
    <title>[Ruby] On Ugly RegEx Accessor</title>
    <content type="html">

&lt;pre&gt;category_id = category_name.split(&amp;quot;-&amp;quot;).last&lt;/pre&gt;</content>
    <author>
      <name>Simo Niemel&#195;&#164;</name>
      <email>simo.niemela@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/674-ugly-regex-accessor/refactors/128247" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor80775</id>
    <published>2008-11-22T11:12:26-08:00</published>
    <title>[Ruby] On Bindings on caller</title>
    <content type="html">

&lt;pre&gt;def capitalize_args(bind)
  eval(&amp;quot;local_variables&amp;quot;, bind).each do |var|
    var = eval(var, bind)
    var.capitalize! if var.is_a?(String)
    var.map! { |arg| arg.is_a?(String) ? arg.capitalize : arg } if var.is_a?(Array)
  end
end

def test(a, b)
  capitalize_args(binding)
  puts a
  puts b
end

def test2(*args)
  capitalize_args(binding)
  puts args
end

test &amp;quot;hello&amp;quot;, &amp;quot;world&amp;quot;
# =&amp;gt; Hello
# =&amp;gt; World

test2 &amp;quot;hello&amp;quot;, 1, &amp;quot;world&amp;quot;
# =&amp;gt; Hello
# =&amp;gt; 1
# =&amp;gt; World&lt;/pre&gt;</content>
    <author>
      <name>Simo Niemel&#195;&#164;</name>
      <email>simo.niemela@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/618-bindings-on-caller/refactors/80775" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor67502</id>
    <published>2008-11-11T10:16:26-08:00</published>
    <title>[PHP] On remove http from url string</title>
    <content type="html">

&lt;pre&gt;&amp;lt;?php

function remove_http($url)
{
  return ereg_replace(&amp;quot;(https?)://&amp;quot;, &amp;quot;&amp;quot;, $url);
}

?&amp;gt;&lt;/pre&gt;</content>
    <author>
      <name>Simo Niemel&#195;&#164;</name>
      <email>simo.niemela@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/598-remove-http-from-url-string/refactors/67502" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor59486</id>
    <published>2008-11-04T11:46:37-08:00</published>
    <title>[Java] On Java IRC Bot</title>
    <content type="html">

&lt;pre&gt;## Request.java

public boolean hasOwnerShip()
{
	return this.find(&amp;quot;^:&amp;quot;+MANDATORY_OWNER);
}

public boolean isPing()
{
	return this.find(&amp;quot;^PING&amp;quot;);
}

// ...

private boolean find(String command)
{
	Pattern regex = Pattern.compile(command, Pattern.CASE_INSENSITIVE);
	Matcher match = regex.matcher(this.line);
	return match.find();
}

private boolean find(String command, int flags)
{
	Pattern regex = Pattern.compile(command, flags);
	Matcher match = regex.matcher(this.line);
	return match.find();
}&lt;/pre&gt;</content>
    <author>
      <name>Simo Niemel&#195;&#164;</name>
      <email>simo.niemela@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/490-java-irc-bot/refactors/59486" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor55193</id>
    <published>2008-10-15T17:00:31-07:00</published>
    <title>[Ruby] On Open hours grouping</title>
    <content type="html">

&lt;pre&gt;dates = {
  :mon =&amp;gt; '9:00 - 20:00',
  :tue =&amp;gt; '9:00 - 20:00',
  :wed =&amp;gt; '10:00 - 21:00',
  :thu =&amp;gt; '9:00 - 20:00',
  :fri =&amp;gt; '8:00 - 21:00',
  :sat =&amp;gt; 'closed',
  :sun =&amp;gt; 'closed'
}

class Hash
  def group
    result = {}
    self.each do |k, v|
      next if result.has_value?(v)
      keys = []
      self.each { |key, value| keys &amp;lt;&amp;lt; key if value == v and !keys.include?(key) }
      result[keys] = v
    end
    result.rehash
  end
end

class Array
  def separator(prefix, last_prefix)
    result = ''
    self.each_with_index do |k, i|
      last = ((i + 1) == (self.length - 1))
      sep = last ? last_prefix : prefix
      result += k
      result += sep unless i + 1 == self.length
    end
    result
  end
end

def build_opening_hours(dates)
  result = []
  dates.group.each do |keys, value|
    result &amp;lt;&amp;lt; keys.map {|k| &amp;quot;opening_hours_#{k.to_s}&amp;quot; }.separator(' - ', ', ') + &amp;quot;: #{value}&amp;quot;
  end
  result
end


p build_opening_hours(dates)

# =&amp;gt; [&amp;quot;opening_hours_fri: 8:00 - 21:00&amp;quot;, 
# &amp;quot;opening_hours_sat, opening_hours_sun: closed&amp;quot;, 
# &amp;quot;opening_hours_wed: 10:00 - 21:00&amp;quot;, 
# &amp;quot;opening_hours_tue - opening_hours_mon, opening_hours_thu: 9:00 - 20:00&amp;quot;]&lt;/pre&gt;</content>
    <author>
      <name>Simo Niemel&#195;&#164;</name>
      <email>simo.niemela@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/526-open-hours-grouping/refactors/55193" rel="alternate"/>
  </entry>
</feed>

