在轨道模型上重构ruby

时间:2012-01-13 17:39:24

标签: ruby ruby-on-rails-3 model refactoring

鉴于以下代码,

你如何重构这个,以便方法search_word可以访问issueid?

我想说改变函数search_word使得它接受3个参数或者使issueid成为实例变量(@issueid)可以被视为不良实践的一个例子,但老实说我找不到任何其他解决方案。如果除此之外没有其他解决方案,您是否介意解释为什么没有其他解决方案?

请记住它是Ruby on Rails模型。

def search_type_of_relation_in_text(issueid, type_of_causality)
    relation_ocurrences = Array.new
    keywords_list = { 
        :C => ['cause', 'causes'],
        :I => ['prevent', 'inhibitors'],
        :P => ['type','supersets'],
        :E => ['effect', 'effects'],
        :R => ['reduce', 'inhibited'],
        :S => ['example', 'subsets'] 
    }[type_of_causality.to_sym]  

    for keyword in keywords_list
        relation_ocurrences + search_word(keyword, relation_type)
    end        

    return relation_ocurrences
end


def search_word(keyword, relation_type)
relation_ocurrences = Array.new

@buffer.search('//p[text()*= "'+keyword+'"]/a').each { |relation|

    relation_suggestion_url   = 'http://en.wikipedia.org'+relation.attributes['href']
    relation_suggestion_title = URI.unescape(relation.attributes['href'].gsub("_" , " ").gsub(/[\w\W]*\/wiki\//, ""))

    if not @current_suggested[relation_type].include?(relation_suggestion_url)
        if @accepted[relation_type].include?(relation_suggestion_url)
            relation_ocurrences << {:title => relation_suggestion_title, :wiki_url => relation_suggestion_url, :causality => type_of_causality, :status => "A", :issue_id => issueid}
        else
            relation_ocurrences << {:title => relation_suggestion_title, :wiki_url => relation_suggestion_url, :causality => type_of_causality, :status => "N", :issue_id => issueid}
        end

    end
} 

end

1 个答案:

答案 0 :(得分:5)

如果您需要其他上下文,请将其作为附加参数传递。这就是它应该如何运作。

设置@ - 类型实例变量以传递上下文是您已识别的错误形式。

您似乎没有意识到许多Ruby约定:

  • 而不是Array.new只使用[ ],而不是Hash.new使用{ }
  • 使用case语句或常量而不是定义哈希,然后只检索其中一个元素,丢弃剩余部分。
  • 除非严格必要,否则请避免使用return,因为默认情况下始终会返回上一个操作。
  • 使用array.each do |item|代替for item in array
  • 对多行块使用do ... end而不是{ ... },其中大括号版本通常为单行保留。避免与哈希声明混淆。
  • 尝试避免在差异较小时复制大块代码。例如,声明一个临时变量,有条件地操纵它,然后存储它而不是定义多个独立变量。

考虑到这一点,这是对它的改造:

KEYWORDS = { 
    :C => ['cause', 'causes'],
    :I => ['prevent', 'inhibitors'],
    :P => ['type','supersets'],
    :E => ['effect', 'effects'],
    :R => ['reduce', 'inhibited'],
    :S => ['example', 'subsets'] 
}

def search_type_of_relation_in_text(issue_id, type_of_causality)
  KEYWORDS[type_of_causality.to_sym].collect do |keyword|
    search_word(keyword, relation_type, issue_id)
  end
end

def search_word(keyword, relation_type, issue_id)
  relation_occurrences = [ ]

  @buffer.search(%Q{//p[text()*= "#{keyword}'"]/a}).each do |relation|
    relation_suggestion_url = "http://en.wikipedia.org#{relation.attributes['href']}"
    relation_suggestion_title = URI.unescape(relation.attributes['href'].gsub("_" , " ").gsub(/[\w\W]*\/wiki\//, ""))

    if (!@current_suggested[relation_type].include?(relation_suggestion_url))
      occurrence = {
        :title => relation_suggestion_title,
        :wiki_url => relation_suggestion_url,
        :causality => type_of_causality,
        :issue_id => issue_id
      }

      occurrence[:status] =
        if (@accepted[relation_type].include?(relation_suggestion_url))
          'A'
        else
          'N'
        end

      relation_ocurrences << occurrence
    end
  end 

  relation_occurrences
end