memery 1.2.0 → 1.3.0

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
  SHA256:
3
- metadata.gz: b422cc5336406f390272a02aea253b991470519a786844cb72b4cc0c05ef3b56
4
- data.tar.gz: 12c2329a8539305d85dcb99b30604e55aa04b0f75153fc6a173ae73875f423c8
3
+ metadata.gz: 826d0bdf9b559052298b1d6fcd11b22c302533e35037f21213680f80d019528b
4
+ data.tar.gz: b953dc19e1d6743f4d12e088b2efad526dac3a92b4eaa9d2ee0aeb5e641b358b
5
5
  SHA512:
6
- metadata.gz: 94b69a032837511e10b368ab6db8212bf6b48c8b835c5f76732484b05dea8910af17712797a42fa334ee0529973f8c61d69ea052c1949619411bc1ae7c0897fa
7
- data.tar.gz: fd17f734226f87487223034bf07afae259ed54ca5b216bfc11322b38adcdf1dd3898bf0f238ab8dcb846492c7f93baa8e51d909584bd1ec1e8b07774d4cc3693
6
+ metadata.gz: 741369cf1d8fb3cf743d19d7415cb96ede62ddb71419882089dd41cf19b461d06cb1dbd08b8c2358fd29e70f6637d34dba8fcc999ae6ea94b8a2adc623c2bac3
7
+ data.tar.gz: f7a8f6829fdf1b4b4a3f674ea5d9394c27230589ad7ccd4d4e61471261915fa81db4cc75ae6048daa068dba50864dc59109e25c8b2e00d48d54c24eddcc640ae
data/.gitignore CHANGED
@@ -1,6 +1,5 @@
1
1
  /.bundle/
2
2
  /.yardoc
3
- /Gemfile.lock
4
3
  /coverage/
5
4
  /doc/
6
5
  /pkg/
@@ -5,7 +5,7 @@ AllCops:
5
5
  DisplayCopNames: true
6
6
  TargetRubyVersion: 2.5
7
7
 
8
- Naming/UncommunicativeMethodParamName:
8
+ Naming/MethodParameterName:
9
9
  AllowedNames: ["x", "y", "z"]
10
10
 
11
11
  RSpec/EmptyLineAfterHook:
@@ -3,10 +3,10 @@ language: ruby
3
3
  sudo: false
4
4
 
5
5
  rvm:
6
- - 2.3
7
6
  - 2.4
8
7
  - 2.5
9
8
  - 2.6
9
+ - 2.7
10
10
  - ruby-head
11
11
 
12
12
  before_install: gem install bundler
@@ -1,5 +1,15 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [1.3.0] - 2020-02-10
4
+ ### Added
5
+ - Allow memoization after including module with Memery. ([@AlexWayfer]) [#23]
6
+
7
+ ### Changed
8
+ - Make `memoize` return the method name to allow chaining. ([@JelF]) [#22]
9
+
10
+ ### Fixed
11
+ - Fix warnings in Ruby 2.7. ([@AlexWayfer]) [#19], [#25]
12
+
3
13
  ## [1.2.0] - 2019-10-19
4
14
  ### Added
5
15
  - Add `:ttl` option for `memoize` method ([@AlexWayfer]) [#11]
@@ -19,7 +29,7 @@
19
29
  - Readme example for memoizing class methods. ([@AlexWayfer]) [#3]
20
30
  - Memery raises `ArgumentError` if method is not defined when you call `memoize`.
21
31
 
22
- ## [0.5.0] - 2017-16-12
32
+ ## [0.5.0] - 2017-06-12
23
33
  - Initial public version.
24
34
 
25
35
  [0.5.0]: https://github.com/tycooon/memery/tree/v0.5.0
@@ -27,10 +37,12 @@
27
37
  [1.0.0]: https://github.com/tycooon/memery/compare/v0.6.0...v1.0.0
28
38
  [1.1.0]: https://github.com/tycooon/memery/compare/v1.0.0...v1.1.0
29
39
  [1.2.0]: https://github.com/tycooon/memery/compare/v1.1.0...v1.2.0
30
- [Unreleased]: https://github.com/tycooon/memery/compare/v1.2.0...HEAD
40
+ [1.3.0]: https://github.com/tycooon/memery/compare/v1.2.0...v1.3.0
41
+ [Unreleased]: https://github.com/tycooon/memery/compare/v1.3.0...HEAD
31
42
 
32
43
  [@tycooon]: https://github.com/tycooon
33
44
  [@AlexWayfer]: https://github.com/AlexWayfer
45
+ [@JelF]: https://github.com/JelF
34
46
 
35
47
  [#3]: https://github.com/tycooon/memery/pull/3
36
48
  [#7]: https://github.com/tycooon/memery/pull/7
@@ -38,3 +50,7 @@
38
50
  [#11]: https://github.com/tycooon/memery/pull/11
39
51
  [#14]: https://github.com/tycooon/memery/pull/14
40
52
  [#17]: https://github.com/tycooon/memery/pull/17
53
+ [#19]: https://github.com/tycooon/memery/pull/19
54
+ [#22]: https://github.com/tycooon/memery/pull/22
55
+ [#23]: https://github.com/tycooon/memery/pull/23
56
+ [#25]: https://github.com/tycooon/memery/pull/25
@@ -0,0 +1,99 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ memery (1.3.0)
5
+ ruby2_keywords (~> 0.0.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.0)
11
+ benchmark-ips (2.7.2)
12
+ benchmark-memory (0.1.2)
13
+ memory_profiler (~> 0.9)
14
+ coderay (1.1.2)
15
+ coveralls (0.8.23)
16
+ json (>= 1.8, < 3)
17
+ simplecov (~> 0.16.1)
18
+ term-ansicolor (~> 1.3)
19
+ thor (>= 0.19.4, < 2.0)
20
+ tins (~> 1.6)
21
+ diff-lcs (1.3)
22
+ docile (1.3.2)
23
+ jaro_winkler (1.5.4)
24
+ json (2.3.0)
25
+ memory_profiler (0.9.14)
26
+ method_source (0.9.2)
27
+ parallel (1.19.1)
28
+ parser (2.7.0.2)
29
+ ast (~> 2.4.0)
30
+ pry (0.12.2)
31
+ coderay (~> 1.1.0)
32
+ method_source (~> 0.9.0)
33
+ rack (2.1.1)
34
+ rainbow (3.0.0)
35
+ rake (13.0.1)
36
+ rspec (3.9.0)
37
+ rspec-core (~> 3.9.0)
38
+ rspec-expectations (~> 3.9.0)
39
+ rspec-mocks (~> 3.9.0)
40
+ rspec-core (3.9.1)
41
+ rspec-support (~> 3.9.1)
42
+ rspec-expectations (3.9.0)
43
+ diff-lcs (>= 1.2.0, < 2.0)
44
+ rspec-support (~> 3.9.0)
45
+ rspec-mocks (3.9.1)
46
+ diff-lcs (>= 1.2.0, < 2.0)
47
+ rspec-support (~> 3.9.0)
48
+ rspec-support (3.9.2)
49
+ rubocop (0.79.0)
50
+ jaro_winkler (~> 1.5.1)
51
+ parallel (~> 1.10)
52
+ parser (>= 2.7.0.1)
53
+ rainbow (>= 2.2.2, < 4.0)
54
+ ruby-progressbar (~> 1.7)
55
+ unicode-display_width (>= 1.4.0, < 1.7)
56
+ rubocop-config-umbrellio (0.79.0.68)
57
+ rubocop (= 0.79.0)
58
+ rubocop-performance (= 1.5.2)
59
+ rubocop-rails (= 2.4.1)
60
+ rubocop-rspec (= 1.37.1)
61
+ rubocop-performance (1.5.2)
62
+ rubocop (>= 0.71.0)
63
+ rubocop-rails (2.4.1)
64
+ rack (>= 1.1)
65
+ rubocop (>= 0.72.0)
66
+ rubocop-rspec (1.37.1)
67
+ rubocop (>= 0.68.1)
68
+ ruby-progressbar (1.10.1)
69
+ ruby2_keywords (0.0.2)
70
+ simplecov (0.16.1)
71
+ docile (~> 1.1)
72
+ json (>= 1.8, < 3)
73
+ simplecov-html (~> 0.10.0)
74
+ simplecov-html (0.10.2)
75
+ sync (0.5.0)
76
+ term-ansicolor (1.7.1)
77
+ tins (~> 1.0)
78
+ thor (1.0.1)
79
+ tins (1.24.0)
80
+ sync
81
+ unicode-display_width (1.6.1)
82
+
83
+ PLATFORMS
84
+ ruby
85
+
86
+ DEPENDENCIES
87
+ benchmark-ips
88
+ benchmark-memory
89
+ bundler
90
+ coveralls
91
+ memery!
92
+ pry
93
+ rake
94
+ rspec
95
+ rubocop-config-umbrellio
96
+ simplecov
97
+
98
+ BUNDLED WITH
99
+ 2.1.2
@@ -1,34 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "ruby2_keywords"
4
+
3
5
  require "memery/version"
4
6
 
5
7
  module Memery
6
8
  class << self
9
+ def monotonic_clock
10
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
11
+ end
12
+ end
13
+
14
+ module ModuleMethods
7
15
  def included(base)
8
16
  base.extend(ClassMethods)
9
17
  base.include(InstanceMethods)
10
- end
11
-
12
- def method_visibility(klass, method_name)
13
- case
14
- when klass.private_method_defined?(method_name)
15
- :private
16
- when klass.protected_method_defined?(method_name)
17
- :protected
18
- when klass.public_method_defined?(method_name)
19
- :public
20
- end
21
- end
22
-
23
- def monotonic_clock
24
- Process.clock_gettime(Process::CLOCK_MONOTONIC)
18
+ base.extend ModuleMethods if base.instance_of?(Module)
25
19
  end
26
20
  end
27
21
 
22
+ extend ModuleMethods
23
+
28
24
  module ClassMethods
29
25
  def memoize(method_name, condition: nil, ttl: nil)
30
26
  prepend_memery_module!
31
27
  define_memoized_method!(method_name, condition: condition, ttl: ttl)
28
+ method_name
32
29
  end
33
30
 
34
31
  def memoized?(method_name)
@@ -42,37 +39,61 @@ module Memery
42
39
 
43
40
  def prepend_memery_module!
44
41
  return if defined?(@_memery_module)
45
- @_memery_module = Module.new
42
+ @_memery_module = Module.new do
43
+ extend MemoizationModule
44
+ end
46
45
  prepend @_memery_module
47
46
  end
48
47
 
49
- def define_memoized_method!(method_name, condition: nil, ttl: nil)
50
- mod_id = @_memery_module.object_id
51
- visibility = Memery.method_visibility(self, method_name)
52
- raise ArgumentError, "Method #{method_name} is not defined on #{self}" unless visibility
48
+ def define_memoized_method!(*args, **kwargs)
49
+ @_memery_module.public_send __method__, self, *args, **kwargs
50
+ end
51
+
52
+ module MemoizationModule
53
+ def define_memoized_method!(klass, method_name, condition: nil, ttl: nil)
54
+ method_key = "#{method_name}_#{object_id}"
55
+
56
+ original_visibility = method_visibility(klass, method_name)
53
57
 
54
- @_memery_module.module_eval do
55
- define_method(method_name) do |*args, &block|
58
+ define_method method_name do |*args, &block|
56
59
  if block || (condition && !instance_exec(&condition))
57
60
  return super(*args, &block)
58
61
  end
59
62
 
60
- key = "#{method_name}_#{mod_id}"
63
+ store = (@_memery_memoized_values ||= {})[method_key] ||= {}
61
64
 
62
- store = (@_memery_memoized_values ||= {})[key] ||= {}
63
-
64
- if store.key?(args) && (ttl.nil? || Memery.monotonic_clock <= store[args][:time] + ttl)
65
+ if store.key?(args) &&
66
+ (ttl.nil? || Memery.monotonic_clock <= store[args][:time] + ttl)
65
67
  return store[args][:result]
66
68
  end
67
69
 
68
70
  result = super(*args)
69
- @_memery_memoized_values[key][args] = { result: result, time: Memery.monotonic_clock }
71
+ @_memery_memoized_values[method_key][args] =
72
+ { result: result, time: Memery.monotonic_clock }
70
73
  result
71
74
  end
72
75
 
73
- send(visibility, method_name)
76
+ ruby2_keywords method_name
77
+
78
+ send original_visibility, method_name
79
+ end
80
+
81
+ private
82
+
83
+ def method_visibility(klass, method_name)
84
+ if klass.private_method_defined?(method_name)
85
+ :private
86
+ elsif klass.protected_method_defined?(method_name)
87
+ :protected
88
+ elsif klass.public_method_defined?(method_name)
89
+ :public
90
+ else
91
+ raise ArgumentError, "Method #{method_name} is not defined on #{klass}"
92
+ end
74
93
  end
75
94
  end
95
+
96
+ private_constant :MemoizationModule
76
97
  end
77
98
 
78
99
  module InstanceMethods
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Memery
4
- VERSION = "1.2.0"
4
+ VERSION = "1.3.0"
5
5
  end
@@ -20,6 +20,8 @@ Gem::Specification.new do |spec|
20
20
  end
21
21
  spec.require_paths = ["lib"]
22
22
 
23
+ spec.add_runtime_dependency "ruby2_keywords", "~> 0.0.2"
24
+
23
25
  spec.add_development_dependency "benchmark-ips"
24
26
  spec.add_development_dependency "benchmark-memory"
25
27
  spec.add_development_dependency "bundler"
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: memery
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuri Smirnov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-19 00:00:00.000000000 Z
11
+ date: 2020-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruby2_keywords
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.2
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: benchmark-ips
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -150,6 +164,7 @@ files:
150
164
  - ".travis.yml"
151
165
  - CHANGELOG.md
152
166
  - Gemfile
167
+ - Gemfile.lock
153
168
  - LICENSE.txt
154
169
  - README.md
155
170
  - Rakefile
@@ -176,7 +191,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
176
191
  - !ruby/object:Gem::Version
177
192
  version: '0'
178
193
  requirements: []
179
- rubygems_version: 3.0.3
194
+ rubygems_version: 3.1.2
180
195
  signing_key:
181
196
  specification_version: 4
182
197
  summary: A gem for memoization.