Module: Thwart::Role

Included in:
DefaultRole
Defined in:
lib/thwart/role.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_responseObject

Returns the value of attribute default_response.



4
5
6
# File 'lib/thwart/role.rb', line 4

def default_response
  @default_response
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/thwart/role.rb', line 4

def name
  @name
end

#responsesObject

Returns the value of attribute responses.



4
5
6
# File 'lib/thwart/role.rb', line 4

def responses
  @responses
end

Instance Method Details

#action_response(action) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/thwart/role.rb', line 51

def action_response(action)
  # Return the action level boolean, proc, or nil if it exists is the responses array
  response = self.responses[action]
  if self.responses.has_key?(action) && (response.is_a?(TrueClass) || response.is_a?(FalseClass) || response.nil? || response.respond_to?(:call))
    return found!(response)
  end
  nil
end

#find_resource_name(resource) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# File 'lib/thwart/role.rb', line 60

def find_resource_name(resource)
  return resource if resource.is_a?(Symbol)
  r ||= resource.thwart_name if resource.respond_to?(:thwart_name)
  if resource.class != Class
    r ||= resource.class.thwart_name if resource.class.respond_to?(:thwart_name)
    r ||= resource.class.name.downcase if Thwart.all_classes_are_resources
  end
  r = r.to_sym if r.respond_to?(:to_sym)
  r
end

#parentsObject



11
12
13
14
# File 'lib/thwart/role.rb', line 11

def parents
  @parents ||= []
  @parents
end

#parents=(p) ⇒ Object



16
17
18
19
# File 'lib/thwart/role.rb', line 16

def parents=(p)
  @parents = p.uniq
  @parents
end

#query(actor, resource, action) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/thwart/role.rb', line 21

def query(actor, resource, action) 
  @query_result_found = false
  resp = nil
  if self.responses.has_key?(action)
    # Find the resource scope response if it exists {:view => {:foo => bool}}
    resp = self.resource_response(self.responses[action], self.find_resource_name(resource)) if !found? 
    # Find the action scope response if it exists {:view => bool}
    resp = self.action_response(action) if !found?
  end
  
  # Return the default if it exists
  resp = found!(self.default_response) if !found? && !self.default_response.nil?
  # Call it if it is a proc
  resp = resp.call(actor, resource, action) if resp.respond_to?(:call) 

  resp
end

#resource_response(resources, name) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/thwart/role.rb', line 39

def resource_response(resources, name)
  # Return the resource scoped response if it exists
  if resources.respond_to?(:[]) && resources.respond_to?(:include?) 
    if resources.include?(name)
      return found!(resources[name])
    elsif resources.include?(:_other)
      return found!(resources[:_other])
    end
  end
  nil
end