Simon
Simon

Reputation: 25983

How can I omit the middle of a chain of methods?

Suppose I have a class with a save method, and three mixins which modify it using aliasing, e.g.

module Callbacks
  def save_with_callbacks
    callback :before_save
    save_without_callbacks
    end
  end

  alias_method_chain :save, :callbacks
end

and similarly for save_with_transaction and save_with_timestamps, mixed in in that order, so MyModel#save calls save_with_timestamps, which calls save_with_transaction, which calls save_with_callbacks, which finally calls the original save.

Now suppose I want to save without a transaction. I can call save_without_transaction, but that doesn't call the code to set the timestamps.

How can I save my model with timestamps and callbacks, but no transaction?

I could reorder the mixins, but my question is about omitting the middle, rather than specifically transactions. How can I omit the middle of a chain of methods?

NOTE - I've used RoR's alias_method_chain for brevity, but my question applies to ruby in general

Upvotes: 1

Views: 96

Answers (1)

Andy
Andy

Reputation: 1490

Your only option seems to be -- to me, anyway -- to provide a second calling chain that handles this functionality further up. I don't think you can do this with aliasing; you're going to have to define a new method that calls the old method, instead - so something like:

  1. MyModel#save <-save_with_timestamps <- save_with_transaction <- save_with_callbacks <-save.
  2. MyModel#save_other <-save_with_timestamps_other <- save_with_callbacks_other <-save_other.

(As an aside, aren't long chains of aliases going to make your code very difficult to read? Personally I think I would work hard to avoid them.)

Upvotes: 2

Related Questions