我在mixin模块中有一个恒定范围的问题。假设我有类似的东西
module Auth
USER_KEY = "user" unless defined? USER_KEY
def authorize
user_id = session[USER_KEY]
def
end
USER_KEY常量应默认为“user”,除非已定义。现在我可以将它混合到几个地方,但在其中一个地方USER_KEY需要不同,所以我们可能会有这样的东西
class ApplicationController < ActionController::Base
USER_KEY = "my_user"
include Auth
def test_auth
authorize
end
end
我希望USER_KEY在授权中使用时会是“my_user”,因为它已经定义了,但它仍然是“user”,取自USER_KEY的模块定义。任何人都知道如何授权使用USER_KEY的类版本?
答案 0 :(得分:57)
USER_KEY
中您声明的Auth
(甚至是有条件的)全球称为Auth::USER_KEY
。虽然包含模块可以以非完全合格的方式引用密钥,但它并没有“混入”包含模块。
如果您希望每个包含模块(例如ApplicationController
)能够定义自己的USER_KEY
,请尝试以下操作:
module Auth
DEFAULT_USER_KEY = 'user'
def self.included(base)
unless base.const_defined?(:USER_KEY)
base.const_set :USER_KEY, Auth::DEFAULT_USER_KEY
end
end
def authorize
user_id = session[self.class.const_get(:USER_KEY)]
end
end
class ApplicationController < ActionController::Base
USER_KEY = 'my_user'
include Auth
end
但是,如果你要解决所有这些问题,你可以将其作为一种类方法:
module Auth
DEFAULT_USER_KEY = 'user'
def self.included(base)
base.extend Auth::ClassMethods
base.send :include, Auth::InstanceMethods
end
module ClassMethods
def user_key
Auth::DEFAULT_USER_KEY
end
end
module InstanceMethods
def authorize
user_id = session[self.class.user_key]
end
end
end
class ApplicationController < ActionController::Base
def self.user_key
'my_user'
end
end
或类级访问者:
module Auth
DEFAULT_USER_KEY = 'user'
def self.included(base)
base.send :attr_accessor :user_key unless base.respond_to?(:user_key=)
base.user_key ||= Auth::DEFAULT_USER_KEY
end
def authorize
user_id = session[self.class.user_key]
end
end
class ApplicationController < ActionController::Base
include Auth
self.user_key = 'my_user'
end
答案 1 :(得分:39)
常量在Ruby中没有全局范围。常量可以在任何范围内显示,但您必须指定常量的位置。当你开始一个新的类,模块或def时,你开始一个新的范围,如果你想要另一个范围的常量,你必须指定在哪里找到它。
X = 0
class C
X = 1
module M
X = 2
class D
X = 3
puts X # => 3
puts C::X # => 1
puts C::M::X # => 2
puts M::X # => 2
puts ::X # => 0
end
end
end
答案 2 :(得分:12)
这是一个简单的解决方案。
的变化:
USER_KEY
。
module Auth
USER_KEY = "user"
def authorize
user_key = self.class.const_defined?(:USER_KEY) ? self.class::USER_KEY : USER_KEY
user_id = session[user_key]
def
end
<强>解释强>
您所看到的行为并非特定于rails,而是由于ruby在未通过::
明确确定范围的情况下查找常量(我在上面称之为“默认”)。使用“当前正在执行的代码的词法范围”查找常量。这意味着ruby首先在执行代码的模块(或类)中查找常量,然后向外移动到每个连续的封闭模块(或类),直到找到在该范围上定义的常量。
在您的控制器中,您拨打authorize
。但是当authorize
正在执行时,当前正在执行的代码位于Auth
中。这就是查找常量的地方。如果Auth没有USER_KEY
,但封闭模块有,那么将使用封闭的模块。例如:
module Outer
USER_KEY = 'outer_key'
module Auth
# code here can access USER_KEY without specifying "Outer::"
# ...
end
end
这种情况的一个特例是顶级执行环境,它被视为属于类Object
。
USER_KEY = 'top-level-key'
module Auth
# code here can access the top-level USER_KEY (which is actually Object::USER_KEY)
# ...
end
使用范围操作符(::
)定义模块或类是一个缺陷:
module Outer
USER_KEY = 'outer_key'
end
module Outer::Auth
# methods here won't be able to use USER_KEY,
# because Outer isn't lexically enclosing Auth.
# ...
end
请注意,可以在定义方法之后定义常量。只有在访问USER_KEY时才会进行查找,因此也可以这样做:
module Auth
# don't define USER_KEY yet
# ...
end
# you can't call authorize here or you'll get an uninitialized constant error
Auth::USER_KEY = 'user'
# now you can call authorize.
答案 3 :(得分:2)
如果你的项目在Rails中,或者至少使用ActiveSupport
模块,你可以大大减少必要的逻辑糖:
module Auth
extend ActiveSupport::Concern
included do
# set a global default value
unless self.const_defined?(:USER_KEY)
self.const_set :USER_KEY, 'module_user'
end
end
end
class ApplicationController < ActionController::Base
# set an application default value
USER_KEY = "default_user"
include Auth
end
class SomeController < ApplicationController
# set a value unique to a specific controller
USER_KEY = "specific_user"
end
我很惊讶没有人建议这种方法,看看OP的场景如何在Rails应用程序中存在......
答案 4 :(得分:0)
There's a far simpler solution to the OP's question than the other answers here reveal:
module Foo
THIS_CONST = 'foo'
def show_const
self.class::THIS_CONST
end
end
class Bar
include Foo
THIS_CONST ='bar'
def test_it
show_const
end
end
class Baz
include Foo
def test_it
show_const
end
end
2.3.1 :004 > r = Bar.new
=> #<Bar:0x000000008be2c8>
2.3.1 :005 > r.test_it
=> "bar"
2.3.1 :006 > z = Baz.new
=> #<Baz:0x000000008658a8>
2.3.1 :007 > z.test_it
=> "foo"
It was @james-a-rosen's answer that gave me the inspiration to try this. I didn't want to go his route because I had several constants that are shared among several classes, each with a different value, and his method looked like a lot of typing.