Class: Permission::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/permission/base.rb

Constant Summary collapse

DEFAULT_FINDERS =
/^((find|with).*|first|last|all|children|paginate|scoped|count)$/
DEFAULT_SEARCHES =
/^search_.*/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Base

Returns a new instance of Base.



18
19
20
21
# File 'lib/permission/base.rb', line 18

def initialize(params)
  @user = params[:user]
  @object = params[:object]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



23
24
25
# File 'lib/permission/base.rb', line 23

def method_missing(method, *args)
  object.send(method, *args)
end

Instance Attribute Details

#objectObject

Returns the value of attribute object.



13
14
15
# File 'lib/permission/base.rb', line 13

def object
  @object
end

#userObject

Returns the value of attribute user.



13
14
15
# File 'lib/permission/base.rb', line 13

def user
  @user
end

Instance Method Details

#attributesObject



39
40
41
# File 'lib/permission/base.rb', line 39

def attributes
  object.attributes.reject{|key,value| !can_read?(key) }
end

#can_read?(attr_name) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/permission/base.rb', line 59

def can_read?(attr_name)
  true
end

#can_write?(attr_name) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/permission/base.rb', line 63

def can_write?(attr_name)
  true
end

#read_attribute(attr_name) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/permission/base.rb', line 43

def read_attribute(attr_name)
  if can_read?(attr_name)
    object.read_attribute(attr_name)
  else
    raise PermissionException.new "#{user} does not have permission to access #{attr_name} on #{object}"
  end
end

#update_attribute(name, value) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/permission/base.rb', line 27

def update_attribute(name, value)
  if can_write?(name)
    object.update_attribute(name, value)
  else
    raise PermissionException.new "#{user} does not have permission to access #{name} on #{object}"
  end
end

#update_attributes(attributes) ⇒ Object



35
36
37
# File 'lib/permission/base.rb', line 35

def update_attributes(attributes)
  object.update_attributes(attributes.reject{|key,value| !can_write?(key) })
end

#write_attribute(attr_name, value) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/permission/base.rb', line 51

def write_attribute(attr_name, value)
  if can_write?(attr_name)
    object.write_attribute(attr_name, value)
  else
    raise PermissionException.new "#{user} does not have permission to access #{attr_name} on #{object}"
  end
end