Class: ViewModel::AccessControl::Composed::ComposedResult

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(allow, veto, allow_error, veto_error) ⇒ ComposedResult

Returns a new instance of ComposedResult.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
# File 'lib/view_model/access_control/composed.rb', line 10

def initialize(allow, veto, allow_error, veto_error)
  raise ArgumentError.new('Non-vetoing result may not have a veto error') if veto_error  && !veto
  raise ArgumentError.new('Allowing result may not have a allow error')   if allow_error && allow

  super
end

Instance Attribute Details

#allowObject

Returns the value of attribute allow

Returns:

  • (Object)

    the current value of allow



9
10
11
# File 'lib/view_model/access_control/composed.rb', line 9

def allow
  @allow
end

#allow_errorObject

Returns the value of attribute allow_error

Returns:

  • (Object)

    the current value of allow_error



9
10
11
# File 'lib/view_model/access_control/composed.rb', line 9

def allow_error
  @allow_error
end

#vetoObject

Returns the value of attribute veto

Returns:

  • (Object)

    the current value of veto



9
10
11
# File 'lib/view_model/access_control/composed.rb', line 9

def veto
  @veto
end

#veto_errorObject

Returns the value of attribute veto_error

Returns:

  • (Object)

    the current value of veto_error



9
10
11
# File 'lib/view_model/access_control/composed.rb', line 9

def veto_error
  @veto_error
end

Instance Method Details

#errorObject



21
22
23
24
25
26
27
# File 'lib/view_model/access_control/composed.rb', line 21

def error
  case
  when veto   then veto_error
  when !allow then allow_error
  else nil
  end
end

#merge(&_block) ⇒ Object

Merge this composed result with another. ‘allow`s widen and `veto`es narrow.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/view_model/access_control/composed.rb', line 30

def merge(&_block)
  if self.veto
    self
  else
    other = yield

    new_allow = self.allow || other.allow

    new_allow_error =
      case
      when new_allow
        nil
      when self.allow_error.nil?
        other.allow_error
      when other.allow_error.nil?
        self.allow_error
      # Mergeable (standard) errors should be merged if possible; if not
      # possible, we should take the first non-standard error.
      when mergeable_error?(self.allow_error)
        if mergeable_error?(other.allow_error)
          self.allow_error.merge(other.allow_error)
        else
          other.allow_error
        end
      else
        self.allow_error
      end

    ComposedResult.new(new_allow, other.veto, new_allow_error, other.veto_error)
  end
end

#permit?Boolean

Returns:

  • (Boolean)


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

def permit?
  !veto && allow
end