<?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:users1266</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/1266" rel="self"/>
  <title>bartz</title>
  <updated>Tue Jan 13 16:49:32 -0800 2009</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor143359</id>
    <published>2009-01-13T16:49:32-08:00</published>
    <title>[PHP] On Universal File Download Class</title>
    <content type="html">&lt;p&gt;I'd refactor all the &amp;quot;action&amp;quot; methods out in separate classes.
&lt;br /&gt;All those methods share similarity in variables they use and logging.&lt;/p&gt;

&lt;pre&gt;// untested, and my PHP is verrry rusty...

class CCAction
{
	const FAIL = &amp;quot;FAIL&amp;quot;;
	const SUCCESS = &amp;quot;SUCCESS&amp;quot;;
	
	protected $url;
	protected $method;
	
	public function fetch()
	{
		if(!isset($this-&amp;gt;url))
		{
			$this-&amp;gt;log(CCAction::FAIL, &amp;quot;No url set&amp;quot;)
		}
		
		if($this-&amp;gt;check_prerequisites())
		{
			if($this-&amp;gt;do_fetch())
			{
				$this-&amp;gt;log(CCAction::SUCCESS, &amp;quot;File has been downloaded using &amp;quot;.$this-&amp;gt;method);	
			}
			else
			{
				$this-&amp;gt;log(CCAction::FAIL, &amp;quot;Could not downloaded file using &amp;quot;.$this-&amp;gt;method);
			}
		}
	}
	
	protected function log($status, $message)
	{
		$this-&amp;gt;log_status(date(&amp;quot;H:i:s d-m-Y&amp;quot;).&amp;quot; $status : $message&amp;quot;);
	}
}

class CCGetUsingCurl extends CCAction
{
	protected $method = &amp;quot;wget&amp;quot;;
	
	public function __init($url)
	{
		$this-&amp;gt;url = $url;
	}
	
	protected function check_prerequisites()
	{
		// check for curl, etc
	}
	
	protected function do_fetch()
	{
		// do the actual fetching
	}
}&lt;/pre&gt;</content>
    <author>
      <name>bartz</name>
      <email>loop@superinfinite.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/440-universal-file-download-class/refactors/143359" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code703</id>
    <published>2009-01-13T16:30:53-08:00</published>
    <updated>2009-01-14T21:12:46-08:00</updated>
    <title>[Ruby] to_xml, with explicit and conditional removal of a :include</title>
    <content type="html">&lt;p&gt;If offline_at is before now, explicitly remove :data from :includes passed to to_xml
&lt;br /&gt;So, if offline_at &amp;gt; Time.now, and args = { :include =&amp;gt; :data}, old_to_xml will be called with { :include =&amp;gt; [] }&lt;/p&gt;

&lt;pre&gt;alias_method :old_to_xml, :to_xml
def to_xml(args={})
  if args[:include]
    if offline_at &amp;amp;&amp;amp; offline_at &amp;lt;= Time.now
      args[:include] -= [:data]
    end
  end
  old_to_xml(args)
end

&lt;/pre&gt;</content>
    <author>
      <name>bartz</name>
      <email>loop@superinfinite.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/703-to_xml-with-explicit-and-conditional-removal-of-a-include" rel="alternate"/>
  </entry>
</feed>

