memery 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +6 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +54 -0
- data/Rakefile +8 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/memery.rb +64 -0
- data/lib/memery/version.rb +5 -0
- data/memery.gemspec +28 -0
- metadata +127 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: '0854715beed165845002189a1574152c3f79024e'
|
4
|
+
data.tar.gz: 8fa7be482b75e8c37caadd35d339dbeefbb670fc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cb9fd46d872ba116c7d69583292ae1f2a36eee6d2095d699a5790fd5aa9dde9d663f1e80b248549ff2d70b8e9e28a0c9a5f071e4b5288f68e1e84e5d3eced1bf
|
7
|
+
data.tar.gz: 671d518ef8430157f8f30e3ea544ef45f9e472d54a8600345d30c8a4029c2782c79c36d93985f48f17718099a9aa9c91e2a59df677319e372f9a4eae2dab4361
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Yuri Smirnov
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# Memery
|
2
|
+
|
3
|
+
Memery is a gem for memoization in Ruby. Example:
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
class A
|
9
|
+
include Memery
|
10
|
+
|
11
|
+
memoize def call
|
12
|
+
puts "calculating"
|
13
|
+
42
|
14
|
+
end
|
15
|
+
|
16
|
+
# or:
|
17
|
+
# def call
|
18
|
+
# ...
|
19
|
+
# end
|
20
|
+
# memoize :call
|
21
|
+
end
|
22
|
+
|
23
|
+
a = A.new
|
24
|
+
a.call # => 42
|
25
|
+
a.call # => 42
|
26
|
+
a.call # => 42
|
27
|
+
|
28
|
+
# Text will be printed only once.
|
29
|
+
```
|
30
|
+
|
31
|
+
## Difference with other gems
|
32
|
+
Memery is very similar to [Memoist](https://github.com/matthewrudy/memoist). The difference is that it doesn't override methods, instead it uses Ruby 2 `Module.prepend` feature. This approach is cleaner and it allows subclasses' methods to work properly: if you redefine a memoized method in a subclass, it's not memoized by default, but you can memoize it normally (without using awkward `identifier: ` argument) and it will just work.
|
33
|
+
|
34
|
+
The other key difference is that it doesn't change method's signature (no extra `reload` param). If you need unmemoized result of method, just create an unmemoized version like this:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
memoize def users
|
38
|
+
get_users
|
39
|
+
end
|
40
|
+
|
41
|
+
def get_users
|
42
|
+
# ...
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
Alternatively, you can clear the whole instance's cache:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
a.clear_memery_cache!
|
50
|
+
```
|
51
|
+
|
52
|
+
## License
|
53
|
+
|
54
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "memery"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/memery.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "memery/version"
|
4
|
+
|
5
|
+
module Memery
|
6
|
+
def self.included(base)
|
7
|
+
base.extend(ClassMethods)
|
8
|
+
base.include(InstanceMethods)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.method_visibility(klass, method_name)
|
12
|
+
case
|
13
|
+
when klass.private_method_defined?(method_name)
|
14
|
+
:private
|
15
|
+
when klass.protected_method_defined?(method_name)
|
16
|
+
:protected
|
17
|
+
else
|
18
|
+
:public
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
module ClassMethods
|
23
|
+
def memoize(method_name)
|
24
|
+
prepend_memery_module!
|
25
|
+
define_memoized_method!(method_name)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def prepend_memery_module!
|
31
|
+
return if defined?(@_memery_module)
|
32
|
+
@_memery_module = Module.new
|
33
|
+
prepend @_memery_module
|
34
|
+
end
|
35
|
+
|
36
|
+
def define_memoized_method!(method_name)
|
37
|
+
mod_id = @_memery_module.object_id
|
38
|
+
visibility = Memery.method_visibility(self, method_name)
|
39
|
+
|
40
|
+
@_memery_module.module_eval do
|
41
|
+
define_method(method_name) do |*args|
|
42
|
+
@_memery_memoized_values ||= {}
|
43
|
+
|
44
|
+
key = [method_name, mod_id].join("_").to_sym
|
45
|
+
store = @_memery_memoized_values[key] ||= {}
|
46
|
+
|
47
|
+
if store.key?(args)
|
48
|
+
store[args]
|
49
|
+
else
|
50
|
+
store[args] = super(*args)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
send(visibility, method_name)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
module InstanceMethods
|
60
|
+
def clear_memery_cache!
|
61
|
+
@_memery_memoized_values = {}
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
data/memery.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path("../lib", __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require "memery/version"
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "memery"
|
9
|
+
spec.version = Memery::VERSION
|
10
|
+
spec.authors = ["Yuri Smirnov"]
|
11
|
+
spec.email = ["tycooon@yandex.ru"]
|
12
|
+
|
13
|
+
spec.summary = "A gem for memoization."
|
14
|
+
spec.description = "Memery is a gem for memoization."
|
15
|
+
spec.homepage = "https://github.com/tycooon/memery"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
20
|
+
end
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler"
|
24
|
+
spec.add_development_dependency "pry"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
spec.add_development_dependency "rubocop-config-umbrellio"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: memery
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yuri Smirnov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-12-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
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: pry
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
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
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop-config-umbrellio
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Memery is a gem for memoization.
|
84
|
+
email:
|
85
|
+
- tycooon@yandex.ru
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".rubocop.yml"
|
93
|
+
- ".travis.yml"
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- bin/console
|
99
|
+
- bin/setup
|
100
|
+
- lib/memery.rb
|
101
|
+
- lib/memery/version.rb
|
102
|
+
- memery.gemspec
|
103
|
+
homepage: https://github.com/tycooon/memery
|
104
|
+
licenses:
|
105
|
+
- MIT
|
106
|
+
metadata: {}
|
107
|
+
post_install_message:
|
108
|
+
rdoc_options: []
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 2.6.14
|
124
|
+
signing_key:
|
125
|
+
specification_version: 4
|
126
|
+
summary: A gem for memoization.
|
127
|
+
test_files: []
|