Class: ViewModel::AccessControl::Result

Inherits:
Struct
  • Object
show all
Defined in:
lib/view_model/access_control.rb

Constant Summary collapse

PERMIT =
Result.new(true).freeze
DENY =
Result.new(false).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(permit, error: nil) ⇒ Result

Returns a new instance of Result.

Raises:

  • (ArgumentError)


15
16
17
18
19
# File 'lib/view_model/access_control.rb', line 15

def initialize(permit, error: nil)
  raise ArgumentError.new('Successful AccessControl::Result may not have an error') if permit && error

  super(permit, error)
end

Instance Attribute Details

#errorObject

Returns the value of attribute error

Returns:

  • (Object)

    the current value of error



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

def error
  @error
end

#permitObject Also known as: permit?

Returns the value of attribute permit

Returns:

  • (Object)

    the current value of permit



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

def permit
  @permit
end

Instance Method Details

#merge(&_block) ⇒ Object

Merge this result with another access control result. Takes a block returning a result, and returns a combined result for both tests. Access is permitted if both results permit. Otherwise, access is denied with the error value of the first denying Result.



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

def merge(&_block)
  if permit?
    yield
  else
    self
  end
end