DISQUS

OLD Bamboo Blog: Micro-patterns in Ruby

  • Luke Redpath · 1 year ago
    I'm a bit puzzled as to why FlickrAdapter is a sub-class of ServiceParser. Aren't they two entirely different things? It seems like an abuse of inheritance merely to create the convenience of just calling register_url at the bottom of the adapter's definition. I would be more inclined to go with a more open interface:

    class ServiceParser
    def register_adapter(url_matcher, adapter)
    @@adapters[url_matcher] = adapter
    end
    end

    class FlickrAdapter
    # knows nothing about ServiceParser
    end
  • Luke Redpath · 1 year ago
    I'm a bit puzzled as to why FlickrAdapter is a sub-class of ServiceParser. Aren't they two entirely different things? It seems like an abuse of inheritance merely to create the convenience of just calling register_url at the bottom of the adapter's definition. I would be more inclined to go with a more open interface:

    class ServiceParser
    def register_adapter(url_matcher, adapter)
    @@adapters[url_matcher] = adapter
    end
    end

    class FlickrAdapter
    # knows nothing about ServiceParser
    end
  • Luke Redpath · 1 year ago
    OK, that didn't work - there doesn't seem to be any indication of whether you can use Textile or similar here so let's give HTML a bash:

    <pre>
    class ServiceParser
    def register_adapter(url_matcher, adapter)
    @@adapters[url_matcher] = adapter
    end
    end

    class FlickrAdapter
    # knows nothing about ServiceParser
    end
    </pre>
  • Ismael · 1 year ago
    Hi Luke.
    Yes, inheritance is probably misleading in my example, and it would certainly be an abuse if I was using ServiceParser only as a factory.

    I actually based the examples on real code where ServiceParser has lots of private methods that are used by the subclasses. In my particular code, the parent class (ServiceParser) initializes with an instance of hPricot, which is available to all subclasses to use on their particular services, and some shared exception handling routines.
  • PTL · 1 year ago
    Thanks for an interesting and informative post Ismael. It's seeing compact but expressive examples like this that makes ruby fun to learn. Keep it up!
  • Eric · 1 year ago
    Under the comment:
    # Refactored factory. No pun intended.
    did you mean
    def self.initialize(url)
    instead of
    def self.instance(url)
    ?

    That threw me for a minute.
  • Ismael · 1 year ago
    @Eric - No, self.instance is a class method, meant to be called on the class instead of an instance of that class.

    SomeFactoryClass.instance( parameter) # => some instance of some class