pakiderm 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +39 -13
- data/lib/pakiderm.rb +20 -58
- data/lib/pakiderm/version.rb +3 -0
- metadata +22 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5a06111ba9f942e52a25123a6bd971bd9d589aee
|
4
|
+
data.tar.gz: 1c44ca4ae3fde3db92211845f9c9d17102929ce5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f982969d4f44cdd49b75b7f51bcbd927578ffc25c085f776b464684acedf85332222db71c403d193594a92b60fb21f2f8d61c1220175f1ee93cf761e5268f9dc
|
7
|
+
data.tar.gz: 59ba25e547895beaa4d345b3ff7a1b9c14551e5671be21c67948590fafddd534dcbe14efaf3ff7354e6bad9d5f2f46869b52a475bcfe7fde26b434c08d0d4454
|
data/README.md
CHANGED
@@ -1,18 +1,23 @@
|
|
1
1
|
Pakiderm
|
2
2
|
=====================
|
3
3
|
|
4
|
-
|
4
|
+
[](http://badge.fury.io/rb/pakiderm)
|
5
|
+
[](https://travis-ci.org/ElMassimo/pakiderm)
|
6
|
+
[](https://coveralls.io/github/ElMassimo/pakiderm?branch=master)
|
7
|
+
[](http://inch-ci.org/github/ElMassimo/pakiderm)
|
8
|
+
[](https://github.com/ElMassimo/queryable/blob/master/LICENSE.txt)
|
9
|
+
|
10
|
+
Allows you to unobtrusively memoize simple methods.
|
5
11
|
|
6
12
|
## Usage
|
7
13
|
```ruby
|
8
14
|
class Counter
|
9
15
|
extend Pakiderm
|
10
16
|
|
17
|
+
memoize \
|
11
18
|
def increment
|
12
19
|
@sum = 1 + @sum.to_i
|
13
20
|
end
|
14
|
-
|
15
|
-
memoize :increment
|
16
21
|
end
|
17
22
|
```
|
18
23
|
Your method will only be called the first time, the result will be stored and returned on subsequent invocations.
|
@@ -25,17 +30,38 @@ counter.increment
|
|
25
30
|
|
26
31
|
counter.increment
|
27
32
|
=> 1
|
28
|
-
|
29
|
-
counter.increment = 5
|
30
|
-
=> 5
|
31
|
-
|
32
|
-
counter.increment
|
33
|
-
=> 5
|
34
33
|
```
|
35
|
-
|
34
|
+
You can also pass a list of methods that should be memoized:
|
35
|
+
```ruby
|
36
|
+
memoize :complex_calculation, :api_response, :db_query
|
37
|
+
```
|
36
38
|
|
37
39
|
## Background
|
38
|
-
Pakiderm
|
40
|
+
Pakiderm adds a method by using `Module#prepend` in order to"intercept" calls to the original method and provide memoization.
|
41
|
+
|
42
|
+
### Caveat
|
43
|
+
If you override the method in a subclass, Pakiderm's method won't be able to intercept calls to the subclass' method.
|
44
|
+
|
45
|
+
License
|
46
|
+
--------
|
47
|
+
|
48
|
+
Copyright (c) 2014 Máximo Mussini
|
49
|
+
|
50
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
51
|
+
a copy of this software and associated documentation files (the
|
52
|
+
"Software"), to deal in the Software without restriction, including
|
53
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
54
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
55
|
+
permit persons to whom the Software is furnished to do so, subject to
|
56
|
+
the following conditions:
|
57
|
+
|
58
|
+
The above copyright notice and this permission notice shall be
|
59
|
+
included in all copies or substantial portions of the Software.
|
39
60
|
|
40
|
-
|
41
|
-
|
61
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
62
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
63
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
64
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
65
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
66
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
67
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/pakiderm.rb
CHANGED
@@ -5,84 +5,46 @@
|
|
5
5
|
#
|
6
6
|
# class PersonQuery
|
7
7
|
# extend Pakiderm
|
8
|
+
#
|
9
|
+
# memoize \
|
8
10
|
# def recent_activity
|
9
11
|
# NSA.get("/api/#{person.id}")
|
10
12
|
# end
|
11
|
-
# memoize :recent_activity
|
12
13
|
# end
|
13
14
|
#
|
14
15
|
module Pakiderm
|
15
|
-
|
16
|
-
# Public: Adds memoization to methods without parameters. Can receive a
|
17
|
-
# single method name, or a list of methods, and an :assignable option that
|
18
|
-
# when true will create an assignment method to modify the memoized value.
|
16
|
+
# Public: Adds memoization to methods without parameters using Module#prepend.
|
19
17
|
#
|
20
|
-
# names - Names of the methods to be memoized.
|
21
|
-
# the last parameter. Only valid option is :assignable.
|
18
|
+
# names - Names of the methods to be memoized.
|
22
19
|
#
|
23
20
|
# Examples
|
24
21
|
#
|
25
22
|
# memoize :tired?, :complex_formula, :long_running_method!
|
26
23
|
#
|
27
|
-
# memoize :meaning_of_life, assignable: true
|
28
|
-
# # self.meaning_of_life = 42
|
29
|
-
#
|
30
24
|
# Returns nothing.
|
31
25
|
def memoize(*names)
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
ivar = Pakiderm.memoized_ivar_for(name)
|
36
|
-
memoized.module_eval Pakiderm.memoized_method(name, ivar)
|
37
|
-
memoized.module_eval Pakiderm.assignment_method(name, ivar) if assignable
|
38
|
-
end
|
39
|
-
prepend memoized
|
40
|
-
end
|
41
|
-
|
42
|
-
# Internal: Generates a memoized method string.
|
43
|
-
#
|
44
|
-
# name - Name of the method to memoize.
|
45
|
-
#
|
46
|
-
# ivar - Name of the instance variable used to store the memoized result.
|
47
|
-
#
|
48
|
-
# Returns a String with the code of the memoized method.
|
49
|
-
def self.memoized_method(name, ivar)
|
50
|
-
<<-MEMOIZATION
|
51
|
-
def #{name}
|
52
|
-
unless #{ivar}
|
53
|
-
#{ivar} = [super]
|
54
|
-
end
|
55
|
-
#{ivar}[0]
|
56
|
-
end
|
57
|
-
MEMOIZATION
|
58
|
-
end
|
26
|
+
prepend Module.new {
|
27
|
+
names.each do |name|
|
28
|
+
ivar = Pakiderm.memoized_ivar_for(name)
|
59
29
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
def self.assignment_method(name, ivar)
|
68
|
-
<<-ASSIGNMENT
|
69
|
-
def #{name}=(value)
|
70
|
-
#{ivar} = [value]
|
30
|
+
define_method(name) {
|
31
|
+
if instance_variable_defined?(ivar)
|
32
|
+
instance_variable_get(ivar)
|
33
|
+
else
|
34
|
+
instance_variable_set(ivar, super())
|
35
|
+
end
|
36
|
+
}
|
71
37
|
end
|
72
|
-
|
38
|
+
}
|
73
39
|
end
|
74
40
|
|
75
|
-
# Internal: Creates a valid instance variable
|
41
|
+
# Internal: Creates a valid name for the instance variable used to store the
|
42
|
+
# memoized value for a method.
|
76
43
|
#
|
77
|
-
# name - Name of the
|
78
|
-
#
|
79
|
-
# Examples
|
80
|
-
#
|
81
|
-
# Pakiderm.memoized_ivar_for(:empty?)
|
82
|
-
# # => '@_memoized_empty_query'
|
44
|
+
# name - Name of the method to memoize.
|
83
45
|
#
|
84
|
-
# Returns a String
|
46
|
+
# Returns a String.
|
85
47
|
def self.memoized_ivar_for(name)
|
86
|
-
"@
|
48
|
+
"@pakiderm_#{ name.to_s.gsub('?', '_question_mark').sub('!', '_bang') }"
|
87
49
|
end
|
88
50
|
end
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pakiderm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Máximo Mussini
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
12
|
-
dependencies:
|
11
|
+
date: 2017-04-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec-given
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.0'
|
13
27
|
description: Pakiderm is a simple module that encapsulates a modern memoization technique.
|
14
28
|
email:
|
15
29
|
- maximomussini@gmail.com
|
@@ -20,30 +34,29 @@ extra_rdoc_files:
|
|
20
34
|
files:
|
21
35
|
- README.md
|
22
36
|
- lib/pakiderm.rb
|
37
|
+
- lib/pakiderm/version.rb
|
23
38
|
homepage: https://github.com/ElMassimo/pakiderm
|
24
39
|
licenses:
|
25
40
|
- MIT
|
26
41
|
metadata: {}
|
27
42
|
post_install_message:
|
28
|
-
rdoc_options:
|
29
|
-
- --charset=UTF-8
|
43
|
+
rdoc_options: []
|
30
44
|
require_paths:
|
31
45
|
- lib
|
32
46
|
required_ruby_version: !ruby/object:Gem::Requirement
|
33
47
|
requirements:
|
34
|
-
- -
|
48
|
+
- - "~>"
|
35
49
|
- !ruby/object:Gem::Version
|
36
50
|
version: '2.0'
|
37
51
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
52
|
requirements:
|
39
|
-
- -
|
53
|
+
- - ">="
|
40
54
|
- !ruby/object:Gem::Version
|
41
55
|
version: '0'
|
42
56
|
requirements: []
|
43
57
|
rubyforge_project:
|
44
|
-
rubygems_version: 2.
|
58
|
+
rubygems_version: 2.5.1
|
45
59
|
signing_key:
|
46
60
|
specification_version: 4
|
47
61
|
summary: Pakiderm will never forget the return value.
|
48
62
|
test_files: []
|
49
|
-
has_rdoc:
|