memery 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.rubocop.yml +1 -1
- data/.travis.yml +23 -3
- data/README.md +56 -3
- data/lib/memery.rb +2 -1
- data/lib/memery/version.rb +1 -1
- data/memery.gemspec +2 -0
- metadata +31 -5
- data/bin/console +0 -15
- data/bin/setup +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2592e62955d94ec822ac01546d7914e4a1b1bac3cf767f133c0bb9d2740d797c
|
4
|
+
data.tar.gz: d55627dd3396a960fb16509986b7843b2507cdc522f04b1c16a1b78c2e367dc8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: edbfaefbbfd51dcac0b99a690e065c291f66b20534e5252fa074ee8c7cc83f3256c5cae63abd63f2d1de8c6fb1ca1253e1969ac36c9098ec8fad518d60b39795
|
7
|
+
data.tar.gz: 50617bebf3b45b586a55d41267835dbbecba826bcb9be8c5a7392b6661a7019eaa1de95745a024c3e437796df7e94537b2affe6942597b5a855bcfa1d3c7c30c
|
data/.rubocop.yml
CHANGED
data/.travis.yml
CHANGED
@@ -1,5 +1,25 @@
|
|
1
|
-
sudo: false
|
2
1
|
language: ruby
|
2
|
+
|
3
|
+
sudo: false
|
4
|
+
|
3
5
|
rvm:
|
4
|
-
- 2.
|
5
|
-
|
6
|
+
- 2.2
|
7
|
+
- 2.3
|
8
|
+
- 2.4
|
9
|
+
- 2.5
|
10
|
+
- ruby-head
|
11
|
+
|
12
|
+
before_install: gem install bundler
|
13
|
+
|
14
|
+
env: SUITE="rspec"
|
15
|
+
|
16
|
+
script: bundle exec $SUITE
|
17
|
+
|
18
|
+
matrix:
|
19
|
+
fast_finish: true
|
20
|
+
# Only run RuboCop on the latest Ruby
|
21
|
+
include:
|
22
|
+
- rvm: 2.5
|
23
|
+
env: SUITE="rubocop"
|
24
|
+
allow_failures:
|
25
|
+
- rvm: ruby-head
|
data/README.md
CHANGED
@@ -1,6 +1,37 @@
|
|
1
|
-
# Memery
|
1
|
+
# Memery [![Gem Version](https://badge.fury.io/rb/memery.svg)](https://badge.fury.io/rb/memery) [![Build Status](https://travis-ci.org/tycooon/memery.svg?branch=master)](https://travis-ci.org/tycooon/memery) [![Coverage Status](https://coveralls.io/repos/github/tycooon/memery/badge.svg?branch=master)](https://coveralls.io/github/tycooon/memery?branch=master)
|
2
2
|
|
3
|
-
Memery is a gem for memoization in Ruby
|
3
|
+
Memery is a Ruby gem for memoization of method return values. The normal memoization in Ruby doesn't require any gems and looks like this:
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
def user
|
7
|
+
@user ||= User.find(some_id)
|
8
|
+
end
|
9
|
+
```
|
10
|
+
|
11
|
+
However, this approach dosn't work if the calculated result can be `nil` or `false` or in case the method is using arguments. You will also require extra `begin`/`end` lines in case your method requires multiple lines:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
def user
|
15
|
+
@user ||= begin
|
16
|
+
some_id = calculate_id
|
17
|
+
klass = calculate_klass
|
18
|
+
klass.find(some_id)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
```
|
22
|
+
|
23
|
+
For all these situations memoization gems (like this one) exist. The last example can be rewritten using memery like this:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
memoize def user
|
27
|
+
some_id = calculate_id
|
28
|
+
klass = calculate_klass
|
29
|
+
klass.find(some_id)
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
## Installation
|
34
|
+
Juts add `gem "memery"` to your Gemfile.
|
4
35
|
|
5
36
|
## Usage
|
6
37
|
|
@@ -30,6 +61,26 @@ a.call { 1 } # => 42
|
|
30
61
|
# Will print because passing a block disables memoization
|
31
62
|
```
|
32
63
|
|
64
|
+
For class methods:
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
class B
|
68
|
+
class << self
|
69
|
+
include Memery
|
70
|
+
|
71
|
+
memoize def call
|
72
|
+
puts "calculating"
|
73
|
+
42
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
B.call # => 42
|
79
|
+
B.call # => 42
|
80
|
+
B.call # => 42
|
81
|
+
# Text will be printed only once.
|
82
|
+
```
|
83
|
+
|
33
84
|
## Difference with other gems
|
34
85
|
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:
|
35
86
|
|
@@ -86,5 +137,7 @@ a.users {}
|
|
86
137
|
However, this solution is kind of hacky.
|
87
138
|
|
88
139
|
## License
|
89
|
-
|
90
140
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
141
|
+
|
142
|
+
## Author
|
143
|
+
Created by Yuri Smirnov.
|
data/lib/memery.rb
CHANGED
@@ -14,7 +14,7 @@ module Memery
|
|
14
14
|
:private
|
15
15
|
when klass.protected_method_defined?(method_name)
|
16
16
|
:protected
|
17
|
-
|
17
|
+
when klass.public_method_defined?(method_name)
|
18
18
|
:public
|
19
19
|
end
|
20
20
|
end
|
@@ -36,6 +36,7 @@ module Memery
|
|
36
36
|
def define_memoized_method!(method_name)
|
37
37
|
mod_id = @_memery_module.object_id
|
38
38
|
visibility = Memery.method_visibility(self, method_name)
|
39
|
+
raise ArgumentError, "Method #{method_name} is not defined on #{self}" unless visibility
|
39
40
|
|
40
41
|
@_memery_module.module_eval do
|
41
42
|
define_method(method_name) do |*args, &block|
|
data/lib/memery/version.rb
CHANGED
data/memery.gemspec
CHANGED
@@ -21,8 +21,10 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.require_paths = ["lib"]
|
22
22
|
|
23
23
|
spec.add_development_dependency "bundler"
|
24
|
+
spec.add_development_dependency "coveralls"
|
24
25
|
spec.add_development_dependency "pry"
|
25
26
|
spec.add_development_dependency "rake"
|
26
27
|
spec.add_development_dependency "rspec"
|
27
28
|
spec.add_development_dependency "rubocop-config-umbrellio"
|
29
|
+
spec.add_development_dependency "simplecov"
|
28
30
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: memery
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.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:
|
11
|
+
date: 2018-04-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: coveralls
|
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'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: pry
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -80,6 +94,20 @@ dependencies:
|
|
80
94
|
- - ">="
|
81
95
|
- !ruby/object:Gem::Version
|
82
96
|
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: simplecov
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
83
111
|
description: Memery is a gem for memoization.
|
84
112
|
email:
|
85
113
|
- tycooon@yandex.ru
|
@@ -95,8 +123,6 @@ files:
|
|
95
123
|
- LICENSE.txt
|
96
124
|
- README.md
|
97
125
|
- Rakefile
|
98
|
-
- bin/console
|
99
|
-
- bin/setup
|
100
126
|
- lib/memery.rb
|
101
127
|
- lib/memery/version.rb
|
102
128
|
- memery.gemspec
|
@@ -120,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
120
146
|
version: '0'
|
121
147
|
requirements: []
|
122
148
|
rubyforge_project:
|
123
|
-
rubygems_version: 2.6
|
149
|
+
rubygems_version: 2.7.6
|
124
150
|
signing_key:
|
125
151
|
specification_version: 4
|
126
152
|
summary: A gem for memoization.
|
data/bin/console
DELETED
@@ -1,15 +0,0 @@
|
|
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__)
|