Executing a mixin method at the end of a class definition - ruby

I have a Mix-in that reflects on the receiver class to generate some code. This means that I need to execute the class method at the end of the class definition, like in this trivially dumbed down example:
module PrintMethods
module ClassMethods
def print_methods
puts instance_methods
end
end
def self.included(receiver)
receiver.extend ClassMethods
end
end
class Tester
include PrintMethods
def method_that_needs_to_print
end
print_methods
end
I'd like to have the mixin do this for me automatically, but I can't come up with a way. My first thought was to add receiver.print_methods to self.included in the mixin, but that won't work because the method that I want it to reflect on has not been declared yet. I could call include PrintMethods at the end of the class, but that feels like bad form.
Are there any tricks to make this happen so I don't need to call print_methods at the end of the class definition?

First of all, there's no end of class definition. Remember that in Ruby you can reopen the Tester class method after you have 'initialized' it, so the interpreter can't know where the class 'ends'.
The solution I can come up with is to make the class via some helper method, like
module PrintMethods
module ClassMethods
def print_methods
puts instance_methods
end
end
def self.included(receiver)
receiver.extend ClassMethods
end
end
class Object
def create_class_and_print(&block)
klass = Class.new(&block)
klass.send :include, PrintMethods
klass.print_methods
klass
end
end
Tester = create_class_and_print do
def method_that_needs_to_print
end
end
But certainly having to define classes this way makes my eyes hurt.

Related

How to access class method from the included hook of a Ruby module

I'd like my module to define new instance methods based on its including class' instance methods. But in the included hook, the class methods are not defined yet (as the module is included at the top of the class, before the class methods are defined):
module MyModule
def self.included(includer)
puts includer.instance_methods.include? :my_class_method # false <- Problem
end
end
class MyClass
include MyModule
def my_class_method
end
end
I want the users of the module to be free to include it at the top of their class.
Is there a way to make a module define additional methods to a class?
Note: I don't have to use the included hook if there is another way to achieve this.
There'a a method_added callback you could use:
module MyModule
def self.included(includer)
def includer.method_added(name)
puts "Method added #{name.inspect}"
end
end
end
class MyClass
include MyModule
def foo ; end
end
Output:
Method added :foo
If you want to track both, existing and future methods, you might need something like this:
module MyModule
def self.on_method(name)
puts "Method #{name.inspect}"
end
def self.included(includer)
includer.instance_methods(false).each do |name|
on_method(name)
end
def includer.method_added(name)
MyModule.on_method(name)
end
end
end
Example:
class MyClass
def foo ; end
include MyModule
def bar; end
end
# Method :foo
# Method :bar

Wrapping a class method?

I have a module I'd like to be able to wrap every method in a class, whether it be an instance method or a class method, but am having a bit of trouble trying to get it to work in both circumstances. Below is what I have right now, it works for wrapping instance methods but calling class methods doesn't seem to work. I assume the module proxy isn't installed correctly for overriding class methods, but I'm not sure what to do to fix that.
I'm limited to ruby 2.7, btw.
module Wrapper
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
module Proxy
end
def wrap_method(name)
puts "wrapping #{name}"
Proxy.define_method(name) do |*args|
puts "in wrap_method, about to call #{name}"
super *args
end
end
def wrap_methods
# wrap any methods that were defined before this was called
self.instance_methods(false).each {|name| wrap_method(name) }
self.singleton_methods(false).each {|name| wrap_method(name) }
# wrap any methods that are defined after this is called
self.class.define_method(:singleton_method_added) do |name|
if respond_to?(:wrap_method)
wrap_method(name)
end
end
self.class.define_method(:method_added) do |name|
if respond_to?(:wrap_method)
wrap_method(name)
end
end
self.prepend(Proxy)
end
end
end
module Top
module Second
class Main
include Wrapper
wrap_methods
def self.first_class_method
puts "in self.first_class_method"
end
def self.second_class_method
puts "in self.second_class_method"
end
def some_instance_method
puts "in some_instance_method"
end
end
end
end
Top::Second::Main.first_class_method
Top::Second::Main.second_class_method
Top::Second::Main.new.some_instance_method
Output:
wrapping first_class_method
wrapping second_class_method
wrapping some_instance_method
in self.first_class_method
in self.second_class_method
in wrap_method, about to call some_instance_method
in some_instance_method
You prepend Proxy to the class itself, and this modifies the method resolution for instances of the class, but not for the class itself.
To achieve the second, you have to
self.singleton_class.prepend(Proxy)
in addition.

How to best abstract class variables in Ruby

I've got a class such as
# this has been simplified for the example
class MyClass
##private_attributes = []
def self.private_attributes(*args)
##private_attributes = args
end
def private_attributes
#private_attributes ||= ##private_attributes
end
end
It works great. I've this ##private_attributes set at class level which's then used at instance level in multiple ways.
I want to abstract this logic somewhere else to simplify my class, something like that
class MyClass
include PrivateAttributes
end
When I create the module PrivateAttributes, however I shape it, the ##private_attributes isn't understood at MyClass level.
I tried many things but here's the latest code attempt
module PrivateAttributes
include ProcessAttributes
def self.included(base)
base.extend(ClassMethods)
base.include(InstanceMethods)
end
module ClassMethods
##private_attributes = []
def private_attributes(*args)
##private_attributes = args
end
end
module InstanceMethods
def private_attributes
#private_attributes ||= process_attributes_from(##private_attributes)
end
def private_attributes?
instance_options[:scope] == :private
end
end
end
It crashes with this error
NameError:
uninitialized class variable ##private_attributes in PrivateAttributes::InstanceMethods
Did you mean? private_constant
In short, the ##private_attributes isn't transferred throughout the code, but looks like it stays at the module level.
What's the best way to abstract this logic from my original class ?
Working solution
An easy workaround is to use mattr_accessor on the class level or anything similar to communicate our data around. I preferred to write down my own methods in this case:
module PrivateAttributes
include ProcessAttributes
def self.included(base)
base.extend(ClassMethods)
base.include(InstanceMethods)
end
module ClassMethods
##private_attributes_memory = []
def private_attributes(*args)
##private_attributes_memory = args
end
def private_attributes_memory
##private_attributes_memory
end
end
module InstanceMethods
def private_attributes
#private_attributes ||= process_attributes_from private_attributes_memory
end
# you can add diverse methods here
# which will be used in MyClass once included
private
def private_attributes_memory
self.class.private_attributes_memory
end
end
end

understanding self.including in ruby

Suppose I have
module Mod
def self.included(base)
some_method
end
def some_method
end
end
class A
include Mod
end
I get some_method is not defined. So how can call some_method as soon as Mod is included ?
You have to create a base class instance to call it.
module Mod
def self.included(base)
base.new.some_method
end
def some_method
end
end
class A
include Mod
end
After including module Mod, some_method will be available as instance method of the instances of class A.
The included method is called on class level, when the module is included into a class and some_method is called on class level too. Therefore some_method needs to be a class method to be found. This will work (note the self.some_method):
module Mod
def self.included(base)
some_method
end
def self.some_method
end
end
class A
include Mod
end
Or you need to create an instance of your base class first and call some_method on that instance like #ArupRakshit mentioned in his answer.
As it has been told by spickermann that :included method is called on class level when module is being included into a class, same way :some_method needs to be called on class level too as it is being called from :included method, we can use base.class_eval as class_eval can be used to add methods to a class.
module Mod
def self.included(base)
base.class_eval do
some_method
end
end
def some_method
end
end
class A
include Mod
end
Here you can do the following with :some_method
A.some_method
#=> nil
A.new.some_method
#=> nil

Add class methods through include

I have Ruby class into which I want to include both class and instance methods. Following the pattern described here, I'm currently using the following:
class SomeObject
include SomeObject::Ability
def self.some_builder_method(params)
# use some_class_method ...
end
end
module SomeObject::Ability
module ClassMethods
def some_class_method(param)
# ...
end
end
def self.included(klass)
klass.extend(ClassMethods)
end
def some_instance_method
# ...
end
end
I'd rather not make two separate modules (one being included and the other being extended), because all the methods in my module logically fit together. On the other hand, this pattern a) requires me to define an additional ClassMethods module and b) requires me to write a boilerplate self.included method for every module.
Is there a better way to do this?
Edit 1: I've found another way, but I'm unsure if this is better than the first.
module Concern
def included(base)
# Define instance methods.
instance_methods.each do |m|
defn = instance_method(m)
base.class_eval { define_method(m, defn) }
end
# Define class methods.
(self.methods - Module.methods).each do |m|
unless m == __method__
base.define_singleton_method(m, &method(m))
end
end
end
end
module SomeModule
extend Concern
def self.class_m
puts "Class"
end
def instance_m
puts "Instance"
end
end
class Allo
include SomeModule
end
Allo.class_m # => "Class"
Allo.new.instance_m # => "Instance"
If I understand you correctly, you really just want to use ActiveSupport::Concern:
module PetWorthy
extend ActiveSupport::Concern
included do
validates :was_pet, inclusion: [true, 'yes']
end
def pet #instance method
end
module ClassMethods
def find_petworthy_animal
# ...
end
end
end
class Kitty
include PetWorthy
end
Kitty.find_petworthy_animal.pet
You (hopefully obviously) don't need to use the included method if you don't have any behavior to trigger on include, but I put it in just to demonstrate.

Resources