Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling super on a method defined by define_method

I have created a Model class where I define methods based on a method (attribute) called in User (which inherits from Model). The problem is that I cannot override the method defined by define_method, and call super to pass to the defined method. I guess this is because the defined method is added to User itself, and not to the Model, so it actually has no method in the superclass (i.e. Model).

The reason I want to do this is because most attributes should be saved to the database directly, while some attributes, like password, needs some additional processing.

class Model
  def self.attribute(name)
    define_method(name) do
      self
    end
  end  
end

class User < Model
  attribute :password
end

class User2 < Model
  attribute :password

  def password
    super
  end
end

@user = User.new
puts @user.password # => <User:0x00000100845540>

@user2 = User2.new
puts @user2.password
# define_super.rb:17:in `password': super: no superclass method 
# `password' for #<User2:0x00000100845578> (NoMethodError)
# from define_super.rb:25:in `<main>'

Is there any way I could change the code to get this working? I need a way to override dynamically created methods.

like image 325
Kim Joar Avatar asked Dec 09 '22 17:12

Kim Joar


1 Answers

Define the method on the superclass:

class Model
  def self.attribute(name)
    superclass.send :define_method, name do
      self
    end
  end  
end
like image 51
mtyaka Avatar answered Dec 28 '22 01:12

mtyaka