Edit: Forgot to include [Ace] in the headline
I'm trying to get a simple module working that will register instances of classes that include the module.
I'm doing this because I hate overwriting DataManager#load_database for every script that does stuff in Marshal loaded classes.
This was (in essence), my first attempt:
module Register @@resources = [] def include(value) @@resources << value endendmodule Registrar def load_because_marshal_skips_initialize msgbox("beep beep") end alias :registration_initialize :initialize def initialize(*args = nil) registration_initialize(args) Register.include(self) endendclass ::RPG::Actor include Registrarendmodule :😀ataManager extend Register class << self alias :register_load_database :load_database def load_database register_load_database return unless !(@@resources.nil? || @@resources.empty?) @@resources.each { |x| x.load_because_marshal_skips_initialize } @@resources.clear end endendIt obviously fails because initialize doesn't even get called - so Registrar#initialize doesn't do jack diddly, meaning that the instance is never registered on the DataManager.
So, attempt #2:
I learned about Module#included
module Registrar def included(base) Register.include(base) endendWell isn't that nifty, now I have a single entry...and it is the class, rather than an instance of RPG::Actor. Not what I was hoping for, but it makes complete sense.
So onto attempt #3:
module Registrar def self.included(base) if base.method_defined?🙂new) base.define_method🙂register_new, self.register_new) alias_method🙂new, :register_new) end end def self.register_new(*args) result = new(args) Register.include(result) return result endendI'm not comfortable with extending #new, so I would like to avoid it; However, not even this approach worked
My final attempt is to basically just add an extension to each Marshal loaded class that can be explicitly invoked by the DataManager.
Not a good strategy, but I've exhausted myself for today and was hoping that you fine folk could offer a smarter alternative.
Any suggestions to clean this up and get it working would be seriously appreciated.
I'm trying to get a simple module working that will register instances of classes that include the module.
I'm doing this because I hate overwriting DataManager#load_database for every script that does stuff in Marshal loaded classes.
This was (in essence), my first attempt:
module Register @@resources = [] def include(value) @@resources << value endendmodule Registrar def load_because_marshal_skips_initialize msgbox("beep beep") end alias :registration_initialize :initialize def initialize(*args = nil) registration_initialize(args) Register.include(self) endendclass ::RPG::Actor include Registrarendmodule :😀ataManager extend Register class << self alias :register_load_database :load_database def load_database register_load_database return unless !(@@resources.nil? || @@resources.empty?) @@resources.each { |x| x.load_because_marshal_skips_initialize } @@resources.clear end endendIt obviously fails because initialize doesn't even get called - so Registrar#initialize doesn't do jack diddly, meaning that the instance is never registered on the DataManager.
So, attempt #2:
I learned about Module#included
module Registrar def included(base) Register.include(base) endendWell isn't that nifty, now I have a single entry...and it is the class, rather than an instance of RPG::Actor. Not what I was hoping for, but it makes complete sense.
So onto attempt #3:
module Registrar def self.included(base) if base.method_defined?🙂new) base.define_method🙂register_new, self.register_new) alias_method🙂new, :register_new) end end def self.register_new(*args) result = new(args) Register.include(result) return result endendI'm not comfortable with extending #new, so I would like to avoid it; However, not even this approach worked
My final attempt is to basically just add an extension to each Marshal loaded class that can be explicitly invoked by the DataManager.
Not a good strategy, but I've exhausted myself for today and was hoping that you fine folk could offer a smarter alternative.
Any suggestions to clean this up and get it working would be seriously appreciated.