有没有办法从这个区块中获取信息

时间:2014-02-16 14:38:59

标签: ruby-on-rails ruby ruby-on-rails-3 block

所以我有我的rails控制器

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

1 个答案:

答案 0 :(得分:1)

您可以使用以下方式访问数据:

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本身提供了一种机制来访问先前存储的信息。