Class: Permissions

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

Defined Under Namespace

Modules: Authorizable

Constant Summary collapse

VERSION =
'0.1.3'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(permissions = {}) ⇒ Permissions

Returns a new instance of Permissions.



16
17
18
19
20
21
22
23
24
# File 'lib/permissions.rb', line 16

def initialize(permissions = {})
  @permissions = permissions

  if block_given?
    @default = Proc.new
  else
    @default = proc { false }
  end
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



14
15
16
# File 'lib/permissions.rb', line 14

def default
  @default
end

#permissionsObject (readonly)

Returns the value of attribute permissions.



14
15
16
# File 'lib/permissions.rb', line 14

def permissions
  @permissions
end

Instance Method Details

#authorize?(key, *args) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/permissions.rb', line 30

def authorize?(key, *args)
  permissions.fetch(key, default).call(*args)
end

#deep_dup(initial_permissions = {}) ⇒ Object



34
35
36
# File 'lib/permissions.rb', line 34

def deep_dup(initial_permissions = {})
  self.class.new(initial_permissions.merge(permissions)) { default }
end

#for(*keys, &block) ⇒ Object



26
27
28
# File 'lib/permissions.rb', line 26

def for(*keys, &block)
  keys.each { |key| permissions[key] = block }
end