Class: Restrict::Restriction

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Restriction

Returns a new instance of Restriction.



5
6
7
8
9
10
# File 'lib/restrict/restriction.rb', line 5

def initialize(*args)
  @options  = args.extract_options!
  @unless   = @options[:unless]
  @on       = @options[:on] || options[:of] || options[:object]
  @actions  = args
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



3
4
5
# File 'lib/restrict/restriction.rb', line 3

def actions
  @actions
end

#onObject

Returns the value of attribute on.



3
4
5
# File 'lib/restrict/restriction.rb', line 3

def on
  @on
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/restrict/restriction.rb', line 3

def options
  @options
end

#unlessObject

Returns the value of attribute unless.



3
4
5
# File 'lib/restrict/restriction.rb', line 3

def unless
  @unless
end

Instance Method Details

#applies_to?(action) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/restrict/restriction.rb', line 12

def applies_to?(action)
  applies_to_action?(action) || applies_to_all_actions?
end

#validate(controller) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/restrict/restriction.rb', line 16

def validate(controller)
  @unless or return

  unless_args = []
  if @on
    object = controller.__send__(on)
    unless_args << object or raise Restrict::AccessDenied, reason: 'object given was #{object.inspect}'
  end

  unless controller.__send__(@unless, *unless_args)
    raise Restrict::AccessDenied, reason: self
  end
end