Class: Hubbado::Policy::RspecMatchers::Permit::PermitMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/hubbado/policy/rspec_matchers/permit.rb

Instance Method Summary collapse

Constructor Details

#initialize(action, *args, **kwargs) ⇒ PermitMatcher

Returns a new instance of PermitMatcher.



10
11
12
13
14
# File 'lib/hubbado/policy/rspec_matchers/permit.rb', line 10

def initialize(action, *args, **kwargs)
  @action = action
  @args = args
  @kwargs = kwargs
end

Instance Method Details

#descriptionObject



29
30
31
# File 'lib/hubbado/policy/rspec_matchers/permit.rb', line 29

def description
  "#{@policy.class.name} permit #{@action}"
end

#failure_messageObject



33
34
35
36
# File 'lib/hubbado/policy/rspec_matchers/permit.rb', line 33

def failure_message
  "Expected #{@policy.class.name} to permit #{@action}, " \
    "but it was revoked with #{@reason ? @result.reason : 'no reason'}"
end

#failure_message_when_negatedObject



38
39
40
# File 'lib/hubbado/policy/rspec_matchers/permit.rb', line 38

def failure_message_when_negated
  "Expected #{@policy.class.name} to deny #{@action}, but it was permitted"
end

#matches?(policy) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/hubbado/policy/rspec_matchers/permit.rb', line 16

def matches?(policy)
  @policy = policy

  if policy.respond_to?(@action)
    @result = policy.send @action, *@args, **@kwargs

    @result.permitted?
  else
    # TODO: Get rid of this branch once the Navigation policy uses the DSL
    policy.send "#{@action}?", *@args, **@kwargs
  end
end