sidekiq-symbols 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 895afbd3618b031afb0839dbc36c10b90a4eb03a
4
+ data.tar.gz: a7df359e07d804c62fc6c7313cb04550dd990bdf
5
+ SHA512:
6
+ metadata.gz: adb7baeb660624f770caedc595e38bcca55a7ed1ffedb076ecf6ca1760d7860ba05a64d5de7eba1734e6239f3d8349f3a98c8af0f916566c76cf799f1b266ac3
7
+ data.tar.gz: b2a086137d07ce88edc86834e4843c6c685c6334b29a1193b841c4b1a90ec690d77e250e70a8e704914645127c7d41038239f999fce19adfab98605eec96489b
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org/"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ (MIT License)
2
+
3
+ Copyright (c) 2014 Adam Prescott
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,72 @@
1
+ [![Build Status](https://travis-ci.org/aprescott/sidekiq-symbols.svg?branch=master)](https://travis-ci.org/aprescott/sidekiq-symbols)
2
+
3
+ sidekiq-symbols gives you symbol keys for your `perform` method.
4
+
5
+ ### Caveats
6
+
7
+ I have not tested this in a production environment! The 0.x version is there for a reason!
8
+
9
+ ### To use
10
+
11
+ Add
12
+
13
+ ```ruby
14
+ include Sidekiq::Symbols
15
+ ```
16
+
17
+ to your Sidekiq job class.
18
+
19
+ ```ruby
20
+ class SomeJob
21
+ include Sidekiq::Worker
22
+ include Sidekiq::Symbols
23
+
24
+ # ...
25
+ end
26
+ ```
27
+
28
+ ### What it does
29
+
30
+ Because Sidekiq round-trips job arguments through JSON serialization-deserialization, a hash argument passed to `perform_async` which uses symbols won't actually be `fetch`able with symbol keys, because
31
+
32
+ ```ruby
33
+ perform_async(x: 1)
34
+ ```
35
+
36
+ leads to when it gets pulled out of Redis.
37
+
38
+ ```ruby
39
+ perform("x" => 1)
40
+ ```
41
+
42
+ sidekiq-symbols forces `perform` to use symbols for all its keys, so that this works:
43
+
44
+ ```ruby
45
+ class SomeJob
46
+ include Sidekiq::Worker
47
+ include Sidekiq::Symbols
48
+
49
+ def perform(arg, opts = {})
50
+ opts[:x] # this works
51
+ end
52
+ end
53
+
54
+ SomeJob.perform_async("foo", x: 1)
55
+ ```
56
+
57
+ Note that `perform_async("foo", "x" => 1)` here would leave `opts["x"] == nil` since you _must_ use `opts[:x]`.
58
+
59
+ ### Keyword arguments
60
+
61
+ Ruby's keyword arguments are essentially an extension of using a symbol-keyed hash argument, so sidekiq-symbols also enables keyword arguments:
62
+
63
+ ```ruby
64
+ class SomeJob
65
+ include Sidekiq::Worker
66
+ include Sidekiq::Symbols
67
+
68
+ def perform(x: 1, y: 2)
69
+ # x and y are availalbe
70
+ end
71
+ end
72
+ ```
@@ -0,0 +1 @@
1
+ require "sidekiq/symbols"
@@ -0,0 +1,33 @@
1
+ require "sidekiq"
2
+
3
+ module Sidekiq
4
+ module Symbols
5
+ def self.included(klass)
6
+ klass.class_eval { prepend(Symbolizer) }
7
+ end
8
+
9
+ module Symbolizer
10
+ def perform(*args)
11
+ symbolized_args = args.map do |arg|
12
+ if arg.is_a?(Hash)
13
+ __sidekiq_symbols_symbolize_keys(arg)
14
+ else
15
+ arg
16
+ end
17
+ end.to_a
18
+ super(*symbolized_args)
19
+ end
20
+
21
+ private
22
+
23
+ def __sidekiq_symbols_symbolize_keys(arg)
24
+ h = {}
25
+ arg.each do |k, v|
26
+ k = k.to_sym if k.respond_to?(:to_sym)
27
+ h[k] = v.is_a?(Hash) ? __sidekiq_symbols_symbolize_keys(v) : v
28
+ end
29
+ h
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,18 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "sidekiq-symbols"
3
+ s.version = "0.1.1"
4
+ s.authors = ["Adam Prescott"]
5
+ s.email = ["adam@aprescott.com"]
6
+ s.homepage = "https://github.com/aprescott/sidekiq-symbols"
7
+ s.summary = "Symbolize your Sidekiq jobs."
8
+ s.description = "Symbolize your Sidekiq jobs."
9
+ s.files = Dir["{lib/**/*,spec/**/*}"] + %w[sidekiq-symbols.gemspec LICENSE Gemfile README.md]
10
+ s.require_path = "lib"
11
+ s.test_files = Dir["spec/*"]
12
+ s.required_ruby_version = ">= 2.0.0"
13
+ s.licenses = ["MIT"]
14
+
15
+ s.add_dependency("sidekiq")
16
+ s.add_development_dependency("rspec", ">= 3.0")
17
+ s.add_development_dependency("pry-byebug")
18
+ end
@@ -0,0 +1,31 @@
1
+ require "rspec/core/formatters/progress_formatter"
2
+
3
+ class ReRunFriendlyFormatter < RSpec::Core::Formatters::ProgressFormatter
4
+ RSpec::Core::Formatters.register self, :dump_summary
5
+
6
+ def dump_summary(summary)
7
+ super
8
+
9
+ failed_files = summary.failed_examples.map { |e| RSpec::Core::Metadata::relative_path(e.file_path) }.uniq
10
+
11
+ return if summary.failed_examples.empty? || failed_files.length > 10
12
+
13
+ output.puts
14
+ output.puts "Rerun all failed examples:"
15
+ output.puts
16
+
17
+ output.puts failure_colored("rspec #{summary.failed_examples.map { |e| RSpec::Core::Metadata::relative_path(e.location) }.join(" ")}")
18
+
19
+ output.puts
20
+ output.puts "Rerun all files containing failures:"
21
+ output.puts
22
+
23
+ output.puts failure_colored("rspec #{failed_files.join(" ")}")
24
+ end
25
+
26
+ private
27
+
28
+ def failure_colored(str)
29
+ RSpec::Core::Formatters::ConsoleCodes.wrap(str, :failure)
30
+ end
31
+ end
@@ -0,0 +1,21 @@
1
+ require "bundler/setup"
2
+ require "sidekiq/symbols"
3
+ require "sidekiq/testing"
4
+ require "pry-byebug"
5
+
6
+ RSpec.configure do |config|
7
+ config.expect_with :rspec do |c|
8
+ c.syntax = :expect
9
+ end
10
+
11
+ config.filter_run :focus
12
+ config.run_all_when_everything_filtered = true
13
+
14
+ if config.files_to_run.one?
15
+ config.full_backtrace = true
16
+ end
17
+
18
+ config.around(:each) do |example|
19
+ Sidekiq::Testing.inline!(&example)
20
+ end
21
+ end
@@ -0,0 +1,46 @@
1
+ require "spec_helper"
2
+
3
+ describe Sidekiq::Symbols do
4
+ class SampleJob
5
+ include Sidekiq::Worker
6
+ include Sidekiq::Symbols
7
+
8
+ def perform(*args, **kwargs)
9
+ $find_a_better_way_than_this = [args, kwargs]
10
+ end
11
+ end
12
+
13
+ class SampleHashArgJob
14
+ include Sidekiq::Worker
15
+ include Sidekiq::Symbols
16
+
17
+ def perform(x, opts = {})
18
+ $find_a_better_way_than_this = [x, opts]
19
+ end
20
+ end
21
+
22
+ def expect_transformation(klass, *input, arg_signature)
23
+ expect(klass.new.perform(*input)).to eq(arg_signature)
24
+ klass.perform_async(*input)
25
+ expect($find_a_better_way_than_this).to eq(arg_signature)
26
+ end
27
+
28
+ it "allows regular arguments" do
29
+ expect_transformation(SampleJob, 1, [[1], {}])
30
+ end
31
+
32
+ it "allows keyword args" do
33
+ expect_transformation(SampleJob, { x: 1 }, [[], x: 1])
34
+ end
35
+
36
+ it "symbolizes old-style hash args" do
37
+ expect_transformation(SampleHashArgJob, 1, { "x" => 1 }, [1, x: 1])
38
+ end
39
+
40
+ it "symbolizes all arguments to Sidekiq's perform" do
41
+ input = [1, "x" => { "y" => 2, z: { "foo bar" => 0 } }]
42
+ arg_signature = [[1], { x: { y: 2, z: { :"foo bar" => 0 } } }]
43
+
44
+ expect_transformation(SampleJob, *input, arg_signature)
45
+ end
46
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq-symbols
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Adam Prescott
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sidekiq
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Symbolize your Sidekiq jobs.
56
+ email:
57
+ - adam@aprescott.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - Gemfile
63
+ - LICENSE
64
+ - README.md
65
+ - lib/sidekiq-symbols.rb
66
+ - lib/sidekiq/symbols.rb
67
+ - sidekiq-symbols.gemspec
68
+ - spec/re_run_friendly_formatter.rb
69
+ - spec/spec_helper.rb
70
+ - spec/symbols_spec.rb
71
+ homepage: https://github.com/aprescott/sidekiq-symbols
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: 2.0.0
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.2.2
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Symbolize your Sidekiq jobs.
95
+ test_files:
96
+ - spec/re_run_friendly_formatter.rb
97
+ - spec/spec_helper.rb
98
+ - spec/symbols_spec.rb