Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: Raise error in Module if class method not found

I would like to put some code in module that throws an error if certain method is not defined. This module relies on the external definition of this method, since this method's implementation is different for all classes. This code would help developers know early that they forgot to implement the method rather than when they tried to use features of the module.

module MyModule
  def self.included(klass)
    raise "MyModule: please `def my_method` on #{klass}" unless klass.respond_to?(:my_method)
  end
end 

I can easily raise an error in a module's included definition if a method is not defined, however since most modules are included at the top of a file, it's likely that my required method is defined in the class, but not before my module is included.

class MyClass
  include MyModule
  def self.my_method
    # ...
  end
end

This would still raise an error :(

Is it possible to raise an error only if the method truly is not defined in the class definition? Almost need a class.onload callback of sorts. If not, any other ideas for how to mitigate the possibilities that a programmer might include our module without defining this needed method?

like image 813
Schneems Avatar asked Dec 12 '22 10:12

Schneems


2 Answers

Sounds like you want to make use of method_missing and define_method.

If you do use method_missing don't forget to:

  • call super for unhandled cases.
  • also implement a respond_to? method

look at this question, plus this and that.

Update:

It sounds the goal is to do static method checking like Java or c++ does. This is not really meaningful in ruby :-(

Since in ruby:

  • Each instance of an object has its own eigenclass. A given object may have the necessary methods mixed in at runtime. So just because Foo does not have a method at class load time is meaningless.
  • Frameworks like RoR hooks method_missing and dynamically create methods needed for the database query methods, so the method may exist (or not) when it is needed.

With regards to "class on load": A class definition is really executed. Try this:

class Foo 
  p "Hi"
end

You will see "Hi" the first and only the first time Foo is used. This is how things like devise hook into do their magic.

class User < ActiveRecord::Base
  # **CALL 'devise' method**
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable

  # **CALL attr_accessible method**
  attr_accessible :email, :password, :password_confirmation
end

So maybe by private convention have developers add a check_class method call to the bottom of the classes in question?

I understand the intent but it seems like fighting the way ruby is designed to work.

As a mostly Java person I appreciate the frustration. Let me guess: repeated cases of code getting pushed to production that had missing methods? :-P

Update2:

wrt onload In ruby barring use of frozen a class get new methods defined all the time. ( Or an instance can get new methods defined just for that instance. ) so checking for a method's nonexistence is only a snapshot check and not as definitive a check as a static language brings to the table. This is ruby's very own Halting problem

like image 139
Pat Avatar answered Dec 29 '22 16:12

Pat


How about declaring a method with that name, which just raises an error, to make sure the user redefines the method?

module MyModule
  def my_method
    raise "Please implement me"
  end
end


class MyClass 
  include MyModule
  def my_method
    # do something
  end
end
like image 37
Dogbert Avatar answered Dec 29 '22 16:12

Dogbert