currentable 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 507a8189752407fb3beb69d7ea63a6c39815c6aa
4
- data.tar.gz: d9d9f05faacbe82b0da483f6f2cc3ca8606e90b2
3
+ metadata.gz: 48656fcd0ae564e469b9e469e4de8d4ad7f52ff8
4
+ data.tar.gz: 173ef4c5547b09cb9d9fedc6c833b58cd8a8c170
5
5
  SHA512:
6
- metadata.gz: 5a11ad3bb18de07c1eefb55ce28bea8073a95abd232acc5e7f976435114ce72b8890f34bb30285c223faf93205346d5ec77401658aa1e4691f90eee9bad0984c
7
- data.tar.gz: e827013b5bd1feddbf949f92ed5c0f75c8e194cdc8539c0534c8ae7bc39001d59b7d7a7d9685b50eb80fd1ff2c9d85c83e1b907008f7cccb5fcac19eb6730040
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
- TODO: Write usage instructions here
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
- ## Development
41
+ The cleaner handles registering all classes that need to be cleaned up.
28
42
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
43
+ ### Rails
30
44
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
45
+ Middleware will automatically be added to cleanup before and after each request.
32
46
 
33
- ## Contributing
47
+ ### Sidekiq
34
48
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/currentable.
49
+ Middleware will be automatically be added to cleanup before and after each worker.
@@ -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
@@ -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
@@ -1,9 +1,11 @@
1
- require_relative 'auto_cleaner'
1
+ require_relative 'middleware/rails/cleaner'
2
2
 
3
3
  module Currentable
4
- class Railtie < Rails::Railtie
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::AutoCleaner
8
+ app.middleware.insert_after ActionDispatch::RemoteIp, Currentable::Middleware::Rails::Cleaner
7
9
  end
8
10
  end
9
11
  end
@@ -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
@@ -0,0 +1,7 @@
1
+ require_relative 'middleware/sidekiq/server/cleaner'
2
+
3
+ ::Sidekiq.configure_server do |config|
4
+ config.server_middleware do |chain|
5
+ chain.add Currentable::Middleware::Sidekiq::Server::Cleaner
6
+ end
7
+ end
@@ -1,3 +1,5 @@
1
1
  module Currentable
2
- VERSION = '0.1.0'.freeze
2
+ ##
3
+ # The current version of the gem.
4
+ VERSION = '0.2.0'.freeze
3
5
  end
data/lib/currentable.rb CHANGED
@@ -7,5 +7,11 @@ if defined? ::Rails
7
7
  require 'currentable/railtie'
8
8
  end
9
9
 
10
+ if defined? ::Sidekiq
11
+ require 'currentable/sidetie'
12
+ end
13
+
14
+ ##
15
+ # Adds a .current and .current= method to objects for thread storeage.
10
16
  module Currentable
11
17
  end
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.1.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-07 00:00:00.000000000 Z
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: []
@@ -1,15 +0,0 @@
1
- require_relative 'cleaner'
2
-
3
- module Currentable
4
- class AutoCleaner
5
- def initialize(app)
6
- @app = app
7
- end
8
-
9
- def call(*args)
10
- @app.call(*args)
11
- ensure
12
- Currentable::Cleaner.clean_all
13
- end
14
- end
15
- end