#self.included(base) 在 Ruby on Rails 的 Restful 身份验证中做了什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5160780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:33:45  来源:igfitidea点击:

What does #self.included(base) do in Ruby on Rails' Restful Authentication?

ruby-on-railsrestful-authentication

提问by nonopolarity

I thought we would do

我以为我们会做

helper_method :current_user, :logged_in?, :authorized?

to make these controller methods available for use as helper methods in views. But in Restful Authentication's lib/authenticated_system.rb, I see:

使这些控制器方法可用作视图中的辅助方法。但是在 Restful Authentication's 中lib/authenticated_system.rb,我看到:

# Inclusion hook to make #current_user and #logged_in?
# available as ActionView helper methods.
def self.included(base)
  base.send :helper_method, :current_user, :logged_in?, :authorized? if base.respond_to? :helper_method
end

Why is it done this way instead of that single line? Also, I don't see includedbeing called anywhere.

为什么以这种方式而不是单行方式完成?此外,我没有看到included在任何地方被调用。

回答by nathanvda

The self.includedfunction is called when the module is included. It allows methods to be executed in the context of the base (where the module is included).

self.included当包含模块时调用该函数。它允许在基础(包含模块的地方)的上下文中执行方法。

More info: a ruby mixin tutorial.

更多信息:ruby mixin教程

回答by FaaduBaalak

Out of the same reason which Peter has mentioned I would like to add an example so that it's easy for the newbie developers to understand self.included(base)and self.extended(base):

出于与 Peter 提到的相同原因,我想添加一个示例,以便新手开发人员可以轻松理解self.included(base)self.extended(base)

module Module1
 def fun1
    puts "fun1 from Module1"
 end

 def self.included(base)
    def fun2
        puts "fun2 from Module1"
    end
 end

 def self.extended(base)
   def fun3
     puts "fun3 from Module1"
   end
 end

end

module Module2
 def foo
    puts "foo from Module2"
 end

 def self.extended(base)
    def bar
        puts "bar from Module2"
    end
 end
end


class Test
include Module1
extend Module2
 def abc
    puts "abc form Test"
 end
end

class Test2
  extend Module1
end

Test.new.abc #=> abc form Test

Test.new.fun1 #=> fun1 from Module1

Test.new.fun2 #=> fun2 from Module1

Test.foo #=> foo from Module2

Test.bar #=> bar from Module2

Test.new.fun3 #=> NoMethodError(undefined method `fun3' ..)

Test2.fun3 #=> fun3 from Module1

Test.new.abc #=> abc 表单测试

Test.new.fun1 #=> 来自 Module1 的 fun1

Test.new.fun2 #=> 来自 Module1 的 fun2

Test.foo #=> 来自 Module2 的 foo

Test.bar #=> 来自 Module2 的 bar

Test.new.fun3 #=> NoMethodError(未定义的方法`fun3' ..)

Test2.fun3 #=> 来自 Module1 的 fun3

extend : methods will be accessible as class methods

扩展:方法可以作为类方法访问

include : methods will be available as instance methods

include :方法将作为实例方法可用

"base" in self.extended(base) / self.included(base) :

self.extended( base) / self.included( base) 中的“ base” :

The base parameter in the static extended method will be either an instance object or class object of the class that extended the module depending whether you extend a object or class, respectively.

静态扩展方法中的基本参数将是扩展模块的类的实例对象或类对象,具体取决于您是扩展对象还是类。

When a class includes a module the module's self.included method will be invoked. The base parameter will be a class object for the class that includes the module.

当一个类包含一个模块时,模块的 self.included 方法将被调用。基本参数将是包含模块的类的类对象。

回答by Ryan Bigg

When the AuthenticatedSystemmethod is included using the includemethod, the self.includedmethod is triggered with whatever it was included into being the argument of base.

AuthenticatedSystem使用该include方法包含该方法时,该self.included方法将被包含为 的参数的任何内容触发base

The code you've shown calls helper_methodand defines some helpful helpers, but only if the basehas a helper_methodmethod.

您显示的代码调用helper_method并定义了一些有用的帮助程序,但前提是base具有helper_method方法。

It's done that way so including the module can set up the helper methods as well as adding additional methods to the class.

这样做是为了包含模块可以设置辅助方法以及向类添加其他方法。

回答by Peter Piper

As it is the first result when searching Google for "self.included(base)" I will try to give a small example on how it works. I don't know how much it differs from the restful-authentication-approach.

由于这是在 Google 中搜索“self.included(base)”时的第一个结果,我将尝试举一个小例子来说明它是如何工作的。我不知道它与 restful-authentication-approach 有多少不同。

It is basically used to make methods from one module available in another module.

它基本上用于使一个模块中的方法在另一个模块中可用。

module One
  def hello
    puts 'hello from module One'
  end
end

module Two
  def self.included(base)
    base.class_eval do
      include One
    end
  end
end

class ExampleClass
  include Two
end

ExampleClass.new.hello # => hello from module One

回答by Lane

Want to digger into self.includedand self.extended?

想要深入了解self.includedself.extended

Please look at here: https://ruby-doc.org/core-2.2.1/Module.html#method-i-included

请看这里:https: //ruby-doc.org/core-2.2.1/Module.html#method-i-included