Class: Passwordless::Constraint

Inherits:
Object
  • Object
show all
Includes:
ControllerHelpers
Defined in:
lib/passwordless/constraint.rb

Overview

A class the constraint routes to authenticated records

Direct Known Subclasses

ConstraintNot

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ControllerHelpers

#authenticate_by_session, #build_passwordless_session, #create_passwordless_session, #create_passwordless_session!, #find_passwordless_session_for, #redirect_session_key, #reset_passwordless_redirect_location!, #save_passwordless_redirect_location!, #session_key, #sign_in, #sign_out

Constructor Details

#initialize(authenticatable_type, **options) ⇒ Constraint

Returns a new instance of Constraint.

Parameters:

  • authenticatable_type (Class)

    Authenticatable class

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :if (Proc)

    A lambda that takes an authenticatable and returns a boolean



14
15
16
17
18
# File 'lib/passwordless/constraint.rb', line 14

def initialize(authenticatable_type, **options)
  @authenticatable_type = authenticatable_type
  # `if' is a keyword but so we do this instead of keyword arguments
  @predicate = options.fetch(:if) { -> (_) { true } }
end

Instance Attribute Details

#authenticatable_typeObject (readonly)

Returns the value of attribute authenticatable_type.



10
11
12
# File 'lib/passwordless/constraint.rb', line 10

def authenticatable_type
  @authenticatable_type
end

#predicateObject (readonly)

Returns the value of attribute predicate.



10
11
12
# File 'lib/passwordless/constraint.rb', line 10

def predicate
  @predicate
end

#sessionObject (readonly)

Returns the value of attribute session.



10
11
12
# File 'lib/passwordless/constraint.rb', line 10

def session
  @session
end

Instance Method Details

#matches?(request) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
# File 'lib/passwordless/constraint.rb', line 20

def matches?(request)
  # used in authenticate_by_session
  @session = request.session
  authenticatable = authenticate_by_session(authenticatable_type)
  !!(authenticatable && predicate.call(authenticatable))
end