Dynamic super-overridable methods in Ruby – The Pug Automatic

Updated . Posted . Visible to the public.

The linked article Show archive.org snapshot shows how metapogramming macros can use define_method to make a method that can be overridden with super in the same class.

Helper

You can use the with_module_inheritance helper below if you want. It can be handy to make parts of a modularity Show archive.org snapshot trait super-able:

module StripAttribute
  as_trait do |attr|
    with_module_inheritance do
      define_method "#{attr}=" do |value|
        write_attribute(attr, value.strip)
      end
    end
  end
end

The helper implementation is here:

# ./lib/ext/module/with_module_inheritance.rb
#
# This macro allows you to define methods in a modularity trait that can be
# modified using the `super` keyword
# See https://thepugautomatic.com/2013/07/dsom/
module WithModuleInheritance
  def with_module_inheritance(&block)
    anonymous_module = Module.new
    include anonymous_module
    anonymous_module.module_eval &block
  end
end
Module.include(WithModuleInheritance)
Profile picture of Henning Koch
Henning Koch
Last edit
Henning Koch
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2023-01-27 15:25)