Class: NoPassword::Link::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Attributes, ActiveModel::Model
Defined in:
lib/nopassword/link/base.rb

Overview

Base class for session-stored objects in the Link authentication flow. Provides ActiveModel compatibility and session persistence.

Direct Known Subclasses

Challenge

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session, **attributes) ⇒ Base

Returns a new instance of Base.



9
10
11
12
13
# File 'lib/nopassword/link/base.rb', line 9

def initialize(session, **attributes)
  @session = session
  data = @session[session_key] || {}
  super(**data.symbolize_keys.merge(attributes))
end

Class Method Details

.delete(session) ⇒ Object



43
44
45
46
# File 'lib/nopassword/link/base.rb', line 43

def self.delete(session)
  key = "nopassword_#{name.demodulize.underscore}".to_sym
  session.delete(key)
end

.update(session) ⇒ Object

Update session data, optionally yielding for modifications



32
33
34
35
36
37
# File 'lib/nopassword/link/base.rb', line 32

def self.update(session, **)
  new(session, **).tap do |instance|
    yield instance if block_given?
    instance.save_without_validation
  end
end

Instance Method Details

#deleteObject



39
40
41
# File 'lib/nopassword/link/base.rb', line 39

def delete
  @session.delete(session_key)
end

#persisted?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/nopassword/link/base.rb', line 48

def persisted?
  false
end

#saveObject



15
16
17
18
19
# File 'lib/nopassword/link/base.rb', line 15

def save
  return false unless valid?
  @session[session_key] = attributes.compact
  true
end

#save!Object



21
22
23
# File 'lib/nopassword/link/base.rb', line 21

def save!
  save || raise(ActiveModel::ValidationError.new(self))
end

#save_without_validationObject

Save without running validations - useful for partial updates



26
27
28
29
# File 'lib/nopassword/link/base.rb', line 26

def save_without_validation
  @session[session_key] = attributes.compact
  true
end