Module: EnsureMagic

Included in:
Guardian
Defined in:
lib/guardian/ensure_magic.rb

Overview

Support for ensure_blah! methods.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/guardian/ensure_magic.rb', line 5

def method_missing(method, *args, &block)
  if method.to_s =~ /\Aensure_(.*)\!\z/
    can_method = :"#{Regexp.last_match[1]}?"

    if respond_to?(can_method)
      unless send(can_method, *args, &block)
        raise Discourse::InvalidAccess.new("#{can_method} failed")
      end
      return
    end
  end

  super.method_missing(method, *args, &block)
end

Instance Method Details

#ensure_can_see!(obj) ⇒ Object

Make sure we can see the object. Will raise a NotFound if it’s nil



21
22
23
# File 'lib/guardian/ensure_magic.rb', line 21

def ensure_can_see!(obj)
  raise Discourse::InvalidAccess.new("Can't see #{obj}") unless can_see?(obj)
end