class Re<%=rule_class%>List < ActiveRecord::Base
  set_table_name :re_<%=rule_name%>_list
  
  def self.find_or_create(word)
    re_word = find(:first, :conditions => ["word = ?", word])
    unless re_word
      re_word = create(:word => word)      
    end  
    re_word.id
  end  
  
end

class Re<%=rule_class%>Count < ActiveRecord::Base
  set_table_name :re_<%=rule_name%>_count
  
  belongs_to :word, :class_name  => 'Re<%=rule_class%>List', :foreign_key => 'word_id'
  
  def self.update_time_codes(word_id)
    now = Time.now;
    [now.strftime("%Y%m%d%H"), now.strftime("%Y%m%d"), now.strftime("%Y%m"), now.strftime("%Y")].each do |time_code|
      re_word_count = find(:first, :conditions => ["time_code = ? AND word_id = ?", time_code, word_id])
      if re_word_count
        re_word_count.update_attributes(:word_count => re_word_count.word_count + 1)
      else  
        create(:time_code => time_code, :word_id => word_id, :word_count => 1)      
      end  
    end  
  end
end

module RulesEngine
  module Rule
    class <%=rule_class%> < RulesEngine::Rule::Definition

      ##################################################################
      # class options
      self.options = 
        {
          :group => 'Twitter',
          :display_name => 'Tweet Word Writer',    
          :help_partial => '/re_rule_definitions/<%=rule_name%>/help',
          :new_partial => '/re_rule_definitions/<%=rule_name%>/new',
          :edit_partial => '/re_rule_definitions/<%=rule_name%>/edit'
        } 
  
      ##################################################################
      # set the rule data
      def data= data
        if data.nil?
          @title = nil
        else
          @title, ignore = ActiveSupport::JSON.decode(data)
        end  
      end
  
      ##################################################################
      # get the rule attributes
      def title
        @title
      end
  
      def summary
        "Write the twitter words to the database"
      end
  
      def data
        [title].to_json
      end
  
      def expected_outcomes
        [:outcome => RulesEngine::Rule::Outcome::NEXT]
      end
  
      ##################################################################
      # set the rule attributes
      def attributes=(params)
        param_hash = params.symbolize_keys

        @title = param_hash[:<%=rule_name%>_title]
      end
  
      ##################################################################
      # validation and errors
      def valid?
        @errors = {}
        @errors[:<%=rule_name%>_title] = "Title required" if title.blank?    
        return @errors.empty?
      end

      ##################################################################
      # callbacks when the rule is added and removed from a workflow
      def before_create()
      end
  
      def before_update()
      end
      
      def before_destroy()
      end
  
      ##################################################################
      # execute the rule
      # this rule does nothing
      def process(process_id, data)
        (data[:tweet_words] || []).each do |word|
          word_id = Re<%=rule_class%>List.find_or_create(word)
          Re<%=rule_class%>Count.update_time_codes(word_id)
        end

        RulesEngine::Rule::Outcome.new(RulesEngine::Rule::Outcome::NEXT)
      end  
    end
  end
end