ã¯ããã«
ã¯ããã¾ãã¦ããçæãããMisocaã§ã¤ã³ã¿ã¼ã³ããã¦ããhmryuã§ããMisocaã«ã¸ã§ã¤ã³ããåã¯ãå人ã§ãµã¼ãã¹ãä½ã£ãããç 究ã§ããã°ã©ã ãæ¸ããããã¦ãã¾ããã
ä¸æ¹ã§ããã¼ã ã§éçºããçµé¨ã¯ãã¾ããªããMisocaã«ã¸ã§ã¤ã³ããå§ãã®é ã¯æ £ããªããã¨ã°ããã§ãããä¸ã§ããä»äººã®æ¸ããã½ã¼ã¹ã³ã¼ããèªãã§ç解ãããã¨ããä¸çªå¤§å¤ã ã£ãããããã¾ããã
ããã§ä»åã¯ãæ©è½è¿½å ã»å¤æ´ãå ããããã«ã½ã¼ã¹ã³ã¼ã*1ãèªãä¸ã§ãåã大åã ã¨æãã3ã¤ã®ã¹ãããã«ã¤ãã¦æ¸ãããã¨æãã¾ãã
1. æ©è½ã¨ã½ã¼ã¹ã³ã¼ãã®å¯¾å¿ã調ã¹ã
ã¾ããèªåãå¤æ´ãå ããæ©è½ãã©ããªãã®ã§ãã©ãã«å®è£ ããã¦ããã®ãç解ããå¿ è¦ãããã¾ããå®éã«ãµã¼ãã¹ãåããã¦ãã©ããªæ©è½ãªã®ãã確èªãã¾ãããã®å¾ããã®æ©è½ãã½ã¼ã¹ã³ã¼ãã®ã©ã®é¨åã«å¯¾å¿ããã®ãã調ã¹ã¾ãã
ä¾ãã°ãã¡ã¼ã«éä¿¡ã«ã¤ãã¦èª¿ã¹ãå ´åã¯ãå®éã®ç»é¢ãéãform
ã¿ã°ã®action
å±æ§ã調ã¹ã¦ãconfig/routes.rb
ã¨ç
§ããåããããã¨ã§ãå¼ã°ãã¦ããã¢ã¯ã·ã§ã³ãç¹å®ãã¾ãã
# config/routes.rb # ... resources :mails do collection do post 'distribute' end end # ...
ããã§ã¯ãMailsController
ã®distribute
ã¢ã¯ã·ã§ã³ãå¼ã°ãã¦ãããã¨ããããã¾ããã
2. å¦çå ¨ä½ã®æµãã追ã
次ã«ãå®éã«ã©ã®ãããªå¦çãããã¦ããã®ããææ¡ãããããã½ã¼ã¹ã³ã¼ããèªãã§ããã¾ããããã§ã¯ã ã¡ã¼ã«éä¿¡ã®å¦çãèªãã§ããã±ã¼ã¹ãèãã¦ã¿ã¾ããã¡ã¼ã«éä¿¡ã®ã¢ã¯ã·ã§ã³ã¯ä»¥ä¸ã®ããã«å®è£ ããã¦ãã¾ãã
class MailsController def distribute # # ... logic ... # distribute = Misoca::Mail::Distribute.new distribute.call # # ... logic ... # end end
èªãã§ã¿ãã¨ãdistribute
ã¢ã¯ã·ã§ã³ã®ä¸ã§ãMisoca::Mail::Distribute
ã®ã¤ã³ã¹ã¿ã³ã¹ãçæãããcall
ãå®è¡ããã¦ãããã¨ããããã¾ããããã®ä¸ã§ã¡ã¼ã«éä¿¡ããã¦ããããªã®ã§ãã¢ããªå
¨ä½ããMisoca::Mail::Distribute
ãå®ç¾©ããã¦ããå ´æãæ¢ãã¾ãã
ãã®ã¨ããã¢ããªå
¨ä½ã®æ§æãææ¡ãã¦ããã¨ãä½æ¥ãæãã¾ããMisocaã®ã¢ã¼ããã¯ãã£ã¼ã¯The Clean Architecture*2ã§æ§æããã¦ãããMisoca::Mail::Distribute
ã®ãããªã¯ã©ã¹ã¯app/misoca
以ä¸ã«[ã°ã«ã¼ãå]/[åå].rb
ãã¨ããååã§ä¿åããã¨ã«ã¼ã«ã決ã¾ã£ã¦ãã¾ã*3ã
module Misoca module Mail class Distribute def call # # ... logic ... # document = find_document document_distributor = Misoca::Mail::DocumentDistributor.new document_distributor.build_to_mail(document) # # ... logic ... # end private def find_document # # ... logic ... # end end end
Misoca::Mail::Distribute
ã®call
ã§ã¯ãfind_document
ã§ã¡ã¼ã«éä¿¡ããææ¸ãæ¢ãã¦ãã¾ãããã®å¾ãMisoca::Mail::DocumentDistributor
ã®ã¤ã³ã¹ã¿ã³ã¹ãçæãããbuild_to_mail
ãå®è¡ããã¦ãããã¨ãåããã¾ãã
ãã®ãããªæãã§ãã©ããªå¦çãããã¦ããã®ãã調ã¹ã¦ããã¾ããè¤éãªã¨ããªã©ã¯ã調ã¹ã¦ãã¦æ··ä¹±ããªãããã«ãä¸è¨ã®ãããªç°¡åãªã¡ã¢ãæ¸ãã¦ãããããã¾ããããã§ã¯ãçåã«æã£ããã¨ãªã©ãç°¡åã«æ¸ãã¦ããã¾ãã
ã¾ããä¸è¨ã®æµã以å¤ã«ãå¿ è¦ã«å¿ãã¦ãã³ã¼ã«ããã¯ããã£ã«ã¿ãªã©ãæ°ã«ãã¦ããããã«ãã¦ãã¾ãã
3. å¤æ°ããã¼ã¿ãã¼ã¹ã®ä¸èº«ãè¦ã
ã½ã¼ã¹ã³ã¼ããèªã¿é²ããä¸ã§ãããã®æç¹ã§ãã®ãªãã¸ã§ã¯ãã¯ã©ããªç¶æ ã«ãªã£ã¦ããã®ãï¼ãããããå®è¡ãããå¾ããã®ã¢ãã«ã®ã¬ã³ã¼ãã¯å¢ãã¦ããã®ãï¼ãã¨ãã£ãçåãåºã¦ãã¾ããä¸éãæµããææ¡ããããå¤æ°ããã¼ã¿ãã¼ã¹ã®ä¸èº«ã調ã¹ã¦ã¿ã¾ãã
åã¯ãpry
ã¨ããGemã使ã£ã¦ãã¾ãã
ã½ã¼ã¹ã³ã¼ãã®ä»»æã®å ´æã«binding.pry
ã¨è¨è¿°ããã¨ããã®æç¹ã§ããã°ã©ã ã®å®è¡ãä¸æåæ¢ãããã¨ãã§ãã¾ãã
def call # # ... logic ... # document = find_document document_distributor = Misoca::Mail::DocumentDistributor.new binding.pry # ãã¬ã¼ã¯ãã¤ã³ããè¨å® document_distributor.build_to_mail(document) # # ... logic ... # end
ãµã¼ãã¼ãèµ·åããã¦ããã³ã³ã½ã¼ã«ãä¸æåæ¢ããç¶æ ã§ãå¤æ°ããã¼ã¿ãã¼ã¹ã®ä¸èº«ãè¦ããã¨ãã§ãã¦ãã¨ã¦ã便å©ã§ãã
[1] pry(#<Misoca::Mail::Distribute>)> Mail.count => 1 [2] pry(#<Misoca::Mail::Distribute>)> next # 次ã®è¡ãå®è¡ [3] pry(#<Misoca::Mail::Distribute>)> Mail.count => 2
ãã®ä¾ã§ã¯ãbuild_to_mail
ã§Mail
ã®ã¬ã³ã¼ããå¢ãã¦ãããã¨ã確èªã§ãã¾ããã
ã¾ã¨ã
ä»åã¯ãã½ã¼ã¹ã³ã¼ããèªãã¨ãã®3ã¤ã®ã¹ãããã«ã¤ãã¦æ¸ãã¾ãããã¾ã åãå§ãã¦1ã¶æãããã§ãããMisocaã®ã¤ã³ã¿ã¼ã³ã§ã¯ãæ§ã ãªãã¨ãå¦ã¹ã¦ãã¾ããå°ãã§ãæ©ãæé·ãã¦ãå¦ãã ç¥èãæè¡ããã®ããã°ã§éå ã§ããã°ã¨æã£ã¦ãã¾ãã
*1:MisocaãRailsã§å®è£ ããã¦ããã®ã§ãããã§ã¯ãRailsã®ã½ã¼ã¹ã³ã¼ããèªããã¨ãæ³å®ãã¦ãã¾ãã
*2:The Clean Architecture | 8th Light
*3:Misocaã§ã¯ãã¢ããªã®ä»æ§ãªã©ãesa.ioã«ã¾ã¨ãã¦ããã¾ãã