module RequirementAuthorization::ClassMethods

Public Instance Methods

requirement(requirement, opts={}, &block) click to toggle source

Setups a hash table of requirements that may be used by class methods from other sub-classed controllers

# File lib/requirement_authorization.rb, line 81
def requirement(requirement, opts={}, &block)
  self.requirements.merge! requirement.to_s => Requirement.new(opts, &block)

  # Build out the class method for this requirement. This is primarly used towards the
  # top of a controller.
  instance_eval %{
    def #{requirement}#{METHOD_SUFIX}(*args)
      requirements['#{requirement}'].filter(self, *args)
    end}

  # Build out the instance method so that this requirement can be called from other
  # instance methods. This proves to be insanely useful for composing requirements
  # together or reusing them from other methods.
  class_eval %{
    def #{requirement}#{METHOD_SUFIX}(*args)
      req = '#{requirement}'
      self.class.send(:requirements).
        fetch(req).
        resolve(self, *args)
    end}
end

Protected Instance Methods

requirements() click to toggle source
# File lib/requirement_authorization.rb, line 104
def requirements
  @@requirements ||= {}
end