1

Currently I have several classes, each of which deals with different sites. They act like the same type, in the sense that they all have crawl_item() method.

class CrawlA def crawl_item ... A.create() end end class CrawlB def crawl_item B.create() end end 

Now I want to catch one model validation exception when calling any of these crawl_item methods, and perform the same rescue action. What would be a good way to implement this?

2
  • I guess we can assume that the methods crawl_item are different in each class, aren't they? Commented Aug 1, 2012 at 12:45
  • @tokland yes they do crawl but the implementations are quite different Commented Aug 3, 2012 at 1:44

1 Answer 1

3

To keep it DRY you can write a wrapper using the classical mix-in structure (module + class include):

module CrawlValidator def with_validations begin yield rescue => exc ... end end end class CrawlA include CrawlValidator def crawl_item with_validations { A.create } end end 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.