Module: RequirementAuthorization::ClassMethods

Defined in:
lib/requirement_authorization.rb

Instance Method Summary collapse

Instance Method Details

#requirement(requirement, opts = {}, &block) ⇒ Object

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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# 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