English 中文(简体)
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.

问题回答

Define the method on the superclass:

class Model
  def self.attribute(name)
    superclass.send :define_method, name do
      self
    end
  end  
end

The way Rails deals with this is that there are multiple ways to get attributes. One of these is (by convention) never overridden so it can be used in your defined methods:

# This method is never overridden, but also rarely used as a public method
def[](key)
  # Returns value of `key` attribute
end

# This is the effective default implementation of an attribute
def att1
  self[:att1]
end

# This shows how you can have custom logic but still access the underlying value
def att2
  self[:att2] unless self[:att2].empty?
end

Using superclass.send :define_method might lead to some unexpected behaviour, because that method would be available to every Model subclasses.

It is well described in this article https://thepugautomatic.com/2013/07/dsom/

So following this article you could define your attribute method like this

class Model
  MODULE_NAME = :DynamicAttributes

  def self.attribute(name)
    if const_defined?(MODULE_NAME, _search_ancestors = false)
      mod = const_get(MODULE_NAME)
    else
      mod = const_set(MODULE_NAME, Module.new)
      include mod
    end

    mod.module_eval do
      define_method(name) do
        self
      end
    end
  end
end




相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签