所以我有我的Rails Controller
class SomeController < ApplicationController
include Something
...
require_information do
more_infomation "Stuff" do
data :stuff_i_want_to_access, [:index]
data :more_stuff_i_want_to_access, [:edit]
data :more_stuff_i_want_to_access, [:show]
end
end
的require_information
DSL是我们创建具有在应用一些自定义的权限,我的东西,但问题是我需要访问的数据从别的地方。有没有一种方法可以从另一个位置访问这3个数据元素。我试过了
SomeController.class_variables
=> []
SomeController.require_information
=> nil
SomeController.require_information2
NoMethodError: undefined method `require_permissionsd' for SomeController:Class
如果有可能的话任何想法
更新:我不认为DSL很重要,因为它只处理权限,但这是DSL
module Something
extend ActiveSupport::Concern
included do
class_attribute :data_information
prepend_before_filter :verify_access
end
module ClassMethods
def require_information &block
self.data_information = []
self.instance_eval &block if block_given?
end
def more_infomation *name, &block
@data = []
self.instance_eval &block if block_given?
self.data_information << { name: name, data: @data.dup }
@data.clear
end
def data *action, &block
@data.push(action)
end
end
def verify_access
# Do stuff
# I have access to data_information by self.class.data_information
end
end
您可以通过以下方式访问数据:
SomeController.data_information
这将为您提供:
[{:name=>["Stuff"],
:data=>
[[:stuff_i_want_to_access, [:index]],
[:more_stuff_i_want_to_access, [:edit]],
[:more_stuff_i_want_to_access, [:show]]]}]
共享DSL实现非常重要,因为DSL实现告诉您信息存储在何处,其他人也可以看到它。您不需要共享DSL实现的唯一方法是,如果DSL在DSL本身中提供了一种机制来访问以前存储的信息。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句