Class: Aclatraz::Suspect::SemanticRoles::Base

Inherits:
Roles
  • Object
show all
Defined in:
lib/aclatraz/suspect.rb

Direct Known Subclasses

Not, Yes

Constant Summary collapse

ROLE_FORMAT =

Role name can have following formats:

/(_(of|at|on|by|for|in))?(\?|\!)\Z/

Constants inherited from Roles

Roles::ACL_ROLE_SUFFIXES

Instance Attribute Summary

Attributes inherited from Roles

#suspect

Instance Method Summary collapse

Methods inherited from Roles

#all, #assign, #delete, #has?, #initialize

Constructor Details

This class inherits a constructor from Aclatraz::Suspect::Roles

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object

Provides syntactic sugars for checking and assigning roles.

Examples

checking permissions... manager? owner_of?(object) manager_of?(Class) responsible_for?(object)

writing permissions...
responsible_for!(object) manager!

Accepted method names

  • role_name
  • role_name_of
  • role_name_at
  • role_name_by
  • role_name_in
  • role_name_on
  • role_name_for


107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/aclatraz/suspect.rb', line 107

def method_missing(meth, *args, &blk)
  meth = meth.to_s
  if meth =~ ROLE_FORMAT
    write = meth[-1].chr == "!" 
    role  = meth.gsub(ROLE_FORMAT, '')
    args.unshift(role.to_sym)
    write ? writer(*args) : reader(*args, &blk)
  else
    # super doesn't work here, so...
    raise NoMethodError, "undefined local variable or method method `#{meth}' for #{inspect}:#{self.class.name}"
  end
end

Instance Method Details

#reader(*args, &blk) ⇒ Object

Check if specified suspect have assigned given role. If true, then given block will be executed.



72
73
74
75
76
# File 'lib/aclatraz/suspect.rb', line 72

def reader(*args, &blk)
  authorized = has?(*args)
  blk.call if authorized && block_given?
  authorized
end

#writer(*args) ⇒ Object

Assigns given role to specified suspect.



79
80
81
# File 'lib/aclatraz/suspect.rb', line 79

def writer(*args)
  assign(*args)
end