繁体   English   中英

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

[英]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.

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