lock_and_cache 4.0.4 → 4.0.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
  SHA1:
3
- metadata.gz: 4a3eaf745d9a9532d2062ea332fb63420fabbdd9
4
- data.tar.gz: d1f4ec88da5e0f728da43d9b2f40d536c4224498
3
+ metadata.gz: c01496db7fee2406fdd535409a2ef112969f9243
4
+ data.tar.gz: becfd8a7f466108d3f592bfe7c8f51e92fcb2a8f
5
5
  SHA512:
6
- metadata.gz: f34529d617b048841b0d33234619846f49a7367ff5627e653298098f0780401314f37df6aa9afe73d967ad358eeaffc3fd0d70ccbc043ecb6a8c1b360c2b61f7
7
- data.tar.gz: d006e7f9e798425f6137d5611c3688b3e78fd906e15390ad0ff015f0834f3fd3e9a4d820c4ca546fa606f17f9ea481296d7f444dc0189cb48b4e368643251648
6
+ metadata.gz: d401e8f1d206a4496fd911a863d8ad78ac2aa4354e94d3a608c428667adbdd92fe5d3870ad4f4913a8c90183a9776ddba90ed931ea4fea705ffbb14e953a39e6
7
+ data.tar.gz: 85a4e1bf664f77d1bf4ea4d6d186593e288ffe488578db96d4090f16bee36b9a2c79fea3bd271b9b64ad3dfae9b68d3c34c9557615f2b343bf67eef1c757f079
data/.travis.yml CHANGED
@@ -1,6 +1,7 @@
1
1
  language: ruby
2
2
  sudo: false
3
3
  rvm:
4
- - 2.1.2
4
+ - 2.3.0
5
+ - 2.4.1
5
6
  services:
6
7
  - redis-server
data/CHANGELOG CHANGED
@@ -1,3 +1,17 @@
1
+ unreleased
2
+
3
+ * ?
4
+
5
+ * Don't test on ruby 2.1
6
+
7
+ 4.0.5 / 2017-04-01
8
+
9
+ * Enhancements
10
+
11
+ * allow dates and times in keys
12
+ * Test on ruby 2.3.0 and 2.4.1
13
+ * 2x faster key generation
14
+
1
15
  4.0.4 / 2016-04-11
2
16
 
3
17
  * Bug fixes
@@ -0,0 +1,87 @@
1
+ require 'set'
2
+ require 'date'
3
+ require 'benchmark/ips'
4
+
5
+ ALLOWED_IN_KEYS = [
6
+ ::String,
7
+ ::Symbol,
8
+ ::Numeric,
9
+ ::TrueClass,
10
+ ::FalseClass,
11
+ ::NilClass,
12
+ ::Integer,
13
+ ::Float,
14
+ ::Date,
15
+ ::DateTime,
16
+ ::Time,
17
+ ].to_set
18
+ parts = RUBY_VERSION.split('.').map(&:to_i)
19
+ unless parts[0] >= 2 and parts[1] >= 4
20
+ ALLOWED_IN_KEYS << ::Fixnum
21
+ ALLOWED_IN_KEYS << ::Bignum
22
+ end
23
+
24
+ EXAMPLES = [
25
+ 'hi',
26
+ :there,
27
+ 123,
28
+ 123.54,
29
+ 1e99,
30
+ 123456789 ** 2,
31
+ 1e999,
32
+ true,
33
+ false,
34
+ nil,
35
+ Date.new(2015,1,1),
36
+ Time.now,
37
+ DateTime.now,
38
+ Mutex,
39
+ Mutex.new,
40
+ Benchmark,
41
+ { hi: :world },
42
+ [[]],
43
+ Fixnum,
44
+ Struct,
45
+ Struct.new(:a),
46
+ Struct.new(:a).new(123)
47
+ ]
48
+ EXAMPLES.each do |example|
49
+ puts "#{example} -> #{example.class}"
50
+ end
51
+
52
+ puts
53
+
54
+ [
55
+ Date.new(2015,1,1),
56
+ Time.now,
57
+ DateTime.now,
58
+ ].each do |x|
59
+ puts x.to_s
60
+ end
61
+
62
+ puts
63
+
64
+ EXAMPLES.each do |example|
65
+ a = ALLOWED_IN_KEYS.any? { |thing| example.is_a?(thing) }
66
+ b = ALLOWED_IN_KEYS.include? example.class
67
+ unless a == b
68
+ raise "#{example.inspect}: #{a.inspect} vs #{b.inspect}"
69
+ end
70
+ end
71
+
72
+ Benchmark.ips do |x|
73
+ x.report("any") do
74
+ example = EXAMPLES.sample
75
+ y = ALLOWED_IN_KEYS.any? { |thing| example.is_a?(thing) }
76
+ a = 1
77
+ y
78
+ end
79
+
80
+ x.report("include") do
81
+ example = EXAMPLES.sample
82
+ y = ALLOWED_IN_KEYS.include? example.class
83
+ a = 1
84
+ y
85
+ end
86
+
87
+ end
@@ -1,3 +1,5 @@
1
+ require 'date'
2
+
1
3
  module LockAndCache
2
4
  # @private
3
5
  class Key
@@ -22,8 +24,11 @@ module LockAndCache
22
24
  #
23
25
  # Recursively extract id from obj. Calls #lock_and_cache_key if available, otherwise #id
24
26
  def extract_obj_id(obj)
25
- if ALLOWED_IN_KEYS.any? { |k| obj.is_a?(k) }
27
+ klass = obj.class
28
+ if ALLOWED_IN_KEYS.include?(klass)
26
29
  obj
30
+ elsif DATE.include?(klass)
31
+ obj.to_s
27
32
  elsif obj.respond_to?(:lock_and_cache_key)
28
33
  extract_obj_id obj.lock_and_cache_key
29
34
  elsif obj.respond_to?(:id)
@@ -43,7 +48,19 @@ module LockAndCache
43
48
  ::TrueClass,
44
49
  ::FalseClass,
45
50
  ::NilClass,
46
- ]
51
+ ::Integer,
52
+ ::Float,
53
+ ].to_set
54
+ parts = ::RUBY_VERSION.split('.').map(&:to_i)
55
+ unless parts[0] >= 2 and parts[1] >= 4
56
+ ALLOWED_IN_KEYS << ::Fixnum
57
+ ALLOWED_IN_KEYS << ::Bignum
58
+ end
59
+ DATE = [
60
+ ::Date,
61
+ ::DateTime,
62
+ ::Time,
63
+ ].to_set
47
64
  METHOD_NAME_IN_CALLER = /in `([^']+)'/
48
65
 
49
66
  attr_reader :context
@@ -1,3 +1,3 @@
1
1
  module LockAndCache
2
- VERSION = '4.0.4'
2
+ VERSION = '4.0.5'
3
3
  end
@@ -21,13 +21,23 @@ describe LockAndCache::Key do
21
21
  expect(described_class.new(a: 1).send(:parts)).to eq(described_class.new([[:a, 1]]).send(:parts))
22
22
  end
23
23
 
24
+ now = Time.now
25
+ today = Date.today
24
26
  {
25
27
  [1] => [1],
26
- ["you"] => ['you'],
27
- [["you"]] => [['you']],
28
- [["you"], "person"] => [["you"], "person"],
29
- [["you"], {:silly=>:person}] => [["you"], [[:silly, :person]] ],
30
- { hi: 'you' } => [[:hi, "you"]],
28
+ ['you'] => ['you'],
29
+ [['you']] => [['you']],
30
+ [['you'], "person"] => [['you'], "person"],
31
+ [['you'], {:silly=>:person}] => [['you'], [[:silly, :person]] ],
32
+ [now] => [now.to_s],
33
+ [[now]] => [[now.to_s]],
34
+ [today] => [today.to_s],
35
+ [[today]] => [[today.to_s]],
36
+ { hi: 'you' } => [[:hi, 'you']],
37
+ { hi: 123 } => [[:hi, 123]],
38
+ { hi: 123.0 } => [[:hi, 123.0]],
39
+ { hi: now } => [[:hi, now.to_s]],
40
+ { hi: today } => [[:hi, today.to_s]],
31
41
  [KeyTestId.new] => ['id'],
32
42
  [[KeyTestId.new]] => [['id']],
33
43
  { a: KeyTestId.new } => [[:a, "id"]],
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lock_and_cache
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.4
4
+ version: 4.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seamus Abshere
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-11 00:00:00.000000000 Z
11
+ date: 2017-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -167,6 +167,7 @@ files:
167
167
  - LICENSE.txt
168
168
  - README.md
169
169
  - Rakefile
170
+ - benchmarks/allowed_in_keys.rb
170
171
  - lib/lock_and_cache.rb
171
172
  - lib/lock_and_cache/action.rb
172
173
  - lib/lock_and_cache/key.rb
@@ -195,7 +196,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
195
196
  version: '0'
196
197
  requirements: []
197
198
  rubyforge_project:
198
- rubygems_version: 2.2.2
199
+ rubygems_version: 2.6.8
199
200
  signing_key:
200
201
  specification_version: 4
201
202
  summary: Lock and cache methods.
@@ -203,4 +204,3 @@ test_files:
203
204
  - spec/lock_and_cache/key_spec.rb
204
205
  - spec/lock_and_cache_spec.rb
205
206
  - spec/spec_helper.rb
206
- has_rdoc: