<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MyFunnyDev &#187; ruby on rails</title>
	<atom:link href="http://michalkuklis.com/blog/category/ruby-on-rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://michalkuklis.com/blog</link>
	<description>web, coding and beyond</description>
	<lastBuildDate>Fri, 25 Jun 2010 05:51:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>ruby maxins in rails plugins</title>
		<link>http://michalkuklis.com/blog/2009/07/20/ruby-maxins-in-rails-plugins/</link>
		<comments>http://michalkuklis.com/blog/2009/07/20/ruby-maxins-in-rails-plugins/#comments</comments>
		<pubDate>Tue, 21 Jul 2009 01:16:46 +0000</pubDate>
		<dc:creator>Michał Kuklis</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://michalkuklis.com/blog/?p=175</guid>
		<description><![CDATA[Very often when looking at the code in rails plugins you can run into this:

module Taggable 
  def self.included&#40;base&#41;
    base.extend&#40;ClassMethods&#41;
  end
  module module ClassMethods
    #methods here
  end
end

This is a part of a bigger pattern which is shown below:

module ModuleA
  def self.included&#40;base&#41;
    [...]]]></description>
			<content:encoded><![CDATA[<p>Very often when looking at the code in rails plugins you can run into this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> Taggable 
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">included</span><span style="color:#006600; font-weight:bold;">&#40;</span>base<span style="color:#006600; font-weight:bold;">&#41;</span>
    base.<span style="color:#9900CC;">extend</span><span style="color:#006600; font-weight:bold;">&#40;</span>ClassMethods<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">module</span> <span style="color:#9966CC; font-weight:bold;">module</span> ClassMethods
    <span style="color:#008000; font-style:italic;">#methods here</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This is a part of a bigger pattern which is shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> ModuleA
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">included</span><span style="color:#006600; font-weight:bold;">&#40;</span>base<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#008000; font-style:italic;"># add class methods from ModuleB</span>
    base.<span style="color:#9900CC;">extend</span><span style="color:#006600; font-weight:bold;">&#40;</span>ModuleB<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">module</span> ModuleB
  <span style="color:#9966CC; font-weight:bold;">def</span> act_as_hello
    <span style="color:#CC0066; font-weight:bold;">p</span> <span style="color:#996600;">&quot;hello from module B&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> ClassC
 <span style="color:#008000; font-style:italic;">#class body here</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># include moduleA in classC</span>
ClassC.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:include</span>, ModuleA<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> ClassD <span style="color:#006600; font-weight:bold;">&lt;</span> ClassC
  act_as_hello
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
classD = ClassD.<span style="color:#9900CC;">new</span></pre></div></div>

<p>The pattern is used often when developing plugins with ActiveRecord. What we gain by inheriting from ClassC  (<b>class ClassD < ClassC</b>) are instance methods from ModuleA. This is done by:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">ClassC.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:include</span>, ModuleA<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Moreover since ModuleA is included in ClassC, ModuleA&#8217;s initializer <strong>def self.included(base)</strong> will be invoked at the time  ModuleA is mixed with ClassC. The invocation will call <b>base.extend(ModuleB)</b>. In this case <b>base</b> represents ClassC which will be extended by adding class methods from ModuleB. The ModuleA&#8217;s init method is shown again below:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">included</span><span style="color:#006600; font-weight:bold;">&#40;</span>base<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#008000; font-style:italic;"># add class methods from ModuleB to ClassC</span>
  base.<span style="color:#9900CC;">extend</span><span style="color:#006600; font-weight:bold;">&#40;</span>ModuleB<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>At the end our ClassD has now access to all class methods defined in ModuleB.  <strong>act_as_hello</strong> will be called during ClassD initialization:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> ClassD <span style="color:#006600; font-weight:bold;">&lt;</span> ClassC
  act_as_hello
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://michalkuklis.com/blog/2009/07/20/ruby-maxins-in-rails-plugins/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>heroku</title>
		<link>http://michalkuklis.com/blog/2008/02/19/heroku/</link>
		<comments>http://michalkuklis.com/blog/2008/02/19/heroku/#comments</comments>
		<pubDate>Wed, 20 Feb 2008 04:25:18 +0000</pubDate>
		<dc:creator>Michał Kuklis</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://michalkuklis.com/blog/?p=31</guid>
		<description><![CDATA[http://heroku.com/ &#8211; awesome work&#8230;
]]></description>
			<content:encoded><![CDATA[<p><a href="http://heroku.com/">http://heroku.com/</a> &#8211; awesome work&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://michalkuklis.com/blog/2008/02/19/heroku/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
