How to override methods on class composed in another class or module?

Hey y’all,

I’m using Rails module ActiveModel, which adds useful validation
behavior to descendants, including by reference to another class,
Errors, that in part composes it.

The initializer for the Errors class is…

def initialize(base)
@base = base
@messages = ActiveSupport::OrderedHash.new
end

…and ActiveModel implentations expose an instance of Errors with a
method #errors.

I would like to override particular methods on the Errors instance in my
ActiveModel implementation, sort of like as follows

def errors
some_state_i_want_to_add_to_the_pseudo_method_overwrite = …
super.tap {|e| e.extend
ExtraErrorsBehaviorWithStateFromCallingClassButHowToDoThis }
end

Moreover, I want this errors override to be useful if I inherit from the
ActiveModel implementation. So, in a descendant of the AR implementation
above, I might like to do this…

def errors
some_additional_state_i_want_to_add_to_the_pseudo_method_overwrite =

super.tap {|e| e.extend
EvenMoreExtraErrorsBehaviorWithStateFromCallingClassButHowToDoThis }
end

Any suggestions?

Thanks,

Grar

-----Messaggio originale-----
Da: Grary S. [mailto:[email protected]]
Inviato: gioved 17 novembre 2011 00:45
A: ruby-talk ML
Oggetto: how to override methods on class composed in another class or
module?

Hey y’all,

I’m using Rails module ActiveModel, which adds useful validation
behavior to
descendants, including by reference to another class, Errors, that in
part
composes it.

The initializer for the Errors class is…

def initialize(base)
@base = base
@messages = ActiveSupport::OrderedHash.new
end

…and ActiveModel implentations expose an instance of Errors with a
method
#errors.

I would like to override particular methods on the Errors instance in my
ActiveModel implementation, sort of like as follows

def errors
some_state_i_want_to_add_to_the_pseudo_method_overwrite = …
super.tap {|e| e.extend
ExtraErrorsBehaviorWithStateFromCallingClassButHowToDoThis } end

Moreover, I want this errors override to be useful if I inherit from the
ActiveModel implementation. So, in a descendant of the AR implementation
above, I might like to do this…

def errors
some_additional_state_i_want_to_add_to_the_pseudo_method_overwrite =

super.tap {|e| e.extend
EvenMoreExtraErrorsBehaviorWithStateFromCallingClassButHowToDoThis } end

Any suggestions?

Thanks,

Grar


Posted via http://www.ruby-forum.com/.


Caselle da 1GB, trasmetti allegati fino a 3GB e in piu’ IMAP, POP3 e
SMTP autenticato? GRATIS solo con Email.it http://www.email.it/f

Sponsor:
Conto Arancio al 4,20%. Soldi sempre disponibili, zero spese, aprilo in
due minuti!
Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid920&d)-12