Refactoring2-4
役割の分割
1つのメソッドに二つ以上の作業をさせないという基本方針にしたがって,「SVGの出力」と「描画のパラメータ指定部」を分割する. 具体的には,rect, text, sparkあたりを換える.
>|ruby| refact前 def rect(cx,cy)
%Q{<rect x="#{cx-2}" y="#{cy-2}" width="4" height="4" fill="red" stroke="none" stroke-width="0" />}
end
def text(cx,cy,value)
%Q{<text x="#{cx}" y="#{cy}" font-family="Verdana" font-size="9" fill="red" >#{value}</text>}
end def spark(cx, cy, value)
%Q{#{rect(cx,cy)}
- {text(cx+6,cy+4,value)}}
end
< |
>|ruby| refact後 def rect(cx,cy,width,height,fill,stroke,stroke_width)
%Q{<rect x="#{cx-2}" y="#{cy-2}" width="#{width}" height="#{height}" fill="#{fill}" stroke="#{stroke}" stroke-width="#{stroke_width}" />}
end
def text(cx,cy,msg,font_family,font_size,fill)
%Q{<text x="#{cx}" y="#{cy}" font-family="#{font_family}" font-size="#{font_size}" fill="#{fill}" >#{msg}</text>}
end
SQ=4 SPARK_COLOR='red' def spark(cx, cy, value)
%Q{#{rect(cx-SQ/2,cy-SQ/2,SQ,SQ,'red','none',0)} #{text(cx+6, cy+4, value,"Verdana","9",SPARK_COLOR)}}
end
< |
Keyword(s):
References:[RubyPrimary]