创建动态方法名称Ruby on Rails

时间:2022-07-26 22:28:49

The devise source code shows for the mappings:

设计源代码显示映射:

def self.define_helpers(mapping) #:nodoc:
  mapping = mapping.name

  class_eval <<-METHODS, __FILE__, __LINE__ + 1
    def authenticate_#{mapping}!(opts={})
      opts[:scope] = :#{mapping}
      warden.authenticate!(opts) if !devise_controller? || opts.delete(:force)
    end

    def #{mapping}_signed_in?
      !!current_#{mapping}
    end

    def current_#{mapping}
      @current_#{mapping} ||= warden.authenticate(scope: :#{mapping})
    end

    def #{mapping}_session
      current_#{mapping} && warden.session(:#{mapping})
    end
  METHODS

  ActiveSupport.on_load(:action_controller) do
    helper_method "current_#{mapping}", "#{mapping}_signed_in?", "#{mapping}_session"
  end
end

Which allows for creation of method current_user, user_signed_in? and others.

哪个允许创建方法current_user,user_signed_in?和别的。

I want to learn how I can create my own dynamic naming for methods like such

我想学习如何为这样的方法创建自己的动态命名

When I try to simulate it with this:

当我尝试用它来模拟它时:

class Devise
    mapping = "user"
    def self.current_#{mapping}
        puts "hello"
    end
    def self.puts_current
        puts @current_#{user}
    end
end

Devise.current_user

I get error: undefined method 'current_user' for Devise:Class (NoMethodError)

我得到错误:Devise的未定义方法'current_user':Class(NoMethodError)

2 个解决方案

#1


2  

You're missing the class_eval call that Devise is using. This is what allows them to use string interpolation to create methods on the fly.

你错过了Devise正在使用的class_eval调用。这使得他们可以使用字符串插值来动态创建方法。

Try this:

尝试这个:

class Devise

  mapping = "user"

  class_eval <<-METHOD
    def self.current_#{mapping}
      puts "hello"
    end
  METHOD

end

Devise.current_user

#2


1  

Note that the lines you try to use are in the 'class_eval' block. You may just try

请注意,您尝试使用的行位于“class_eval”块中。你可以尝试一下

class Devise
    mapping = "user"

    class_eval <<-METHODS

    def self.current_#{mapping}
        puts "hello"
    end
    def self.puts_current
        puts @current_#{mapping}
    end

    METHODS
end

Devise.current_user

...and this would work. Or read some docs about class_eval for understanding.

......这会奏效。或者阅读一些关于class_eval的文档以便理解。

#1


2  

You're missing the class_eval call that Devise is using. This is what allows them to use string interpolation to create methods on the fly.

你错过了Devise正在使用的class_eval调用。这使得他们可以使用字符串插值来动态创建方法。

Try this:

尝试这个:

class Devise

  mapping = "user"

  class_eval <<-METHOD
    def self.current_#{mapping}
      puts "hello"
    end
  METHOD

end

Devise.current_user

#2


1  

Note that the lines you try to use are in the 'class_eval' block. You may just try

请注意,您尝试使用的行位于“class_eval”块中。你可以尝试一下

class Devise
    mapping = "user"

    class_eval <<-METHODS

    def self.current_#{mapping}
        puts "hello"
    end
    def self.puts_current
        puts @current_#{mapping}
    end

    METHODS
end

Devise.current_user

...and this would work. Or read some docs about class_eval for understanding.

......这会奏效。或者阅读一些关于class_eval的文档以便理解。