简体   繁体   English

有没有办法从这个障碍中获取信息

[英]Is there a way to information out of this block

so i have my rails controller 所以我有我的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

The require_information DSL is something I created for us to have some custom permissions in the application but the problem is I need to access that data from somewhere else. 我为我们创建了require_information DSL,以便在应用程序中具有一些自定义权限,但是问题是我需要从其他地方访问该数据。 Is there a way to access the 3 data elements from another location. 有没有一种方法可以从另一个位置访问这3个数据元素。 I tried 我试过了

SomeController.class_variables
=> []
SomeController.require_information
=> nil
SomeController.require_information2
NoMethodError: undefined method `require_permissionsd' for SomeController:Class

Any ideas if this is even possible 如果有可能的话任何想法

UPDATE: I didnt think the DSL was important because its just dealing with permissions but here is the DSL 更新:我不认为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

You can access the data with: 您可以通过以下方式访问数据:

SomeController.data_information

which will give you: 这将为您提供:

[{:name=>["Stuff"],
  :data=>
   [[:stuff_i_want_to_access, [:index]],
    [:more_stuff_i_want_to_access, [:edit]],
    [:more_stuff_i_want_to_access, [:show]]]}]

The DSL implementation was important to share because it's the DSL implementation that tells you where the information is being stored, which is what enables others to see it. 共享DSL实现非常重要,因为DSL实现告诉您信息存储在何处,其他人也可以看到它。 The only way you wouldn't have needed to share the DSL implementation is if the DSL had provided a mechanism in the DSL itself to access the information stored previously. 您不需要共享DSL实现的唯一方法是,如果DSL在DSL本身中提供了一种机制来访问以前存储的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM