pakiderm 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +41 -0
- data/lib/pakiderm.rb +88 -0
- metadata +49 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c5dbcfdc6c5fa18f286ce21d37b5b9560747a8e3
|
4
|
+
data.tar.gz: 5c295331e64db36225eb9554e8ef488d1756d446
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 27c60daed7867e398ee9412cabc17724509aac0498a354d25fc9873bbda9fc3b784e5e038173c5eb5757572228c16010cea8e7e4c48b621f1c6903e525d24f9b
|
7
|
+
data.tar.gz: 262c3402e1d6dc60c650994050c6af51e9957af9d64506216638069478a65640cf0cf3b1599a46533d036ecb8664a1dac429c969f6b43d6ce623957ec5519e9b
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
Pakiderm
|
2
|
+
=====================
|
3
|
+
|
4
|
+
Allows you to memoize simple methods and works only on ruby 2.0.
|
5
|
+
|
6
|
+
## Usage
|
7
|
+
```ruby
|
8
|
+
class Counter
|
9
|
+
extend Pakiderm
|
10
|
+
|
11
|
+
def increment
|
12
|
+
@sum = 1 + @sum.to_i
|
13
|
+
end
|
14
|
+
|
15
|
+
memoize :increment
|
16
|
+
end
|
17
|
+
```
|
18
|
+
Your method will only be called the first time, the result will be stored and returned on subsequent invocations.
|
19
|
+
```irb
|
20
|
+
counter = Counter.new
|
21
|
+
=> Counter
|
22
|
+
|
23
|
+
counter.increment
|
24
|
+
=> 1
|
25
|
+
|
26
|
+
counter.increment
|
27
|
+
=> 1
|
28
|
+
|
29
|
+
counter.increment = 5
|
30
|
+
=> 5
|
31
|
+
|
32
|
+
counter.increment
|
33
|
+
=> 5
|
34
|
+
```
|
35
|
+
As you can see, it also provides an assignment operator, so you can override the memoized value :smiley:.
|
36
|
+
|
37
|
+
## Background
|
38
|
+
Pakiderm uses Module#prepend to add the memoized method before yours in the method lookup path of your class. Your method is called by using 'super', because the class is an ancestor to the prepended module.
|
39
|
+
|
40
|
+
### Corolary
|
41
|
+
If you override a memoized method in a derived class, you alter the method chain, so the derived method won't be memoized unless you invoke memoize in the derived class as well.
|
data/lib/pakiderm.rb
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# Public: Mixin that adds memoization functionality to a plain ruby object.
|
2
|
+
# Memoization is selectively applied to methods by using the `memoize` method.
|
3
|
+
#
|
4
|
+
# Examples
|
5
|
+
#
|
6
|
+
# class PersonQuery
|
7
|
+
# extend Pakiderm
|
8
|
+
# def recent_activity
|
9
|
+
# NSA.get("/api/#{person.id}")
|
10
|
+
# end
|
11
|
+
# memoize :recent_activity
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
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.
|
19
|
+
#
|
20
|
+
# names - Names of the methods to be memoized. Can take a hash of options as
|
21
|
+
# the last parameter. Only valid option is :assignable.
|
22
|
+
#
|
23
|
+
# Examples
|
24
|
+
#
|
25
|
+
# memoize :tired?, :complex_formula, :long_running_method!
|
26
|
+
#
|
27
|
+
# memoize :meaning_of_life, assignable: true
|
28
|
+
# # self.meaning_of_life = 42
|
29
|
+
#
|
30
|
+
# Returns nothing.
|
31
|
+
def memoize(*names)
|
32
|
+
assignable = names.last.is_a?(Hash) ? names.pop[:assignable] : false
|
33
|
+
memoized = Module.new
|
34
|
+
names.each do |name|
|
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
|
59
|
+
|
60
|
+
# Internal: Generates an assignment method for the memoized value.
|
61
|
+
#
|
62
|
+
# name - Name of the memoized method.
|
63
|
+
#
|
64
|
+
# ivar - Name of the instance variable used to store the memoized result.
|
65
|
+
#
|
66
|
+
# Returns a String with the code of the assignment method.
|
67
|
+
def self.assignment_method(name, ivar)
|
68
|
+
<<-ASSIGNMENT
|
69
|
+
def #{name}=(value)
|
70
|
+
#{ivar} = [value]
|
71
|
+
end
|
72
|
+
ASSIGNMENT
|
73
|
+
end
|
74
|
+
|
75
|
+
# Internal: Creates a valid instance variable name from a method name
|
76
|
+
#
|
77
|
+
# name - Name of the memoized method.
|
78
|
+
#
|
79
|
+
# Examples
|
80
|
+
#
|
81
|
+
# Pakiderm.memoized_ivar_for(:empty?)
|
82
|
+
# # => '@_memoized_empty_query'
|
83
|
+
#
|
84
|
+
# Returns a String with the code of the assignment method.
|
85
|
+
def self.memoized_ivar_for(name)
|
86
|
+
"@_memoized_#{name.to_s.sub(/\?$/, '_query').sub(/!$/, '_bang')}"
|
87
|
+
end
|
88
|
+
end
|
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pakiderm
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Máximo Mussini
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-04-14 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Pakiderm is a simple module that encapsulates a modern memoization technique.
|
14
|
+
email:
|
15
|
+
- maximomussini@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files:
|
19
|
+
- README.md
|
20
|
+
files:
|
21
|
+
- README.md
|
22
|
+
- lib/pakiderm.rb
|
23
|
+
homepage: https://github.com/ElMassimo/pakiderm
|
24
|
+
licenses:
|
25
|
+
- MIT
|
26
|
+
metadata: {}
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options:
|
29
|
+
- --charset=UTF-8
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - '>='
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '2.0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubyforge_project:
|
44
|
+
rubygems_version: 2.2.2
|
45
|
+
signing_key:
|
46
|
+
specification_version: 4
|
47
|
+
summary: Pakiderm will never forget the return value.
|
48
|
+
test_files: []
|
49
|
+
has_rdoc:
|