<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>hasan&#039;s blog (বল্গ) &#187; Ruby</title>
	<atom:link href="http://we4tech.wordpress.com/category/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://we4tech.wordpress.com</link>
	<description>work for fun!!!</description>
	<lastBuildDate>Wed, 25 Jan 2012 11:56:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='we4tech.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/77f5f3e5f84d83c83da12540728f5cb3?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>hasan&#039;s blog (বল্গ) &#187; Ruby</title>
		<link>http://we4tech.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://we4tech.wordpress.com/osd.xml" title="hasan&#039;s blog (বল্গ)" />
	<atom:link rel='hub' href='http://we4tech.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Ruby Twitter OAuth API : 401 Unauthorized error also says Failed to validate oauth signature and token</title>
		<link>http://we4tech.wordpress.com/2010/09/20/ruby-twitter-oauth-api-401-unauthorized-error-also-says-failed-to-validate-oauth-signature-and-token/</link>
		<comments>http://we4tech.wordpress.com/2010/09/20/ruby-twitter-oauth-api-401-unauthorized-error-also-says-failed-to-validate-oauth-signature-and-token/#comments</comments>
		<pubDate>Mon, 20 Sep 2010 08:10:32 +0000</pubDate>
		<dc:creator>nhm tanveer hossain khan</dc:creator>
				<category><![CDATA[OAuth]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://we4tech.wordpress.com/?p=575</guid>
		<description><![CDATA[Ah! what a waste, after trying out for couple of hours i&#8217;ve suddenly found out a blessing post from &#8220;blaine garrett&#8221; . hussss! he really got it fixed through synchronizing system time. my system time was around 30 mins beyond twitter server time. so far i can assume, this problem was occurring due to twitter &#8230; <a href="http://we4tech.wordpress.com/2010/09/20/ruby-twitter-oauth-api-401-unauthorized-error-also-says-failed-to-validate-oauth-signature-and-token/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=we4tech.wordpress.com&amp;blog=537526&amp;post=575&amp;subd=we4tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Ah! what a waste, after trying out for couple of hours i&#8217;ve suddenly found out a blessing post from <a href="http://blainegarrett.com/2009/07/14/failed-to-validate-oauth-signature-and-token-on-twitter-oauth-check-your-cloc/">&#8220;blaine garrett&#8221;</a> . hussss! he really got it fixed through synchronizing system time. my system time was around 30 mins beyond twitter server time.</p>
<p>so far i can assume, this problem was occurring due to twitter oauth service call validation, since the attached timestamp (through OAuth timestamp or some other header which was carrying request time stamp) was giving far beyond time (ie 30 mins beyond in my case). thus twitter was rejecting the request.</p>
<p>anyway, after synchronizing my system date and time, i can see it&#8217;s working.</p>
<p>thanks &#8220;baline garrett&#8221; <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/we4tech.wordpress.com/575/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/we4tech.wordpress.com/575/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/we4tech.wordpress.com/575/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/we4tech.wordpress.com/575/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/we4tech.wordpress.com/575/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/we4tech.wordpress.com/575/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/we4tech.wordpress.com/575/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/we4tech.wordpress.com/575/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/we4tech.wordpress.com/575/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/we4tech.wordpress.com/575/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/we4tech.wordpress.com/575/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/we4tech.wordpress.com/575/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/we4tech.wordpress.com/575/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/we4tech.wordpress.com/575/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=we4tech.wordpress.com&amp;blog=537526&amp;post=575&amp;subd=we4tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://we4tech.wordpress.com/2010/09/20/ruby-twitter-oauth-api-401-unauthorized-error-also-says-failed-to-validate-oauth-signature-and-token/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<georss:point>23.709921 90.407143</georss:point>
		<geo:lat>23.709921</geo:lat>
		<geo:long>90.407143</geo:long>
		<media:content url="http://0.gravatar.com/avatar/4bb774de244da2d6e7f39a189b905077?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hasan</media:title>
		</media:content>
	</item>
		<item>
		<title>Ruby process &amp; ActiveRecord data set executing in multi cores</title>
		<link>http://we4tech.wordpress.com/2010/01/29/ruby-process-and-activerecord-data-set-executing-in-multicores/</link>
		<comments>http://we4tech.wordpress.com/2010/01/29/ruby-process-and-activerecord-data-set-executing-in-multicores/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 19:34:43 +0000</pubDate>
		<dc:creator>nhm tanveer hossain khan</dc:creator>
				<category><![CDATA[framework]]></category>
		<category><![CDATA[multicore process]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[tekSymmetry.net]]></category>

		<guid isPermaLink="false">http://we4tech.wordpress.com/?p=557</guid>
		<description><![CDATA[Now these days! multi core process is more available than getting a single core processor. perhaps single core is soon getting in Zoo! (oops! perhaps in museum) here i demonstrated how we have executed a big chunk of data over multicores processor. <a href="http://we4tech.wordpress.com/2010/01/29/ruby-process-and-activerecord-data-set-executing-in-multicores/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=we4tech.wordpress.com&amp;blog=537526&amp;post=557&amp;subd=we4tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>You know what! in one of <a href="http://www.teksymmetry.com">our</a> (tekSymmetry LLC) projects, we have so many background calculations,<br />
which usually takes so many hours to get fully completed. ever since we have introduced those processes,<br />
we were having problem with it&#8217;s execution time. sometimes it get&#8217;s in nerve</p>
<p>as you know a single ruby process can use a single processor&#8217;s core at a time.<br />
this is probable one of the reasons why muli processes based deployment<br />
strategy is picked by ruby on rails community.</p>
<p>anyway, these days our servers got more than one core! more precisely,<br />
in our case each of our production server got 8 cores based intel xeon processor.</p>
<p>so you see the question rose if we could run those long running expensive process in multicores<br />
our system could have better chance to get faster!.</p>
<p>well this blog post is intended for showing you the technique how we have done it in ruby on rails.</p>
<h3>for better understanding, let me give you some hints so you can get the context -</h3>
<ul>
<li>we have big database table rows!</li>
<li>processing a single row doesn&#8217;t require anything from the same database table.</li>
<li>we are using linux (in our case debian lenny)</li>
</ul>
<h3>so here is the way we have done it -</h3>
<ol>
<li>we took the max rows count for the main query</li>
<li>and divided by the number of cores we have</li>
<li>then we forked child process with each subset of the rows</li>
<li>and executed the logic and related stuffs!</li>
<li>on the parent process we initiated a loop where it was checking the newly forked process status</li>
<li>if all the pid files (which are generated by the newly forked children) are removed,<br />
parent process will flag it as successful execution thus it will end the loop.</li>
</ol>
<p>so you see, it is damn! simple <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> _) and it is working for us <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> _),<br />
it has improved our execution time 8x faster, because of getting 8 cores in new server.</p>
<p>here is the code in ruby how we did it. (we created a helper &#8220;<strong>multicore_execution_helper.rb</strong>&#8220;  and <strong>included</strong> in model, thus <strong>execute_in_multicores</strong> became usable)</p>
<pre><a name="l1">1    </a>module MulticoreExecutionHelper
<a name="l2">2    </a>
<a name="l3">3    </a>  def execute_in_multicores(
<a name="l4">4    </a>      p_cores, p_total_rows, p_model, p_conditions = {}, &amp;block)
<a name="l5">5    </a>
<a name="l6">6    </a>    p_cores == 2 if p_cores.to_i == 0
<a name="l7">7    </a>    total_items_per_core = p_total_rows / p_cores
<a name="l8">8    </a>    logger.info "[BATCH-PROCESS-LOG] Total processes - #{p_cores}, " +
<a name="l9">9    </a>                "total rows - #{p_total_rows} [#{total_items_per_core} / 1 core]"
<a name="l10">10   </a>
<a name="l11">11   </a>    # Create job id for each process
<a name="l12">12   </a>    job_ids = p_cores.times.collect{|i| rand.to_s }
<a name="l13">13   </a>
<a name="l14">14   </a>    # Fork process for each core and execute the block
<a name="l15">15   </a>    p_cores.times do |offset|
<a name="l16">16   </a>      Process.fork do
<a name="l17">17   </a>        logger.info "[BATCH-PROCESS-LOG] Starting thread - #{offset} " +
<a name="l18">18   </a>                    "assigned # #{job_ids[offset]}"
<a name="l19">19   </a>
<a name="l20">20   </a>        # Keep job track through the created process pid file.
<a name="l21">21   </a>        pid_file = File.join(RAILS_ROOT, 'tmp/pids/', "#{job_ids[offset]}.pid")
<a name="l22">22   </a>        File.open(pid_file, 'w') {|f| f.puts Process.pid.to_s}
<a name="l23">23   </a>
<a name="l24">24   </a>        # Since fork process is created from the sample of the parent
<a name="l25">25   </a>        # process's memory so we need to reconnect all live connections.
<a name="l26">26   </a>        begin
<a name="l27">27   </a>          ActiveRecord::Base.connection.reconnect!
<a name="l28">28   </a>
<a name="l29">29   </a>          # Retrieve data from the specific row through the defined
<a name="l30">30   </a>          # offset and limit
<a name="l31">31   </a>          teams = p_model.find(
<a name="l32">32   </a>              :all, {
<a name="l33">33   </a>                  <img src='http://s1.wp.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> ffset =&gt; (offset * total_items_per_core),
<a name="l34">34   </a>                  :limit =&gt; total_items_per_core}.merge(p_conditions))
<a name="l35">35   </a>
<a name="l36">36   </a>          block.call(teams)
<a name="l37">37   </a>        rescue =&gt; $e
<a name="l38">38   </a>          logger.error "[BATCH-PROCESS-LOG] Exception raised during " +
<a name="l39">39   </a>                       "execution - #{$e.inspect}"
<a name="l40">40   </a>        end
<a name="l41">41   </a>
<a name="l42">42   </a>        # Remove pid since we are done here!
<a name="l43">43   </a>        FileUtils.rm(pid_file)
<a name="l44">44   </a>      end
<a name="l45">45   </a>    end
<a name="l46">46   </a>
<a name="l47">47   </a>    # monitor whether the process is completed or still in progress
<a name="l48">48   </a>    # don't return this method unless all the forked processes have
<a name="l49">49   </a>    # completed their job
<a name="l50">50   </a>    sleep(2)
<a name="l51">51   </a>
<a name="l52">52   </a>    while 1 do
<a name="l53">53   </a>      fully_completed = true
<a name="l54">54   </a>      for job_id in job_ids
<a name="l55">55   </a>        pid_file = File.join(RAILS_ROOT, 'tmp/pids/', "#{job_id}.pid")
<a name="l56">56   </a>        if fully_completed &amp;&amp; File.exists?(pid_file)
<a name="l57">57   </a>          fully_completed = false
<a name="l58">58   </a>          break
<a name="l59">59   </a>        end
<a name="l60">60   </a>      end
<a name="l61">61   </a>
<a name="l62">62   </a>      break if fully_completed
<a name="l63">63   </a>      sleep(2)
<a name="l64">64   </a>      logger.debug '[BATCH-PROCESS-LOG] again...'
<a name="l65">65   </a>    end
<a name="l66">66   </a>  end
<a name="l67">67   </a>
<a name="l68">68   </a>end
<a name="l69">69   </a></pre>
<h3>here is the usages code -</h3>
<pre><a name="l143">143  </a>      execute_in_multicores(p_total_cores, SomeStuff.count, SomeStuff) do |some_stuffs|
<a name="l144">144  </a>        # Do.. whatever you wanna do with the stuff here! these are gonna run on multicores!
<a name="l151">151  </a>      end

see it is really simple! <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> _) if you like it let me know! how much you like it <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> _)
<a href="http://github.com/we4tech/rails_multicore_execution_helper/blob/master/multicore_execution_helper.rb">here you can find the code on github</a> 

best wishes!</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/we4tech.wordpress.com/557/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/we4tech.wordpress.com/557/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/we4tech.wordpress.com/557/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/we4tech.wordpress.com/557/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/we4tech.wordpress.com/557/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/we4tech.wordpress.com/557/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/we4tech.wordpress.com/557/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/we4tech.wordpress.com/557/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/we4tech.wordpress.com/557/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/we4tech.wordpress.com/557/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/we4tech.wordpress.com/557/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/we4tech.wordpress.com/557/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/we4tech.wordpress.com/557/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/we4tech.wordpress.com/557/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=we4tech.wordpress.com&amp;blog=537526&amp;post=557&amp;subd=we4tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://we4tech.wordpress.com/2010/01/29/ruby-process-and-activerecord-data-set-executing-in-multicores/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<georss:point>23.709921 90.407143</georss:point>
		<geo:lat>23.709921</geo:lat>
		<geo:long>90.407143</geo:long>
		<media:content url="http://0.gravatar.com/avatar/4bb774de244da2d6e7f39a189b905077?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hasan</media:title>
		</media:content>
	</item>
		<item>
		<title>what killed my time to run my first test using “cucumber”</title>
		<link>http://we4tech.wordpress.com/2008/10/19/what-killed-my-time-to-run-my-first-test-using-cucumber/</link>
		<comments>http://we4tech.wordpress.com/2008/10/19/what-killed-my-time-to-run-my-first-test-using-cucumber/#comments</comments>
		<pubDate>Sun, 19 Oct 2008 12:12:33 +0000</pubDate>
		<dc:creator>nhm tanveer hossain khan</dc:creator>
				<category><![CDATA[BDD]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://hasan.we4tech.com/what-killed-my-time-to-run-my-first-test-using-cucumber/</guid>
		<description><![CDATA[stucked with &#8220;No such file or directory &#8211; cucumber.yml&#8221; error? then you must doing the same mistake as i was doing for last 1 hour. i had the following code in Rakefile require &#8216;cucumber/rake/task&#8217; Cucumber::Rake::Task.new do &#124;t&#124; profile = ENV['PROFILE'] &#124;&#124; &#8216;default&#8217; t.cucumber_opts = &#8220;&#8211;profile #{profile}&#8221; end the fix is just keep the following code &#8230; <a href="http://we4tech.wordpress.com/2008/10/19/what-killed-my-time-to-run-my-first-test-using-cucumber/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=we4tech.wordpress.com&amp;blog=537526&amp;post=421&amp;subd=we4tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>stucked with &#8220;No such file or directory &#8211; <a href="http://github.com/aslakhellesoy/cucumber/">cucumber</a>.yml&#8221; error?<br />
then you must doing the same mistake as i was doing for last 1 hour.</p>
<p>i had the following code in <strong>Rakefile</strong></p>
<blockquote><p>require &#8216;<a href="http://github.com/aslakhellesoy/cucumber/">cucumber</a>/rake/task&#8217;</p>
<p>Cucumber::Rake::Task.new do |t|<br />
profile = ENV['PROFILE'] || &#8216;default&#8217;<br />
t.cucumber_opts = &#8220;&#8211;profile #{profile}&#8221;<br />
end</p></blockquote>
<p>the fix is just keep the following code only -</p>
<blockquote><p>require &#8216;<a href="http://github.com/aslakhellesoy/cucumber/">cucumber</a>/rake/task&#8217;</p>
<p>Cucumber::Rake::Task.new do |t|<br />
end</p></blockquote>
<p><strong>so are you still facing problem while you are executing &#8220;rake features&#8221; but it doesn&#8217;t come up with any output?</strong><br />
here is the check list &#8211; (this list may grow gradually) -<br />
1. do you have features directory<br />
2. do you have features/steps directory<br />
3. lets say you have &#8220;features/transfer.feature&#8221; file do you know that you must have &#8220;features/steps/transfer_steps.rb&#8221; file?<br />
4. do you know &#8220;feature name&#8221; must be prefixed for steps file?<br />
hope this might help you.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/we4tech.wordpress.com/421/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/we4tech.wordpress.com/421/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/we4tech.wordpress.com/421/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/we4tech.wordpress.com/421/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/we4tech.wordpress.com/421/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/we4tech.wordpress.com/421/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/we4tech.wordpress.com/421/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/we4tech.wordpress.com/421/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/we4tech.wordpress.com/421/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/we4tech.wordpress.com/421/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/we4tech.wordpress.com/421/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/we4tech.wordpress.com/421/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/we4tech.wordpress.com/421/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/we4tech.wordpress.com/421/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=we4tech.wordpress.com&amp;blog=537526&amp;post=421&amp;subd=we4tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://we4tech.wordpress.com/2008/10/19/what-killed-my-time-to-run-my-first-test-using-cucumber/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4bb774de244da2d6e7f39a189b905077?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hasan</media:title>
		</media:content>
	</item>
		<item>
		<title>ruby dynamic factory method implementation</title>
		<link>http://we4tech.wordpress.com/2008/05/10/ruby-dynamic-factory-method-implementation/</link>
		<comments>http://we4tech.wordpress.com/2008/05/10/ruby-dynamic-factory-method-implementation/#comments</comments>
		<pubDate>Sat, 10 May 2008 06:44:42 +0000</pubDate>
		<dc:creator>nhm tanveer hossain khan</dc:creator>
				<category><![CDATA[design pattern]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://hasan.we4tech.com/ruby-dynamic-abstract-factory-implementation/</guid>
		<description><![CDATA[i was looking for some way out to implement  factory method on ruby code. where i have a VersionControl::ServiceFactory which will take different implementation as factory method. ie. VersionControl::ServiceFactory::subversion, VersionControl::ServiceFactory::git and so on. though all these &#8220;subversion, git, perforce&#8221; methods are not predefined, these will be added while new implemention is added. my skeleton (abstract) &#8230; <a href="http://we4tech.wordpress.com/2008/05/10/ruby-dynamic-factory-method-implementation/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=we4tech.wordpress.com&amp;blog=537526&amp;post=409&amp;subd=we4tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>i was looking for some way out to implement  factory method on ruby code. where i have a VersionControl::ServiceFactory which will take different implementation as factory method. ie. VersionControl::ServiceFactory::subversion, VersionControl::ServiceFactory::git and so on.</p>
<p>though all these &#8220;subversion, git, perforce&#8221; methods are not predefined, these will be added while new implemention is added. my skeleton (abstract) implementation was in VersionControl::Service, so whatever implemention (svn, git, perforce, other) comes ahead it will implement method from VersionControl::Service method. since ruby doesn&#8217;t support class abstraction so this was the only way  i got in head.</p>
<p>so my skeleton implemention was consist of the following code in summary -</p>
<pre style="background:#000000 none repeat scroll 0 0;color:#ffffff;padding:10px;">module VersionControl

# log entry class for data object
class LogEntry; end

# local repository information
class Information; end

# abstract service class.
# define service which will be exposed for a
# normal version controlling service.
class Service

# retrieve recent logs from the current directory
# required parameters - *base project path* and other options.
def logs(p_path, p_options = {}); raise "not implemented method" end

# generate diff from mentioned revision number
# or current revision number
# required parameters - *base project path* and other options.
def diff(p_path, p_options = {}); raise "not implemented method" end

# find repository information
def info(p_path, p_options = {}); raise "not implemented method" end
# check out content from version control server
def checkout(p_source, p_path, p_options = {}); raise "not implemented method" end

end

# factory class for supporting different version control implementation
class ServiceFactory; end
end</pre>
<p>and the implementation is written in this way -</p>
<pre style="background:#000000 none repeat scroll 0 0;color:#ffffff;padding:10px;">class ServiceFactory

class &lt; &lt; self
# add subversion factory method implementation
@@subversion_instance = SubversionService.new
def subversion
return @@subversion_instance
end
end
end</pre>
<p>so every implementation also push it&#8217;s factory method to &#8220;VersionControl::ServiceFactory&#8221; class. so this way i have implemented dynamic factory method on ruby.</pre>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/we4tech.wordpress.com/409/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/we4tech.wordpress.com/409/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/we4tech.wordpress.com/409/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/we4tech.wordpress.com/409/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/we4tech.wordpress.com/409/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/we4tech.wordpress.com/409/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/we4tech.wordpress.com/409/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/we4tech.wordpress.com/409/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/we4tech.wordpress.com/409/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/we4tech.wordpress.com/409/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/we4tech.wordpress.com/409/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/we4tech.wordpress.com/409/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/we4tech.wordpress.com/409/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/we4tech.wordpress.com/409/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/we4tech.wordpress.com/409/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/we4tech.wordpress.com/409/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=we4tech.wordpress.com&amp;blog=537526&amp;post=409&amp;subd=we4tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://we4tech.wordpress.com/2008/05/10/ruby-dynamic-factory-method-implementation/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4bb774de244da2d6e7f39a189b905077?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hasan</media:title>
		</media:content>
	</item>
		<item>
		<title>upcoming project mojar_workflow, workflow engine in ruby</title>
		<link>http://we4tech.wordpress.com/2008/02/10/upcoming-project-mojar_workflow-workflow-engine-in-ruby/</link>
		<comments>http://we4tech.wordpress.com/2008/02/10/upcoming-project-mojar_workflow-workflow-engine-in-ruby/#comments</comments>
		<pubDate>Sun, 10 Feb 2008 07:59:25 +0000</pubDate>
		<dc:creator>nhm tanveer hossain khan</dc:creator>
				<category><![CDATA[enterprise]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://hasan.we4tech.com/upcoming-project-mojar_workflow-workflow-engine-in-ruby/</guid>
		<description><![CDATA[hi, we just kicking start a new open source ruby based workflow engine project &#8220;mojar workflow&#8220;. we named it after our deshi word &#8220;mojar&#8221; reason is very clear to spread out this word. mojar workflow, is integral solution to execute a flow of business rules. for example - you have an action where you have &#8230; <a href="http://we4tech.wordpress.com/2008/02/10/upcoming-project-mojar_workflow-workflow-engine-in-ruby/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=we4tech.wordpress.com&amp;blog=537526&amp;post=396&amp;subd=we4tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>hi,<br />
we just kicking start a new open source ruby based workflow engine project <strong>&#8220;<a href="http://rubyforge.org/projects/mojarworkflow/">mojar workflow</a>&#8220;</strong>.<br />
we named it after our deshi word &#8220;mojar&#8221; reason is very clear to<br />
spread out this word.</p>
<p>mojar workflow, is integral solution to execute a flow of business<br />
rules. for example -</p>
<p>you have an action where you have the following set of rules -<br />
1. start transaction<br />
2. verify user account<br />
3. verify user balance<br />
4. verify user dues<br />
5. reduce dues from balance<br />
6. complete transaction</p>
<p>after few days you got a new requirement, where you suppose to reduce<br />
user dues by the 10% because of company new discount policy.<br />
so you have to implement the following rules -<br />
1. start transaction<br />
2. verify user account<br />
3. verify user balance<br />
4. verify user dues<br />
5. reduce dues by 10% of discount<br />
5. reduce discounted dues from balance<br />
6. complete transaction</p>
<p>to implement such scenario you have to again code in your stable<br />
release. but using mojar workflow, you can add that new concern from<br />
the abstract flow maintenance layer. where you can define this flow in<br />
yaml file or an xml document.</p>
<p>keep your eyes on -<br />
<a href="http://rubyforge.org/projects/mojarworkflow/">http://rubyforge.org/projects/mojarworkflow/</a></p>
<p>best wishes,</p>
</p>
<p><!--acc585eaf45842d4a72b95f260dd36c8--></p>
</p>
<p><!--c329955b83bf724dfaa8a090a5ee57e0-->
</p>
<p><!--22ad517e94d773d73a81892dc6e71324--></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/we4tech.wordpress.com/396/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/we4tech.wordpress.com/396/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/we4tech.wordpress.com/396/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/we4tech.wordpress.com/396/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/we4tech.wordpress.com/396/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/we4tech.wordpress.com/396/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/we4tech.wordpress.com/396/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/we4tech.wordpress.com/396/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/we4tech.wordpress.com/396/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/we4tech.wordpress.com/396/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/we4tech.wordpress.com/396/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/we4tech.wordpress.com/396/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/we4tech.wordpress.com/396/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/we4tech.wordpress.com/396/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/we4tech.wordpress.com/396/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/we4tech.wordpress.com/396/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=we4tech.wordpress.com&amp;blog=537526&amp;post=396&amp;subd=we4tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://we4tech.wordpress.com/2008/02/10/upcoming-project-mojar_workflow-workflow-engine-in-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4bb774de244da2d6e7f39a189b905077?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hasan</media:title>
		</media:content>
	</item>
		<item>
		<title>simple fragment cache implementation on ruby on rails</title>
		<link>http://we4tech.wordpress.com/2007/11/17/simple-fragment-cache-implementation-on-ruby-on-rails/</link>
		<comments>http://we4tech.wordpress.com/2007/11/17/simple-fragment-cache-implementation-on-ruby-on-rails/#comments</comments>
		<pubDate>Fri, 16 Nov 2007 21:00:55 +0000</pubDate>
		<dc:creator>nhm tanveer hossain khan</dc:creator>
				<category><![CDATA[cache]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://hasan.we4tech.com/simple-fragment-cache-implementation-on-ruby-on-rails</guid>
		<description><![CDATA[i was getting serious performance problem with one of my projects. so i came up with a simple fragment cache implementation on ruby on rails. after implementing this stuff, i replaced &#8220;render(:partial =&#62; &#8230;)&#8221; with the following method - render_from_cache_or_render(:cache_key =&#62;”cache key”, :cache_expire_after =&#62; ConstantHelper::TAG_CLOUD_EXPIRED_IN, # minutes :partial =&#62; “….”) let&#8217;s have a look on &#8230; <a href="http://we4tech.wordpress.com/2007/11/17/simple-fragment-cache-implementation-on-ruby-on-rails/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=we4tech.wordpress.com&amp;blog=537526&amp;post=385&amp;subd=we4tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>i was getting serious performance problem with one of my projects. so i came up with a simple fragment cache implementation on ruby on rails.</p>
<p>after implementing this stuff, i replaced &#8220;render(:partial =&gt; &#8230;)&#8221; with the following method -</p>
<blockquote>
<pre>render_from_cache_or_render(:cache_key =&gt;”cache key”, :cache_expire_after =&gt; ConstantHelper::TAG_CLOUD_EXPIRED_IN, # minutes :partial =&gt; “….”)</pre>
</blockquote>
<p>let&#8217;s have a look on my implementation -</p>
<blockquote><p>def render_from_cache_or_render(p_args)</p>
<p>return render(p_args) if true == p_args[:cache_off]</p>
<p># check from cache<br />
cache_key = p_args[:cache_key]<br />
cached_content = CacheService.get_cache(cache_key)</p>
<p>if not cached_content.nil? and not cached_content.empty?<br />
return cached_content<br />
else<br />
content = render(p_args)<br />
# cache expire time if defined<br />
cache_expire_time_in_minutes = p_args[:cache_expire_after] || 60<br />
CacheService.add_cache(cache_key, cache_expire_time_in_minutes, content)<br />
return content<br />
end<br />
end</p></blockquote>
<p>actually, my implemented &#8220;CacheService&#8221; class is simply storing all cache in a hash map.<br />
when some cache was requested for peek, cache expiry was checked before returning the cached value.</p>
<p>for CacheService implementation look at the bottom of my post.</p>
<p>anyway, after implementing and utilizing this stuff, i gained 70+ requests capability per second. fyi, before applying cache it was around 10 per second.</p>
<blockquote><p>module Cache<br />
class Item<br />
attr_accessor :key, :expire_time, :content, :created_on</p>
<p>def initialize(p_key, p_expire_time, p_content)<br />
@key = p_key<br />
@expire_time = p_expire_time * 60 # in minutes<br />
@content = p_content<br />
@created_on = Time.now<br />
end<br />
end<br />
end</p>
<p>class CacheService<br />
@@CACHES = {}<br />
@@CACHE_EXPIRE_TIMES = {}</p>
<p>def self.add_cache(p_key, p_expire_time, p_content)<br />
cache_item = Cache::Item.new(p_key, p_expire_time, p_content)<br />
@@CACHES[p_key.to_sym] = cache_item<br />
end</p>
<p>def self.get_cache(p_key)<br />
# load content from cache<br />
cached_content = @@CACHES[p_key.to_sym]<br />
return nil if cached_content.nil?</p>
<p># verify cache validity<br />
return cached_content.content if not expired?(cached_content)<br />
return nil<br />
end</p>
<p>private<br />
def self.expired?(p_cache)<br />
# find time difference<br />
time_difference = Time.now &#8211; p_cache.created_on<br />
return true if time_difference &gt; p_cache.expire_time<br />
end<br />
end</p></blockquote>
<p>best wishes,<!--3d03e28513b3f05efbe65bc64219c415--></p>
<p><!--f6265e57e7aec9039f352fd813fc9a58--></p>
<p><!--5d693aaea750a2e28b4ea43ff6938b40--></p>
<p><!--4d046ce81eef3b7632a83741ad51e2d5--></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/we4tech.wordpress.com/385/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/we4tech.wordpress.com/385/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/we4tech.wordpress.com/385/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/we4tech.wordpress.com/385/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/we4tech.wordpress.com/385/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/we4tech.wordpress.com/385/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/we4tech.wordpress.com/385/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/we4tech.wordpress.com/385/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/we4tech.wordpress.com/385/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/we4tech.wordpress.com/385/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/we4tech.wordpress.com/385/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/we4tech.wordpress.com/385/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/we4tech.wordpress.com/385/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/we4tech.wordpress.com/385/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/we4tech.wordpress.com/385/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/we4tech.wordpress.com/385/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=we4tech.wordpress.com&amp;blog=537526&amp;post=385&amp;subd=we4tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://we4tech.wordpress.com/2007/11/17/simple-fragment-cache-implementation-on-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4bb774de244da2d6e7f39a189b905077?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hasan</media:title>
		</media:content>
	</item>
		<item>
		<title>thats why i like ruby!!! thanks dynamic scripting&#8230;</title>
		<link>http://we4tech.wordpress.com/2007/11/14/thats-why-i-like-ruby-thanks-dynamic-scripting/</link>
		<comments>http://we4tech.wordpress.com/2007/11/14/thats-why-i-like-ruby-thanks-dynamic-scripting/#comments</comments>
		<pubDate>Wed, 14 Nov 2007 11:46:34 +0000</pubDate>
		<dc:creator>nhm tanveer hossain khan</dc:creator>
				<category><![CDATA[mongrel]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://hasan.we4tech.com/thats-why-i-like-ruby-thanks-dynamic-scripting</guid>
		<description><![CDATA[if you have rails deployment on windows environment with mongrel service, i think you might face the following problem - Errno::EINVAL (Invalid argument): /app/models/index_service.rb:63:in `write&#8217; /app/models/index_service.rb:63:in `puts&#8217; this problem was because of &#8220;puts&#8221; what i forgot to remove before deploying on test server. if your deployment on windows service environment and if your code has &#8230; <a href="http://we4tech.wordpress.com/2007/11/14/thats-why-i-like-ruby-thanks-dynamic-scripting/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=we4tech.wordpress.com&amp;blog=537526&amp;post=384&amp;subd=we4tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>if you have rails deployment on windows environment with mongrel service, i think you might face the following problem -</p>
<blockquote><p>Errno::EINVAL (Invalid argument):<br />
/app/models/index_service.rb:63:in `write&#8217;<br />
/app/models/index_service.rb:63:in `puts&#8217;</p>
</blockquote>
<p>this problem was because of &#8220;puts&#8221; what i forgot to remove before deploying on test server.<br />
if your deployment on windows service environment and if your code has few &#8220;puts&#8221; usages, you must face this problem with mongrel</p>
<p>on mongrel group, i found they are working with this, hopefully they will replace puts with logger and other things.<br />
anyway, the quickest solution i had in mind was just use the dynamic behavior of ruby. here is what i did -</p>
<blockquote><p>def puts(p_args)<br />
logger.debug(p_args)<br />
end</p>
</blockquote>
<p>thats all fixed my problem <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>thank ruby, thanks for dynamic scripting&#8230;
</p>
<p><!--cbfed39141f6c58eda59d0d0c368ad77--></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/we4tech.wordpress.com/384/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/we4tech.wordpress.com/384/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/we4tech.wordpress.com/384/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/we4tech.wordpress.com/384/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/we4tech.wordpress.com/384/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/we4tech.wordpress.com/384/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/we4tech.wordpress.com/384/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/we4tech.wordpress.com/384/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/we4tech.wordpress.com/384/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/we4tech.wordpress.com/384/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/we4tech.wordpress.com/384/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/we4tech.wordpress.com/384/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/we4tech.wordpress.com/384/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/we4tech.wordpress.com/384/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/we4tech.wordpress.com/384/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/we4tech.wordpress.com/384/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=we4tech.wordpress.com&amp;blog=537526&amp;post=384&amp;subd=we4tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://we4tech.wordpress.com/2007/11/14/thats-why-i-like-ruby-thanks-dynamic-scripting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4bb774de244da2d6e7f39a189b905077?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hasan</media:title>
		</media:content>
	</item>
		<item>
		<title>simple AOP implementation in ruby</title>
		<link>http://we4tech.wordpress.com/2007/11/10/simple-aop-implementation-in-ruby/</link>
		<comments>http://we4tech.wordpress.com/2007/11/10/simple-aop-implementation-in-ruby/#comments</comments>
		<pubDate>Sat, 10 Nov 2007 08:39:09 +0000</pubDate>
		<dc:creator>nhm tanveer hossain khan</dc:creator>
				<category><![CDATA[aop]]></category>
		<category><![CDATA[aspect oriented programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://hasan.we4tech.com/simple-aop-implementation-in-ruby</guid>
		<description><![CDATA[i was suppose to work on some of my other projects, but i passed my time by writing a simple aop implementation in ruby. it is neither powerful like AspectJ nor comparable with AspectR. however, i was having fun with my day off. here is how my simple example is running- def test_aop # apply &#8230; <a href="http://we4tech.wordpress.com/2007/11/10/simple-aop-implementation-in-ruby/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=we4tech.wordpress.com&amp;blog=537526&amp;post=383&amp;subd=we4tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>i was suppose to work on some of my other projects, but i passed my time by writing a simple aop implementation in ruby.<br />
it is neither powerful like AspectJ nor comparable with AspectR. however, i was having fun with my day off.</p>
<p>here is how my simple example is running-</p>
<blockquote><p>def test_aop<br />
# apply pointcuts<br />
apply_advices(:before, /^do_.+/, SimpleService, SimpleServiceAdvice, :before_do)<br />
apply_advices(:around, /^do_.+/, SimpleService, SimpleServiceAdvice, :around_do)<br />
apply_advices(:after, /^do_.+/, SimpleService, SimpleServiceAdvice, :after_do)</p>
<p>simple_service = SimpleService.new<br />
simple_service.do_1(&#8220;A&#8221;)<br />
end</p></blockquote>
<p>here is the parameter name.</p>
<blockquote><p>apply_advices(type_of_advice, pointcuts_in_regex, service_class, advice_class, advice_method)</p></blockquote>
<p>instead of making aspectJ type pointcuts syntax, i have used regex, which is fine for the time being.</p>
<p>if you run this code you will find the following output -</p>
<blockquote><p>Before execution.<br />
Around {<br />
1 performed &#8211; A<br />
}<br />
After execution.</p></blockquote>
<p>now let&#8217;s have a look on my <strong>SimpleService</strong> class.</p>
<blockquote><p>class SimpleService</p>
<p>def do_1(p_param)<br />
puts &#8220;1 performed &#8211; #{p_param}&#8221;<br />
end</p>
<p>def do_2<br />
puts &#8220;2 performed.&#8221;<br />
end</p>
<p>def no_do<br />
puts &#8220;No do&#8221;<br />
end<br />
end</p></blockquote>
<p>and here is my aspect class,</p>
<blockquote><p>class SimpleServiceAdvice</p>
<p>def around_do(p_invoke)<br />
puts &#8220;Around { &#8220;<br />
output = p_invoke.proceede()<br />
puts &#8220;}&#8221;<br />
return output<br />
end</p>
<p>def before_do(p_invoke)<br />
puts &#8220;Before execution.&#8221;<br />
end</p>
<p>def after_do(p_params, p_output)<br />
puts &#8220;After execution.&#8221;<br />
end<br />
end</p></blockquote>
<p>my implementation is very straight forward, actually during implementing this stuff, i really felt the strength of meta programming. it is so flexible and so easy that sky is the limit.</p>
<p><a href="http://hasan.we4tech.com/files/aop_test.zip">here is the attached source code. </a></p>
<p><a href="http://aspectr.sourceforge.net/">much better and complete aop implementation in ruby</a></p>
<p><!--5359d0a9bb968d2498a0830a998fa5d6--></p>
<p><!--8d9ce1ad44c878667a75943248cf2393--></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/we4tech.wordpress.com/383/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/we4tech.wordpress.com/383/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/we4tech.wordpress.com/383/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/we4tech.wordpress.com/383/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/we4tech.wordpress.com/383/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/we4tech.wordpress.com/383/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/we4tech.wordpress.com/383/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/we4tech.wordpress.com/383/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/we4tech.wordpress.com/383/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/we4tech.wordpress.com/383/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/we4tech.wordpress.com/383/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/we4tech.wordpress.com/383/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/we4tech.wordpress.com/383/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/we4tech.wordpress.com/383/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/we4tech.wordpress.com/383/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/we4tech.wordpress.com/383/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=we4tech.wordpress.com&amp;blog=537526&amp;post=383&amp;subd=we4tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://we4tech.wordpress.com/2007/11/10/simple-aop-implementation-in-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4bb774de244da2d6e7f39a189b905077?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hasan</media:title>
		</media:content>
	</item>
		<item>
		<title>Fat Refactoring: use include module to reduce number of lines</title>
		<link>http://we4tech.wordpress.com/2007/11/03/fat-refactoring-use-include-module-to-reduce-number-of-lines/</link>
		<comments>http://we4tech.wordpress.com/2007/11/03/fat-refactoring-use-include-module-to-reduce-number-of-lines/#comments</comments>
		<pubDate>Sat, 03 Nov 2007 04:48:01 +0000</pubDate>
		<dc:creator>nhm tanveer hossain khan</dc:creator>
				<category><![CDATA[best practice]]></category>
		<category><![CDATA[refactoring]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>

		<guid isPermaLink="false">http://hasan.we4tech.com/fat-refactoring-use-include-module-to-reduce-number-of-lines</guid>
		<description><![CDATA[if i didn&#8217;t mention that before, i should tell it now, here at somewhere in&#8230; rnd team we are playing a lot with ruby on rails. these days our rails team is completely focusing on a product(which is secret for the time being ) where we found a lot of interesting stuffs, for instance. few &#8230; <a href="http://we4tech.wordpress.com/2007/11/03/fat-refactoring-use-include-module-to-reduce-number-of-lines/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=we4tech.wordpress.com&amp;blog=537526&amp;post=380&amp;subd=we4tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>if i didn&#8217;t mention that before, i should tell it now, here at <a title="somewhere in..." href="http://www.somewherein.net/">somewhere in&#8230;</a> rnd team we are playing a lot with ruby on rails. these days our rails team is completely focusing on a product(which is secret for the time being <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ) where we<br />
found a lot of interesting stuffs, for instance.</p>
<p>few days back, we found our application_helper and few controllers are growing too fast and getting extra fat (lines of code). so we had few refactoring to reduce the extra fat.</p>
<p>now have a look on the code we had with in application_helper.rb taken from tag/v-0.3<br />
<img class="alignnone size-medium wp-image-438" title="fat_refactoring_before" src="http://we4tech.files.wordpress.com/2007/11/fat_refactoring_before.gif?w=102&#038;h=300" alt="fat_refactoring_before" width="102" height="300" /><br />
this code is not completely visible over the screen snap, this is 340 number of lines. which was the output of our 3 iterations.</p>
<p>though these number of lines are not that much problematic, but we had a scenario which was difficult to make it more concern aware and single concerned.</p>
<p>now have a look on our code which is taken from the current trunk,<br />
<img class="alignnone size-medium wp-image-439" title="fat_reforing_after" src="http://we4tech.files.wordpress.com/2007/11/fat_reforing_after.gif?w=215&#038;h=300" alt="fat_reforing_after" width="215" height="300" /><br />
Wow, now it is 50 lines only including the header copyright information.<br />
the trick was very simple, we followed the following conventions -</p>
<p>1. find out all related and same concerned functions<br />
2. stick team together in a module<br />
3. include the module to statically import all functions<br />
no integration error, nothing has occurred.<br />
we are happy with this <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>i think, our ruby learning process is going smooth <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><!--6af55683b5bcff206d455f3a7c9cf4fc--></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/we4tech.wordpress.com/380/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/we4tech.wordpress.com/380/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/we4tech.wordpress.com/380/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/we4tech.wordpress.com/380/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/we4tech.wordpress.com/380/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/we4tech.wordpress.com/380/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/we4tech.wordpress.com/380/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/we4tech.wordpress.com/380/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/we4tech.wordpress.com/380/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/we4tech.wordpress.com/380/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/we4tech.wordpress.com/380/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/we4tech.wordpress.com/380/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/we4tech.wordpress.com/380/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/we4tech.wordpress.com/380/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/we4tech.wordpress.com/380/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/we4tech.wordpress.com/380/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=we4tech.wordpress.com&amp;blog=537526&amp;post=380&amp;subd=we4tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://we4tech.wordpress.com/2007/11/03/fat-refactoring-use-include-module-to-reduce-number-of-lines/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4bb774de244da2d6e7f39a189b905077?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hasan</media:title>
		</media:content>

		<media:content url="http://we4tech.files.wordpress.com/2007/11/fat_refactoring_before.gif?w=102" medium="image">
			<media:title type="html">fat_refactoring_before</media:title>
		</media:content>

		<media:content url="http://we4tech.files.wordpress.com/2007/11/fat_reforing_after.gif?w=215" medium="image">
			<media:title type="html">fat_reforing_after</media:title>
		</media:content>
	</item>
		<item>
		<title>split out test case into &#8220;preparation&#8221; and &#8220;verification&#8221; state.</title>
		<link>http://we4tech.wordpress.com/2007/10/28/split-out-test-case-into-preparation-and-verification-state/</link>
		<comments>http://we4tech.wordpress.com/2007/10/28/split-out-test-case-into-preparation-and-verification-state/#comments</comments>
		<pubDate>Sun, 28 Oct 2007 03:13:35 +0000</pubDate>
		<dc:creator>nhm tanveer hossain khan</dc:creator>
				<category><![CDATA[best practice]]></category>
		<category><![CDATA[design pattern]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://hasan.we4tech.com/split-out-test-case-into-preparation-and-verification-state</guid>
		<description><![CDATA[i was wondering how i could make my test method more organized and DR(Y)ied. so i had a nice time while i was writing test to ensure my current modification works ok with existing setup. i don&#8217;t know whether any design pattern or best practice is already exist on this topic. i came up with &#8230; <a href="http://we4tech.wordpress.com/2007/10/28/split-out-test-case-into-preparation-and-verification-state/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=we4tech.wordpress.com&amp;blog=537526&amp;post=373&amp;subd=we4tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>i was wondering how i could make my test method more organized and DR(Y)ied.<br />
so i had a nice time while i was writing test to ensure my current modification works ok with existing setup.</p>
<p>i don&#8217;t know whether any design pattern or best practice is already exist on this topic.<br />
i came up with something that is really good for me. here is a bit about my task and also the explanation about what i did.</p>
<p>i have a model &#8220;category&#8221; where i have the following relations -</p>
<blockquote><p>has_many :category_mappings, :dependent =&gt; :destroy<br />
has_many :items, :through =&gt; :category_mappings</p>
<p>has_many :categories, :foreign_key =&gt; &#8220;parent_id&#8221;, :dependent =&gt; :destroy</p>
<p>has_many :attribute_category_mappings, :dependent =&gt; :destroy<br />
has_many :properties, :through =&gt; :attribute_category_mappings</p>
<p>belongs_to :category</p></blockquote>
<p>i was applying :dependent = &gt; :destroy with the mapping model and child categories, which are needs to be removed as a part of the category destroy process.</p>
<p>so i had a messy unit test method to ensure this is working fine. sorry for not keeping my old code snaps otherwise i could show the messy one. anyway here what i wrote later to make it bit cleaner than the previous messy version -</p>
<blockquote><p>def test_destroy_category_with_dependent<br />
category = Category.find(3)</p>
<p># prepare for verification<br />
prepare_for_verifying_related_property_mappings(category)<br />
prepare_for_verifying_category_mappings(category)<br />
prepare_for_verifying_child_category(category)</p>
<p># perform action<br />
category.destroy</p>
<p># ensure the action<br />
assert_raise(ActiveRecord::RecordNotFound) {<br />
Category.find(category.id)<br />
}</p>
<p># perform verification<br />
verify_related_property_mappings(category)<br />
verify_related_category_mappings(category)<br />
verify_related_child_category(category)<br />
end</p></blockquote>
<p>here i just sliced my test method in 2 different roles,</p>
<p>1. preparation stage<br />
2. verification stage</p>
<p>1. preparation stage -<br />
in this stage, i just keep log for current state or other steps which are important for later testing.</p>
<p>2. verification stage -<br />
in this stage, i just verified my new state comparing with the old state (which was kept during preparation stage).<br />
so let&#8217;s have a look on the typical code which i wrote in  my preparation and verification stage -</p>
<div style="margin-left:40px;">def prepare_for_verifying_child_category(p_category)<br />
puts &#8220;Prepare for verifying related child category &#8211; #{p_category}.&#8221;<br />
@old_category_count = Category.count<br />
@old_child_category_count = p_category.categories.count<br />
@old_child_categories = Category.find_all_by_parent_id(p_category.id)<br />
assert_not_equal(0, @old_child_categories.length, &#8220;No child category found.&#8221;)<br />
end</div>
<p style="margin-left:40px;">def verify_related_child_category(p_category)<br />
puts &#8220;verify related child categories &#8211; #{p_category}&#8221;<br />
now_category_count = Category.count<br />
assert_equal(true, ((@old_category_count &#8211; now_category_count) &gt; 1),<br />
&#8220;No category has been removed.&#8221;)<br />
end</p>
<p>this seem pretty good for me, as long as i can make my code bunch simple and easy to change.</p>
<p>best wishes,</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/we4tech.wordpress.com/373/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/we4tech.wordpress.com/373/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/we4tech.wordpress.com/373/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/we4tech.wordpress.com/373/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/we4tech.wordpress.com/373/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/we4tech.wordpress.com/373/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/we4tech.wordpress.com/373/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/we4tech.wordpress.com/373/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/we4tech.wordpress.com/373/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/we4tech.wordpress.com/373/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/we4tech.wordpress.com/373/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/we4tech.wordpress.com/373/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/we4tech.wordpress.com/373/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/we4tech.wordpress.com/373/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/we4tech.wordpress.com/373/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/we4tech.wordpress.com/373/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=we4tech.wordpress.com&amp;blog=537526&amp;post=373&amp;subd=we4tech&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://we4tech.wordpress.com/2007/10/28/split-out-test-case-into-preparation-and-verification-state/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4bb774de244da2d6e7f39a189b905077?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hasan</media:title>
		</media:content>
	</item>
	</channel>
</rss>
