[英]Is there a way to information out of this block
所以我有我的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本身中提供了一種機制來訪問以前存儲的信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.