Module: Authorization::Identity::UserExtensions::InstanceMethods

Defined in:
lib/publishare/identity.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/publishare/identity.rb', line 22

def method_missing( method_sym, *args )
  method_name = method_sym.to_s
  authorizable_object = args.empty? ? nil : args[0]

  base_regex = "is_(\\w+)"
  fancy_regex = base_regex + "_(#{Authorization::Base::VALID_PREPOSITIONS_PATTERN})"
  is_either_regex = '^((' + fancy_regex + ')|(' + base_regex + '))'
  base_not_regex = "is_no[t]?_(\\w+)"
  fancy_not_regex = base_not_regex + "_(#{Authorization::Base::VALID_PREPOSITIONS_PATTERN})"
  is_not_either_regex = '^((' + fancy_not_regex + ')|(' + base_not_regex + '))'

  if method_name =~ Regexp.new(is_either_regex + '_what$')
    role_name = $3 || $6
    has_role_for_objects(role_name, authorizable_object)
  elsif method_name =~ Regexp.new(is_not_either_regex + '\?$')
    role_name = $3 || $6
    not is_role?( role_name, authorizable_object )
  elsif method_name =~ Regexp.new(is_either_regex + '\?$')
    role_name = $3 || $6
    is_role?( role_name, authorizable_object )
  elsif method_name =~ Regexp.new(is_not_either_regex + '$')
    role_name = $3 || $6
    is_no_role( role_name, authorizable_object )
  elsif method_name =~ Regexp.new(is_either_regex + '$')
    role_name = $3 || $6
    is_role( role_name, authorizable_object )
  else
    super
  end
end