<?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:users379</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/379" rel="self"/>
  <title>we4tech</title>
  <updated>Sat Apr 19 07:29:53 -0700 2008</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor5297</id>
    <published>2008-04-19T07:29:53-07:00</published>
    <title>[Ruby] On Rubyize this : 6th edition</title>
    <content type="html">&lt;p&gt;most of the suggestions are great, here is my one. 
&lt;br /&gt;while i complied this suggestion i had the following points in mind - 
&lt;br /&gt; 1. extensibility 
&lt;br /&gt; 2. simplicity in terms of exposing api
&lt;br /&gt; 
&lt;/p&gt;

&lt;pre&gt;# voting system related code
class Topic
  attr_accessor :name, :answers
  
  def initialize(p_topic_name)
    @name = p_topic_name
    @answers = {}
  end
  
  def add_answer(p_answer)
    @answers[p_answer.downcase] = 0
    return self
  end
  
  def select_answer(p_answer)
    answer = p_answer.downcase
    verify_valid_answer(answer)
    @answers[answer] = @answers[answer] + 1
  end
  
  def count_response(p_answer)
    answer = p_answer.downcase
    verify_valid_answer(answer)
    return @answers[answer]
  end
  
  private
  def verify_valid_answer(p_answer)
    raise &amp;quot;You have selected an unknown answer - #{p_answer}&amp;quot; if !@answers.has_key?(p_answer)
  end
end

class VotingSystem
  
  @@topics = {}
  
  def self.add_topic(p_topic)
    @@topics[p_topic.name.downcase] = p_topic
    return self
  end
  
  def self.cast(p_topic_name, p_answer)
    topic_name = p_topic_name.downcase
    verify_valid_topic(topic_name)
    @@topics[topic_name].select_answer(p_answer)
  end
  
  # display all results if p_topic_name is not specified
  def self.result(p_topic_name = nil)
    if p_topic_name.nil?
      @@topics.each do |e_topic_name, e_topic|
        show_result_from(e_topic)
      end
    else
      topic_name = p_topic_name.downcase
      verify_valid_topic(topic_name)
      show_result_from(@@topics[topic_name])
    end
  end
  
  private
  def self.verify_valid_topic(p_topic_name)
    raise &amp;quot;You have selected an unknown topic - #{p_topic_name}&amp;quot; if !@@topics.has_key?(p_topic_name)
  end
  
  def self.show_result_from(p_topic)
    puts &amp;quot;Topic - '#{p_topic.name}'&amp;quot;
    p_topic.answers.each do |e_answer, e_count|
      puts &amp;quot;#{e_count} people agreed on '#{e_answer}'&amp;quot;
    end
  end
end

# usages 

# setup voting system with topics
VotingSystem.add_topic(Topic.new(&amp;quot;is 'the flinstones' a boring t.v. show?&amp;quot;).add_answer(&amp;quot;yes, it sucks!&amp;quot;).add_answer(&amp;quot;no, it rocks!&amp;quot;))
# yet another topic to vote
VotingSystem.add_topic(Topic.new(&amp;quot;should i ask again?&amp;quot;).add_answer(&amp;quot;yes, go on!&amp;quot;).add_answer(&amp;quot;no, its ok!&amp;quot;))

# add few casts
VotingSystem.cast(&amp;quot;is 'the flinstones' a boring t.v. show?&amp;quot;, &amp;quot;yes, it sucks!&amp;quot;)
VotingSystem.cast(&amp;quot;should i ask again?&amp;quot;, &amp;quot;no, its ok!&amp;quot;)

# view all results
VotingSystem.result

# view specific result
puts &amp;quot;--------------&amp;quot;
VotingSystem.result(&amp;quot;is 'the flinstones' a boring t.v. show?&amp;quot;)
&lt;/pre&gt;</content>
    <author>
      <name>we4tech</name>
      <email>hasan83bd@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/283-rubyize-this-6th-edition/refactors/5297" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code170</id>
    <published>2007-12-05T10:37:34-08:00</published>
    <updated>2007-12-05T10:37:34-08:00</updated>
    <title>[Ruby] optimize query</title>
    <content type="html">&lt;p&gt;i have to generate a list of locations with the following format - 
&lt;br /&gt;asia (100002)
&lt;br /&gt;bangladesh (50000)
&lt;br /&gt;dhaka (3000)&lt;/p&gt;

&lt;p&gt;which is consist of the following format - 
&lt;br /&gt;&amp;lt;location name&amp;gt; (&amp;lt;total number of items from the same location&amp;gt;)&lt;/p&gt;

&lt;p&gt;also all of these content are limited by specific category.
&lt;br /&gt;hope someone will come up with some refactoring suggestion :)
&lt;br /&gt;by the way, although i had indexing for location_id, item_id but the explain says the following report -
&lt;br /&gt;+----+-------------+-------+--------+---------------+---------+---------+---------------------------------+------+----------------------------------------------+
&lt;br /&gt;| id | select_type | table | type   | possible_keys | key     | key_len | ref                             | rows | Extra                                        |
&lt;br /&gt;+----+-------------+-------+--------+---------------+---------+---------+---------------------------------+------+----------------------------------------------+
&lt;br /&gt;|  1 | SIMPLE      | cm    | ALL    | NULL          | NULL    | NULL    | NULL                            |   19 | Using where; Using temporary; Using filesort | 
&lt;br /&gt;|  1 | SIMPLE      | ilm   | ALL    | NULL          | NULL    | NULL    | NULL                            |   38 | Using where                                  | 
&lt;br /&gt;|  1 | SIMPLE      | l     | eq_ref | PRIMARY       | PRIMARY | 8       | ads_development.ilm.location_id |    1 |                                              | 
&lt;br /&gt;+----+-------------+-------+--------+---------------+---------+---------+---------------------------------+------+----------------------------------------------+
&lt;/p&gt;

&lt;pre&gt;DDL
-------
CREATE TABLE `locations` (
  `id` bigint(20) NOT NULL auto_increment,
  `name` varchar(255) character set latin1 NOT NULL,
  `parent_id` bigint(20) default '0',
  `zip_code` varchar(255) character set latin1 default NULL,
  `lat` float default '0',
  `lng` float default '0',
  `order` int(11) default '0',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=35 DEFAULT CHARSET=utf8;

CREATE TABLE `item_location_mappings` (
  `id` bigint(20) NOT NULL auto_increment,
  `location_id` bigint(20) NOT NULL,
  `item_id` bigint(20) NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `IDX_LocationID` (`location_id`),
  KEY `IDX_ItemID` (`item_id`)
) ENGINE=MyISAM AUTO_INCREMENT=107 DEFAULT CHARSET=latin1;

CREATE TABLE `category_mappings` (
  `id` int(11) NOT NULL auto_increment,
  `item_id` int(11) NOT NULL,
  `category_id` int(11) NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `IDX_CategoryID` (`category_id`),
  KEY `IDX_ItemID` (`item_id`)
) ENGINE=MyISAM AUTO_INCREMENT=198 DEFAULT CHARSET=latin1;


my written query 
----------------------

SELECT 
        l.*, 
        count(*) as weight
FROM locations l
	JOIN item_location_mappings ilm 
		ON ilm.location_id = l.id
	JOIN category_mappings cm
		ON cm.item_id = ilm.item_id
WHERE
	cm.category_id = 45
GROUP BY l.id&lt;/pre&gt;</content>
    <author>
      <name>we4tech</name>
      <email>hasan83bd@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/170-optimize-query" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor926</id>
    <published>2007-11-24T06:28:58-08:00</published>
    <title>[Ruby] On fragment cache </title>
    <content type="html">&lt;p&gt;hi @jeremy,
&lt;br /&gt;thank you for your link, it is really helpful :)&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>we4tech</name>
      <email>hasan83bd@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/160-fragment-cache/refactors/926" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor923</id>
    <published>2007-11-23T16:51:27-08:00</published>
    <title>[Ruby] On managable test method</title>
    <content type="html">&lt;p&gt;thank you @macournoyer, i also think shoulda might be a good choice.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>we4tech</name>
      <email>hasan83bd@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/159-managable-test-method/refactors/923" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor919</id>
    <published>2007-11-23T05:10:23-08:00</published>
    <title>[Ruby] On managable test method</title>
    <content type="html">&lt;p&gt;hi @macournoyer,
&lt;br /&gt;thanks again for your suggestion. 
&lt;br /&gt;actually i got to know about RSpec during this rails europe conference, here in our team we always follow TDD, as we were mostly from java background we have some fascination with junit type stuff. &lt;/p&gt;

&lt;p&gt;we would love to move on RSpec, which is already scheduled to be executed from our next project milestone. 
&lt;br /&gt;best wishes :)&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>we4tech</name>
      <email>hasan83bd@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/159-managable-test-method/refactors/919" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor918</id>
    <published>2007-11-23T05:04:33-08:00</published>
    <title>[Ruby] On fragment cache </title>
    <content type="html">&lt;p&gt;hi @jeremy 
&lt;br /&gt;i was trying to figure out what you did on your refactoring suggestion.
&lt;br /&gt;perhaps something is wrong, i couldn't find &amp;quot;seconds&amp;quot; method from &amp;quot;Time&amp;quot; class.
&lt;br /&gt;best wishes,&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>we4tech</name>
      <email>hasan83bd@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/160-fragment-cache/refactors/918" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor917</id>
    <published>2007-11-23T05:01:16-08:00</published>
    <title>[Ruby] On fragment cache </title>
    <content type="html">&lt;p&gt;hi @macournoyer,
&lt;br /&gt;thanks for your comment,
&lt;br /&gt;actually i didn't know about ActionController::Cache related option. 
&lt;br /&gt;i was looking for some solution which has higher level abstraction and flexibility.&lt;/p&gt;

&lt;p&gt;looking at the documentation, i think it is lacking some features which is more relevant with my project -
&lt;br /&gt;1. not event driven (which i will provide on my implementation)
&lt;br /&gt;2. couldn't find anyway to set cache expire time. &lt;/p&gt;

&lt;p&gt;so far i found the follow good features -
&lt;br /&gt;1. supports multiple implementation
&lt;br /&gt;2. supports way to invalidate the fragment.&lt;/p&gt;

&lt;p&gt;and again thanks for pointing action controller cache.&lt;/p&gt;

&lt;p&gt;best wishes, &lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>we4tech</name>
      <email>hasan83bd@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/160-fragment-cache/refactors/917" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code160</id>
    <published>2007-11-22T12:48:24-08:00</published>
    <updated>2007-11-24T06:29:02-08:00</updated>
    <title>[Ruby] fragment cache </title>
    <content type="html">&lt;p&gt;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. i wrote another method which was used instead of &amp;quot;render&amp;quot; method.&lt;/p&gt;

&lt;p&gt;as i had mongrel backed deployment, so i could get shared memory advantage. as you know mongrel under production mode it keeps only single instance of all models. &lt;/p&gt;

&lt;pre&gt;# i use this method instead of &amp;quot;render&amp;quot;
render_from_cache_or_render(:cache_key =&amp;gt;&#226;&#8364;&#157;cache key&#226;&#8364;&#157;, :cache_expire_after =&amp;gt; ConstantHelper::TAG_CLOUD_EXPIRED_IN, # minute

# implementation
def render_from_cache_or_render(p_args)

    return render(p_args) if true == p_args[:cache_off]

    # check from cache
    cache_key = p_args[:cache_key]
    cached_content = CacheService.get_cache(cache_key)

    if not cached_content.nil? and not cached_content.empty?
      return cached_content
    else
    content = render(p_args)
    
    # cache expire time if defined
    cache_expire_time_in_minutes = p_args[:cache_expire_after] || 60
      CacheService.add_cache(cache_key, cache_expire_time_in_minutes, content)
      return content
    end
end


# complete source code of cache service
module Cache
    class Item
      attr_accessor :key, :expire_time, :content, :created_on

      def initialize(p_key, p_expire_time, p_content)
        @key = p_key
        @expire_time = p_expire_time * 60 # in minutes
        @content = p_content
        @created_on = Time.now
      end
    end
end

class CacheService
    @@CACHES = {}
    @@CACHE_EXPIRE_TIMES = {}

    def self.add_cache(p_key, p_expire_time, p_content)
      cache_item = Cache::Item.new(p_key, p_expire_time, p_content)
      @@CACHES[p_key.to_sym] = cache_item
    end

    def self.get_cache(p_key)
      # load content from cache
      cached_content = @@CACHES[p_key.to_sym]
      return nil if cached_content.nil?

      # verify cache validity
      return cached_content.content if not expired?(cached_content)
      return nil
    end

    private
    def self.expired?(p_cache)
      # find time difference
      time_difference = Time.now - p_cache.created_on
      return true if time_difference &amp;gt; p_cache.expire_time
    end
 end&lt;/pre&gt;</content>
    <author>
      <name>we4tech</name>
      <email>hasan83bd@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/160-fragment-cache" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code159</id>
    <published>2007-11-21T09:13:22-08:00</published>
    <updated>2007-11-23T16:51:29-08:00</updated>
    <title>[Ruby] managable test method</title>
    <content type="html">&lt;p&gt;in my test case method, i was putting all type of concerns together, which was deadly producing unmanageable test method.
&lt;br /&gt;so later i split my test method in two phases, first one is preparation phase and second one is verification phase.&lt;/p&gt;

&lt;p&gt;i was wondering whether there is more art of making this thing more understandable.&lt;/p&gt;

&lt;pre&gt;def test_destroy_category_with_dependent
    category = Category.find(3)

    # prepare for verification
    prepare_for_verifying_related_property_mappings(category)
    prepare_for_verifying_category_mappings(category)
    prepare_for_verifying_child_category(category)

    # perform action
    category.destroy

    # ensure the action
    assert_raise(ActiveRecord::RecordNotFound) {
      Category.find(category.id)
    }

    # perform verification
    verify_related_property_mappings(category)
    verify_related_category_mappings(category)
    verify_related_child_category(category)
end&lt;/pre&gt;</content>
    <author>
      <name>we4tech</name>
      <email>hasan83bd@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/159-managable-test-method" rel="alternate"/>
  </entry>
</feed>

