2

I'm trying to create a component system in Ruby using the observer pattern. Components must be defined as modules because they exist only to be mixed in to a ComponentContainer. But there are certain methods that Components have, which I'd ideally like to define in some kind of base class, but I can't do that since they're modules.

Here's what I'd like to do:

module Component
  def self.on(event, &block)
    #definition..
  end

  def self.fire(event)
    #pass event to subscribers
  end
end

module FooComponent < Component
  on :foo_event do |param1, param2|
    #...
  end
end

The different types of Components use the on and fire methods, but they can't inherit them, because modules can't have parents. What should I do? Is this not ruby-like?

I could get this to work by making Component and FooComponent classes, but then I can't mix them into a ComponentContainer using extend or include.

3
  • What's a ComponentContainer? I'm a Ruby ignoramus, but this sounds interesting. May 23, 2011 at 17:23
  • @Tom Anderson: I haven't made it yet, but a ComponentContainer is just a class that will hold a composition of components. A ComponentContainer filled with components has the functionality of all of its components (methods, etc).
    – ryeguy
    May 23, 2011 at 17:32
  • By the way, Ruby ships with the Observable module.
    – steenslag
    May 23, 2011 at 21:23

3 Answers 3

3

A clean way to do this is to abstract away the use of extend using the Module#included hook method. This method is called on a module with a reference to the base that is including it. What this code does is creates a Component module that automatically extends the base with the desired methods:

module Component
  def self.included(base)
    base.extend Methods
  end

  module Methods
    def on(event, &block)
      # ...
    end

    def fire(event)
      # ...
    end
  end
end

module FooComponent
  include Component

  on :foo_event do |param1, param2|
    # ...
  end
end
4
  • 1
    Why would I do this instead of simply using extend?
    – ryeguy
    May 23, 2011 at 20:23
  • @ryeguy: If you abstract the details involved with doing something to a common interface, then you won't need to change as much code if something needs to change with your component model. This is commonly known as modularity.
    – Aaa
    May 23, 2011 at 21:53
  • I don't think you got what I meant. If you take everything you've put in the Methods module directly into Component, you could achieve the same effect as you have here by using extend Component instead of include Component. There is no point in using include and then using the included hook. extend already does this. Unless I'm misunderstanding what you meant.
    – ryeguy
    May 24, 2011 at 19:05
  • @ryeguy: The point is that, by using the same interface for all modules (include), your code is more regular and you won't need to remember as many special cases. You make FooComponent behave like a normal module. You aren't adding functionality by doing this, you're just making your work easier.
    – Aaa
    May 28, 2011 at 21:43
2

What if you created a separate module called BaseComponent that defined the basic methods all component modules should have, and then in your custom components, include that BaseComponent.

Then, you should still be able to mixin those custom components into your ComponentContainer class.

2

You can extend the Component's methods into your "child"-module:

module Component
  extend self # (only if you also want to allow calling via Component.on like in the original example)

  def on(event, &block)
    #definition..
  end

  def fire(event)
    #pass event to subscribers
  end
end

module FooComponent
  extend Component

  on :foo_event do |param1, param2|
    #...
  end
end
2
  • Why is the eigenclass stuff necessary? If you take out the extend self from Component, and change the first line of FooComponent to extend Component, it would have the same effect, wouldn't it?
    – ryeguy
    May 23, 2011 at 17:44
  • You can extend instances too: str = "a"; str.extend Component; str.fire
    – steenslag
    May 23, 2011 at 21:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.