currentable 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +20 -6
- data/lib/currentable/cleaner.rb +12 -1
- data/lib/currentable/instance.rb +34 -0
- data/lib/currentable/middleware/rails/cleaner.rb +30 -0
- data/lib/currentable/middleware/sidekiq/server/cleaner.rb +20 -0
- data/lib/currentable/railtie.rb +5 -3
- data/lib/currentable/registry.rb +20 -0
- data/lib/currentable/sidetie.rb +7 -0
- data/lib/currentable/version.rb +3 -1
- data/lib/currentable.rb +6 -0
- metadata +5 -3
- data/lib/currentable/auto_cleaner.rb +0 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 48656fcd0ae564e469b9e469e4de8d4ad7f52ff8
|
4
|
+
data.tar.gz: 173ef4c5547b09cb9d9fedc6c833b58cd8a8c170
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 82a5e3af042877a7a6b2783bfa9e5d910a205c65f941e833339f068829c9706d494157c1ae50286fa7e63cfd23b317f1e611867e5d51972435fce00d500b667c
|
7
|
+
data.tar.gz: c59a5cdc87a7752f7dd08dd8bafb5931a6051a6051eaff33ef6b099b1622a92c5029ff8a60b6dce6443f93a7e810eb55f156f640391916367bbd80117fd22925
|
data/README.md
CHANGED
@@ -22,14 +22,28 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
-
|
25
|
+
```ruby
|
26
|
+
class Person < ActiveRecord::Base
|
27
|
+
include Currentable::Instance
|
28
|
+
end
|
29
|
+
|
30
|
+
Person.current = Person.find(1)
|
31
|
+
|
32
|
+
Person.current
|
33
|
+
# => Person(id: 1)
|
34
|
+
|
35
|
+
Currentable::Cleaner.clean_all
|
36
|
+
|
37
|
+
Person.current
|
38
|
+
# => nil
|
39
|
+
```
|
26
40
|
|
27
|
-
|
41
|
+
The cleaner handles registering all classes that need to be cleaned up.
|
28
42
|
|
29
|
-
|
43
|
+
### Rails
|
30
44
|
|
31
|
-
|
45
|
+
Middleware will automatically be added to cleanup before and after each request.
|
32
46
|
|
33
|
-
|
47
|
+
### Sidekiq
|
34
48
|
|
35
|
-
|
49
|
+
Middleware will be automatically be added to cleanup before and after each worker.
|
data/lib/currentable/cleaner.rb
CHANGED
@@ -1,10 +1,21 @@
|
|
1
1
|
module Currentable
|
2
|
+
##
|
3
|
+
# Cleans up all registered current objects.
|
4
|
+
#
|
5
|
+
# @note Will only clean for objects stored on the calling thread.
|
6
|
+
#
|
7
|
+
# @example
|
8
|
+
# Currentable::Cleaner.clean_all
|
2
9
|
class Cleaner
|
3
10
|
class << self
|
11
|
+
##
|
12
|
+
# Sets all registered {Currentable::Instance} classes current value to nil
|
13
|
+
#
|
14
|
+
# @note Will only clean for objects stored on the calling thread.
|
4
15
|
def clean_all
|
5
16
|
Currentable::Registry.currents.each do |klass|
|
6
17
|
if defined? ::Rails
|
7
|
-
Rails.logger.debug "Cleaning current for #{klass}"
|
18
|
+
::Rails.logger.debug "Cleaning current for #{klass}"
|
8
19
|
end
|
9
20
|
klass.current = nil
|
10
21
|
end
|
data/lib/currentable/instance.rb
CHANGED
@@ -1,7 +1,24 @@
|
|
1
1
|
require_relative 'registry'
|
2
2
|
|
3
3
|
module Currentable
|
4
|
+
##
|
5
|
+
# Add the magic to a class.
|
6
|
+
#
|
7
|
+
# @example
|
8
|
+
# class Person < ActiveRecord::Base
|
9
|
+
# include Currentable::Instance
|
10
|
+
#
|
11
|
+
# # ...
|
12
|
+
#
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# Person.current = Person.first
|
16
|
+
#
|
17
|
+
# Person.current.first_name
|
18
|
+
# # => "Maddie"
|
4
19
|
module Instance
|
20
|
+
##
|
21
|
+
# @private
|
5
22
|
def self.included(klass)
|
6
23
|
super
|
7
24
|
|
@@ -10,15 +27,32 @@ module Currentable
|
|
10
27
|
Currentable::Registry.register_current(klass)
|
11
28
|
end
|
12
29
|
|
30
|
+
##
|
31
|
+
# Methods available on the class
|
13
32
|
module ClassMethods
|
33
|
+
##
|
34
|
+
# Set a current value on the current thread.
|
35
|
+
#
|
36
|
+
# @param current [Object, nil] The value for the current thread.
|
14
37
|
def current=(current)
|
38
|
+
if !current.nil? && !current.is_a?(self)
|
39
|
+
raise ArgumentError, "#{current} (#{current.class.name}) is not an instance of #{name}"
|
40
|
+
end
|
15
41
|
Thread.current[current_identifier] = current
|
16
42
|
end
|
17
43
|
|
44
|
+
##
|
45
|
+
# Get the threads value for the current
|
46
|
+
#
|
47
|
+
# @return [Object, nil] The value stored for the current thread.
|
18
48
|
def current
|
19
49
|
Thread.current[current_identifier]
|
20
50
|
end
|
21
51
|
|
52
|
+
##
|
53
|
+
# @private
|
54
|
+
#
|
55
|
+
# The identifier for the current. Used as the thread hash key.
|
22
56
|
def current_identifier
|
23
57
|
"current.#{name}"
|
24
58
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'currentable/cleaner'
|
2
|
+
|
3
|
+
module Currentable
|
4
|
+
module Middleware
|
5
|
+
module Rails
|
6
|
+
##
|
7
|
+
# @private
|
8
|
+
#
|
9
|
+
# Rails Middleware
|
10
|
+
class Cleaner
|
11
|
+
##
|
12
|
+
# @private
|
13
|
+
def initialize(app)
|
14
|
+
@app = app
|
15
|
+
end
|
16
|
+
|
17
|
+
##
|
18
|
+
# @private
|
19
|
+
def call(*args)
|
20
|
+
Currentable::Cleaner.clean_all
|
21
|
+
begin
|
22
|
+
@app.call(*args)
|
23
|
+
ensure
|
24
|
+
Currentable::Cleaner.clean_all
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'currentable/cleaner'
|
2
|
+
|
3
|
+
module Currentable
|
4
|
+
module Middleware
|
5
|
+
module Sidekiq
|
6
|
+
module Server
|
7
|
+
class Cleaner
|
8
|
+
def call(_worker, _job, _queue)
|
9
|
+
Currentable::Cleaner.clean_all
|
10
|
+
begin
|
11
|
+
yield
|
12
|
+
ensure
|
13
|
+
Currentable::Cleaner.clean_all
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/currentable/railtie.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
-
require_relative '
|
1
|
+
require_relative 'middleware/rails/cleaner'
|
2
2
|
|
3
3
|
module Currentable
|
4
|
-
|
4
|
+
##
|
5
|
+
# Auto installs the middleware for clearing currents in a Rails application.
|
6
|
+
class Railtie < ::Rails::Railtie
|
5
7
|
initializer 'currentable.install_middleware' do |app|
|
6
|
-
app.middleware.insert_after ActionDispatch::RemoteIp, Currentable::
|
8
|
+
app.middleware.insert_after ActionDispatch::RemoteIp, Currentable::Middleware::Rails::Cleaner
|
7
9
|
end
|
8
10
|
end
|
9
11
|
end
|
data/lib/currentable/registry.rb
CHANGED
@@ -1,14 +1,34 @@
|
|
1
1
|
module Currentable
|
2
|
+
##
|
3
|
+
# @private
|
4
|
+
#
|
5
|
+
# Contains all the classes that are registered as Currentable instances.
|
2
6
|
class Registry
|
7
|
+
##
|
8
|
+
# @private
|
9
|
+
#
|
10
|
+
# Registry lock.
|
3
11
|
MUTEX = Mutex.new
|
4
12
|
|
5
13
|
class << self
|
14
|
+
##
|
15
|
+
# @private
|
16
|
+
#
|
17
|
+
# Adds a class as a current
|
18
|
+
#
|
19
|
+
# @param klass [Currentable::Instance] The class instance to register.
|
6
20
|
def register_current(klass)
|
7
21
|
MUTEX.synchronize do
|
8
22
|
_currents.add(klass)
|
9
23
|
end
|
10
24
|
end
|
11
25
|
|
26
|
+
##
|
27
|
+
# @private
|
28
|
+
#
|
29
|
+
# Return all currents registered.
|
30
|
+
#
|
31
|
+
# @return [Array<Currentable::Instance>]
|
12
32
|
def currents
|
13
33
|
MUTEX.synchronize { _currents }
|
14
34
|
end
|
data/lib/currentable/version.rb
CHANGED
data/lib/currentable.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: currentable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maddie Schipper
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-11-
|
11
|
+
date: 2018-11-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -76,11 +76,13 @@ files:
|
|
76
76
|
- README.md
|
77
77
|
- Rakefile
|
78
78
|
- lib/currentable.rb
|
79
|
-
- lib/currentable/auto_cleaner.rb
|
80
79
|
- lib/currentable/cleaner.rb
|
81
80
|
- lib/currentable/instance.rb
|
81
|
+
- lib/currentable/middleware/rails/cleaner.rb
|
82
|
+
- lib/currentable/middleware/sidekiq/server/cleaner.rb
|
82
83
|
- lib/currentable/railtie.rb
|
83
84
|
- lib/currentable/registry.rb
|
85
|
+
- lib/currentable/sidetie.rb
|
84
86
|
- lib/currentable/version.rb
|
85
87
|
homepage: https://github.com/maddiesch/currentable
|
86
88
|
licenses: []
|