current_session 0.1.3 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b1b39dda8338daaeb45ccebe39f01c23931d9387685619750a65c0a1acc7f6cb
4
- data.tar.gz: 8214ca0c3d15c9d8cb27a963ba41e5e7cd71375c1e2d24ea616972b3beeed193
3
+ metadata.gz: 13ac22af37f6aa5545fb02c46c729473b043b67c4073c9b481015c422c7d5927
4
+ data.tar.gz: c2bd6c1da99e69a41ecadf263f2c0219633d45da7cb723476d4fb8904c8344ee
5
5
  SHA512:
6
- metadata.gz: 0e1e74c3e85d3806e3d8235f396f67d5132a7ff3ba49bd384d3a0bb316073ec9d17bf3f546a92754c0beceb43b08740532ec75a4a25a32c8cc9f835fcec29f7f
7
- data.tar.gz: 3c0f7bba4344ab4be82509930173accd0b76712ee07b3dbb8018f788a22304d467c27478aa9ca7691ebecb794f7783cf835cecf8b9ed90f6bcdb005cc8e2df2e
6
+ metadata.gz: 1e193a8e670f0f684dc509193b0d1ada1f092551b312cc3032527520e8c1957c413a2bd77c2637556498b5f898b49974839d4752e1d09792dda68a73e357cc5f
7
+ data.tar.gz: 4cd6ca157c873142fc4c43c9a14c2a16e7f8487364d94c116204e7aca36d9b005c5f05d44e80aa3264885363d08e74271d4d442c786d7359fd44c01ee47225d3
data/.rubocop.yml CHANGED
@@ -32,3 +32,6 @@ Lint/EmptyBlock:
32
32
 
33
33
  RSpec/MultipleExpectations:
34
34
  Enabled: false
35
+
36
+ Style/SignalException:
37
+ EnforcedStyle: semantic
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- current_session (0.1.3)
4
+ current_session (0.1.4)
5
5
  activesupport (>= 6.0.0)
6
6
 
7
7
  GEM
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CurrentSession
4
+ #
5
+ # Base class for providing auth methods
6
+ #
7
+ class AuthMethod
8
+ class_attribute :user_class
9
+
10
+ class FindBy < self
11
+ def call(&block)
12
+ find_by_auth.try(&block)
13
+ end
14
+ end
15
+
16
+ class FindOrCreateBy < self
17
+ def call(&block)
18
+ find_or_create_by_auth.try(&block)
19
+ end
20
+ end
21
+
22
+ def self.new_auth_class(auth_methods_module)
23
+ new_auth_class =
24
+ if auth_methods_module.method_defined?(:find_by_auth)
25
+ Class.new(CurrentSession::AuthMethod::FindBy) { include auth_methods_module }
26
+ elsif auth_methods_module.method_defined?(:find_or_create_by_auth)
27
+ Class.new(CurrentSession::AuthMethod::FindOrCreateBy) { include auth_methods_module }
28
+ else
29
+ fail NotImplementedError, "You must implement find_by_auth or find_or_create_by_auth"
30
+ end
31
+ new_auth_class.user_class = user_class
32
+ new_auth_class
33
+ end
34
+
35
+ def initialize(request)
36
+ @request = request
37
+ end
38
+ attr_reader :request
39
+
40
+ def auth
41
+ request.env["omniauth.auth"]
42
+ end
43
+ end
44
+ end
@@ -6,39 +6,46 @@ module CurrentSession
6
6
  #
7
7
  class Base < ActiveSupport::CurrentAttributes
8
8
  include CurrentSession::Interface
9
- include CurrentSession::RaiseNotImplementedError
10
9
  attribute :current_user
11
10
 
12
11
  class << self
13
- attr_accessor :user_class, :session_token_class
12
+ attr_accessor :session_token_class
13
+ attr_reader :user_class
14
+
15
+ def user_class=(user_class)
16
+ @user_class = user_class
17
+ @auth_class.user_class = user_class if @auth_class
18
+ end
14
19
 
15
20
  def current_time(_)
16
21
  Time.current
17
22
  end
18
23
 
19
- def session_methods=(session_methods_module)
20
- @session_repository_class = Class.new(CurrentSession::Repository) { include session_methods_module }
24
+ def session_methods=(session_methods)
25
+ @session_methods = session_methods
26
+ @session_class = CurrentSession::SessionMethod.new_session_class(session_methods)
21
27
  end
22
28
 
23
29
  def session_methods(&block)
24
30
  if block
25
- session_methods = Module.new(&block)
26
- @session_repository_class = Class.new(CurrentSession::Repository) { include session_methods }
31
+ @session_methods = Module.new(&block)
32
+ @session_class = CurrentSession::SessionMethod.new_session_class(session_methods)
27
33
  else
28
- @session_repository_class
34
+ @session_methods
29
35
  end
30
36
  end
31
37
 
32
38
  def auth_methods=(auth_methods_module)
33
- @auth_class = Class.new(CurrentSession::Auth) { include auth_methods_module }
39
+ @auth_class = CurrentSession::AuthMethod.new_auth_class(auth_methods_module)
34
40
  end
35
41
 
36
42
  def auth_methods(&block)
37
43
  if block
38
- auth_methods = Module.new(&block)
39
- @auth_class = Class.new(CurrentSession::Auth) { include auth_methods }
44
+ @auth_methods = Module.new(&block)
45
+ @auth_class = CurrentSession::AuthMethod.new_auth_class(auth_methods)
46
+ @auth_class.user_class = user_class
40
47
  else
41
- @auth_class
48
+ @auth_methods
42
49
  end
43
50
  end
44
51
  end
@@ -19,8 +19,8 @@ module CurrentSession
19
19
  end
20
20
 
21
21
  def create(request)
22
- auth = @auth_class.new(request, user_class)
23
- auth.find_or_create_by_auth.try do |user|
22
+ auth = @auth_class.new(request)
23
+ auth.call do |user|
24
24
  auth.update(user)
25
25
  session_repository(request).update_session_token(user)
26
26
  end
@@ -37,7 +37,7 @@ module CurrentSession
37
37
  private
38
38
 
39
39
  def session_repository(request)
40
- @session_repository_class.new(
40
+ @session_class.new(
41
41
  request: request,
42
42
  user_class: user_class,
43
43
  session_token_class: session_token_class,
@@ -4,7 +4,11 @@ module CurrentSession
4
4
  #
5
5
  # Base class for processing to get session_token from request.session
6
6
  #
7
- class Repository
7
+ class SessionMethod
8
+ def self.new_session_class(session_methods)
9
+ Class.new(CurrentSession::SessionMethod) { include session_methods }
10
+ end
11
+
8
12
  def initialize(current_time:, request:, user_class:, session_token_class:)
9
13
  @current_time = current_time
10
14
  @request = request
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CurrentSession
4
- VERSION = "0.1.3"
4
+ VERSION = "0.1.5"
5
5
  end
@@ -10,9 +10,8 @@ module CurrentSession
10
10
  extend ActiveSupport::Autoload
11
11
  autoload :Base
12
12
  autoload :Interface
13
- autoload :Auth
14
- autoload :Repository
15
- autoload :RaiseNotImplementedError
13
+ autoload :AuthMethod
14
+ autoload :SessionMethod
16
15
  autoload :SessionMethods
17
16
 
18
17
  def self.key(user_class)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: current_session
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masa (Aileron inc)
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-09-30 00:00:00.000000000 Z
11
+ date: 2022-10-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -146,11 +146,10 @@ files:
146
146
  - bin/setup
147
147
  - current_session.gemspec
148
148
  - lib/current_session.rb
149
- - lib/current_session/auth.rb
149
+ - lib/current_session/auth_method.rb
150
150
  - lib/current_session/base.rb
151
151
  - lib/current_session/interface.rb
152
- - lib/current_session/raise_not_implemented_error.rb
153
- - lib/current_session/repository.rb
152
+ - lib/current_session/session_method.rb
154
153
  - lib/current_session/session_methods.rb
155
154
  - lib/current_session/session_methods/active_record_session.rb
156
155
  - lib/current_session/session_methods/env_session.rb
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module CurrentSession
4
- #
5
- # Base class for providing auth methods
6
- #
7
- class Auth
8
- def initialize(request, user_class)
9
- @request = request
10
- @user_class = user_class
11
- end
12
- attr_reader :request, :user_class
13
-
14
- def auth
15
- request.env["omniauth.auth"]
16
- end
17
- end
18
- end
@@ -1,36 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module CurrentSession
4
- #
5
- # Class for verifying whether it is implemented correctly
6
- #
7
- module RaiseNotImplementedError
8
- extend ActiveSupport::Concern
9
- class_methods do
10
- def raise_not_implemented_error
11
- raise_not_implemented_error_for_repository
12
- raise_not_implemented_error_for_auth
13
- end
14
-
15
- private
16
-
17
- # rubocop:disable Layout/LineLength
18
- def raise_not_implemented_error_for_repository
19
- raise NotImplementedError, "You must setting self.session_repository_class= or session_methods" unless @session_repository_class
20
- raise NotImplementedError, "You must implement #{@session_repository_class}#find" unless @session_repository_class.method_defined? :find
21
- raise NotImplementedError, "You must implement #{@session_repository_class}#destroy" unless @session_repository_class.method_defined? :destroy
22
- raise NotImplementedError, "You must implement #{@session_repository_class}#destroy" unless @session_repository_class.method_defined? :destroy
23
- raise NotImplementedError, "You must implement #{@session_repository_class}#create" unless @session_repository_class.method_defined? :create
24
- end
25
- # rubocop:enable Layout/LineLength
26
-
27
- # rubocop:disable Layout/LineLength
28
- def raise_not_implemented_error_for_auth
29
- raise NotImplementedError, "You must setting self.auth_class= or auth_methods" unless @auth_class
30
- raise NotImplementedError, "You must implement #{@auth_class}#find_or_create_by_auth" unless @auth_class.method_defined? :find_or_create_by_auth
31
- raise NotImplementedError, "You must implement #{@auth_class}#update" unless @auth_class.method_defined? :update
32
- end
33
- # rubocop:enable Layout/LineLength
34
- end
35
- end
36
- end