Class: Permissable::PermissionsCache

Inherits:
Object
  • Object
show all
Defined in:
lib/permissable/permissions_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(permission_list) ⇒ PermissionsCache

Returns a new instance of PermissionsCache.



8
9
10
11
# File 'lib/permissable/permissions_cache.rb', line 8

def initialize(permission_list)
  @_permissions   = permission_list
  @_query_results = nil
end

Instance Attribute Details

#_permissionsObject

Returns the value of attribute _permissions.



5
6
7
# File 'lib/permissable/permissions_cache.rb', line 5

def _permissions
  @_permissions
end

#_query_resultsObject

Returns the value of attribute _query_results.



6
7
8
# File 'lib/permissable/permissions_cache.rb', line 6

def _query_results
  @_query_results
end

Instance Method Details

#allObject



44
45
46
47
48
# File 'lib/permissable/permissions_cache.rb', line 44

def all
  results = @_query_results || []
  @_query_results = nil
  results
end

#exists?Boolean

Returns:

  • (Boolean)


50
51
52
53
54
# File 'lib/permissable/permissions_cache.rb', line 50

def exists?
  results = @_query_results || []
  @_query_results = nil
  !results.empty?
end

#for_member(member) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/permissable/permissions_cache.rb', line 13

def for_member(member)
  arr = create_result_array
  @_query_results = arr.find_all do |perm|  
    (member.key?(:member_type) ? true : perm.member_type == member[:member_type]) && [member[:member_id]].flatten.uniq.include?(perm.member_id)
  end
  
  return self
  
end

#for_resource(resource) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/permissable/permissions_cache.rb', line 23

def for_resource(resource)
  arr = create_result_array
  resource = Permissable.flatten_resource(resource)
  @_query_results = arr.find_all do |perm|
    [resource[:resource_id]].flatten.include?(perm.resource_id) && [resource[:resource_type]].flatten.include?(perm.resource_type)
  end
  
  return self
  
end

#with_permission_to(methods) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/permissable/permissions_cache.rb', line 34

def with_permission_to(methods)
  arr = create_result_array      
  @_query_results = arr.find_all do |perm|
    [methods].flatten.collect{ |m| m.to_s }.include?(perm.permission_type)
  end
  
  return self
  
end