short_circu_it 0.29.1 → 0.29.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/short_circu_it/memoization_store.rb +25 -19
- data/lib/short_circu_it/version.rb +1 -1
- data/lib/short_circu_it.rb +6 -10
- metadata +8 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ce12ac822aed393836ee26072bd9b0eade6df9d9aee2811a735bd217b2ede2ca
|
4
|
+
data.tar.gz: f3fd42c11368138e035955190d00aca55d5754067a8083173d952aac39d0ea6c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d7d05900cf5a4a20afee6f405bda95ff2e22523f048a23134734979ea7c29723c4dac6a01134241cd0bc637a8c958b3b3ed23ac3575cea4e7a28d84a87f63603
|
7
|
+
data.tar.gz: 16c5e13cf88cd8bca999340be8c2f6a2fa6e6ea8abb60a11eb18d73cdc023f8440ccc4131cb3bb2b444a56578f62ecfbb8a8de61d92062db9967fe17340df4f0
|
@@ -4,6 +4,9 @@ require "active_support/core_ext/module"
|
|
4
4
|
|
5
5
|
module ShortCircuIt
|
6
6
|
class MemoizationStore
|
7
|
+
NOT_MEMOIZED = Object.new
|
8
|
+
private_constant :NOT_MEMOIZED
|
9
|
+
|
7
10
|
delegate :memoization_observers, to: :owner
|
8
11
|
|
9
12
|
# @param owner [*] The object being memoized
|
@@ -12,16 +15,23 @@ module ShortCircuIt
|
|
12
15
|
end
|
13
16
|
|
14
17
|
# @param method_name [Symbol] The name of the method being memoized.
|
15
|
-
# @param
|
18
|
+
# @param args [Array<*>] Arguments passed to the method being memoized.
|
19
|
+
# @param kwargs [Hash] Keyword arguments passed to the method being memoized.
|
16
20
|
# @yield [] Yields to a given block with no arguments. Memoizes the value returned by the block.
|
17
21
|
# @return [*] The value returned either from the memoization cache if present, or yielded block if not.
|
18
|
-
def memoize(method_name,
|
19
|
-
|
22
|
+
def memoize(method_name, args, kwargs)
|
23
|
+
argument_hash = [ *args, kwargs ].hash
|
24
|
+
|
25
|
+
value = memoized_value(method_name, argument_hash)
|
26
|
+
|
27
|
+
return value unless value == NOT_MEMOIZED
|
20
28
|
|
21
|
-
|
29
|
+
state_hash = state_hash(method_name)
|
30
|
+
|
31
|
+
clear_memoization(method_name) unless current_memoization_for_method?(method_name, state_hash)
|
22
32
|
|
23
33
|
yield.tap do |returned_value|
|
24
|
-
current_memoization_for_method(method_name)[argument_hash] = returned_value
|
34
|
+
current_memoization_for_method(method_name, state_hash)[argument_hash] = returned_value
|
25
35
|
end
|
26
36
|
end
|
27
37
|
|
@@ -30,7 +40,7 @@ module ShortCircuIt
|
|
30
40
|
# @param *method_names [Symbol] The name of a memoized method
|
31
41
|
# @return [Boolean] True if a value was cleared, false if not
|
32
42
|
def clear_memoization(*method_names)
|
33
|
-
method_names.all? { |method_name| memoized_hash.delete(method_name
|
43
|
+
method_names.all? { |method_name| memoized_hash.delete(method_name) }
|
34
44
|
end
|
35
45
|
|
36
46
|
# Clears all memoized values on the object
|
@@ -52,32 +62,28 @@ module ShortCircuIt
|
|
52
62
|
memoized_hash[method_name] ||= {}
|
53
63
|
end
|
54
64
|
|
55
|
-
# @param method_name [Symbol] The name of the method to memoize
|
56
|
-
# @param argument_hash [Integer] The hash value of the arguments passed to the method
|
57
|
-
# @return [Boolean] True if the method has a current memoized value with the given arguments
|
58
|
-
def memoized?(method_name, argument_hash)
|
59
|
-
current_memoization_for_method?(method_name) && current_memoization_for_method(method_name).key?(argument_hash)
|
60
|
-
end
|
61
|
-
|
62
65
|
# @param method_name [String] The name of the method to memoize
|
63
66
|
# @param argument_hash [Integer] The hash value of the arguments passed to the method
|
64
67
|
# @return [*] The value that has been memoized for the method/argument combination
|
65
68
|
def memoized_value(method_name, argument_hash)
|
66
|
-
raise NotMemoizedError unless memoized?(method_name, argument_hash)
|
67
|
-
|
68
69
|
current_memoization_for_method(method_name)[argument_hash]
|
69
70
|
end
|
70
71
|
|
71
72
|
# @param method_name [Symbol] The name of a memoized method
|
72
73
|
# @return [Hash] A hash of memoized values for the current state of the observed objects for the given method.
|
73
|
-
def current_memoization_for_method(method_name)
|
74
|
-
|
74
|
+
def current_memoization_for_method(method_name, state_hash = nil)
|
75
|
+
state_hash ||= state_hash(method_name)
|
76
|
+
|
77
|
+
# Memoize values inside a hash with default value of NOT_MEMOIZED
|
78
|
+
memoization_for_method(method_name)[state_hash] ||= Hash.new(NOT_MEMOIZED)
|
75
79
|
end
|
76
80
|
|
77
81
|
# @param method_name [Symbol] The name of a memoized method
|
78
82
|
# @return [Boolean] True if there are any memoized values for the current state of the observed objects.
|
79
|
-
def current_memoization_for_method?(method_name)
|
80
|
-
|
83
|
+
def current_memoization_for_method?(method_name, state_hash = nil)
|
84
|
+
state_hash ||= state_hash(method_name)
|
85
|
+
|
86
|
+
memoization_for_method(method_name).key?(state_hash)
|
81
87
|
end
|
82
88
|
|
83
89
|
# @param method_name [Symbol] The name of a memoized method
|
data/lib/short_circu_it.rb
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "short_circu_it/version"
|
4
|
-
require "short_circu_it/errors"
|
5
|
-
require "short_circu_it/memoization_store"
|
6
3
|
require "active_support/core_ext/module"
|
7
4
|
require "active_support/core_ext/array"
|
8
5
|
require "active_support/concern"
|
9
6
|
require "around_the_world"
|
10
7
|
|
8
|
+
require_relative "short_circu_it/version"
|
9
|
+
require_relative "short_circu_it/errors"
|
10
|
+
require_relative "short_circu_it/memoization_store"
|
11
|
+
|
11
12
|
module ShortCircuIt
|
12
13
|
extend ActiveSupport::Concern
|
13
14
|
include AroundTheWorld
|
@@ -80,13 +81,8 @@ module ShortCircuIt
|
|
80
81
|
method_name,
|
81
82
|
prevent_double_wrapping_for: ShortCircuIt,
|
82
83
|
) do |*args, **opts|
|
83
|
-
memoization_store.memoize(method_name,
|
84
|
-
|
85
|
-
if RUBY_VERSION < "2.7" && opts.blank?
|
86
|
-
super(*args)
|
87
|
-
else
|
88
|
-
super(*args, **opts)
|
89
|
-
end
|
84
|
+
memoization_store.memoize(method_name, args, opts) do
|
85
|
+
super(*args, **opts)
|
90
86
|
end
|
91
87
|
end
|
92
88
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: short_circu_it
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.29.
|
4
|
+
version: 0.29.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Allen Rettberg
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-02-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -16,28 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 6.0.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 6.0.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: around_the_world
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - '='
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 0.29.
|
33
|
+
version: 0.29.2
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - '='
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 0.29.
|
40
|
+
version: 0.29.2
|
41
41
|
description: Memoize methods safely with parameter and dependency observation
|
42
42
|
email:
|
43
43
|
- allen.rettberg@freshly.com
|
@@ -59,7 +59,7 @@ metadata:
|
|
59
59
|
homepage_uri: https://github.com/RubyAfterAll/spicerack/tree/main/short_circu_it
|
60
60
|
source_code_uri: https://github.com/RubyAfterAll/spicerack/tree/main/short_circu_it
|
61
61
|
changelog_uri: https://github.com/RubyAfterAll/spicerack/blob/main/short_circu_it/CHANGELOG.md
|
62
|
-
documentation_uri: https://www.rubydoc.info/gems/short_circu_it/0.29.
|
62
|
+
documentation_uri: https://www.rubydoc.info/gems/short_circu_it/0.29.2
|
63
63
|
post_install_message:
|
64
64
|
rdoc_options: []
|
65
65
|
require_paths:
|
@@ -75,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
75
75
|
- !ruby/object:Gem::Version
|
76
76
|
version: '0'
|
77
77
|
requirements: []
|
78
|
-
rubygems_version: 3.3.
|
78
|
+
rubygems_version: 3.3.15
|
79
79
|
signing_key:
|
80
80
|
specification_version: 4
|
81
81
|
summary: An intelligent and feature rich memoization gem
|