short_circu_it 0.6.3 → 0.6.4
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 +118 -2
- data/lib/short_circu_it/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8e7f261ac6aaee08daa2c0e9f9aff3e81d6a82dd19f71b8be5f5d6ff590b02cf
|
4
|
+
data.tar.gz: 0b0a0069e63dc99f7e471bdb6c53370a3fc1103b3212e0bdf58d0ff2a07ddcd8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: da812e637b60671e65164524f41103f2a96b850bede88267d870a6c28eef0c27067b6817af5773832100efcde5cc5548c394fb5589ae1773cc5a37bac78c0491
|
7
|
+
data.tar.gz: 88347985f0e8a7441f18fc675dfbbfd2846b26e964b2fe8ced22eb90f7b8152eecd4a4542783be9a6c6a13b01deae916a23926de037ba870543d57fc6880f0f3
|
data/README.md
CHANGED
@@ -1,9 +1,20 @@
|
|
1
|
-
# ShortCircuIt
|
1
|
+
# [ShortCircuIt](https://www.youtube.com/watch?v=XtP88AGsslo)
|
2
2
|
|
3
3
|
[](https://semaphoreci.com/freshly/spicerack)
|
4
4
|
[](https://codeclimate.com/github/Freshly/spicerack/maintainability)
|
5
5
|
[](https://codeclimate.com/github/Freshly/spicerack/test_coverage)
|
6
6
|
|
7
|
+
* [Installation](#installation)
|
8
|
+
* [What's memoization?](#whats-memoization)
|
9
|
+
* [Usage](#usage)
|
10
|
+
* [Static methods](#static-methods)
|
11
|
+
* [Arguments](#arguments)
|
12
|
+
* [Instance State](#instance-state)
|
13
|
+
* [Observables](#observables)
|
14
|
+
* [Development](#development)
|
15
|
+
* [Contributing](#contributing)
|
16
|
+
* [License](#license)
|
17
|
+
|
7
18
|
## Installation
|
8
19
|
|
9
20
|
Add this line to your application's Gemfile:
|
@@ -20,9 +31,114 @@ Or install it yourself as:
|
|
20
31
|
|
21
32
|
$ gem install short_circu_it
|
22
33
|
|
34
|
+
## What's memoization?
|
35
|
+
|
36
|
+
ShortCircuIt provides method memoization with awareness for arguments, instance state, weather, Oxford commas, or anything else you can think to throw at it.
|
37
|
+
|
38
|
+
Imagine you have this method in your code, for some reason:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
def nth_digit_of_pi(n)
|
42
|
+
# MATH
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
Now imagine you need to show the 1000th digit of pi in three places on a single webpage. Do you calculate it three times, or do you assign it to a variable once and use that variable three times? The latter is memoization; the former is... also an option.
|
47
|
+
|
23
48
|
## Usage
|
24
49
|
|
25
|
-
|
50
|
+
### Static methods
|
51
|
+
|
52
|
+
In the simplest case, `ShortCircuIt` is no harder to forget than the method name itself:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
class TheClassiest
|
56
|
+
include ShortCircuIt
|
57
|
+
|
58
|
+
def how_methodical
|
59
|
+
puts "Pity on my sysop!"
|
60
|
+
this_must_be_expensive
|
61
|
+
end
|
62
|
+
memoize :how_methodical
|
63
|
+
end
|
64
|
+
|
65
|
+
the_classiest = TheClassiest.new
|
66
|
+
3.times.map { the_classiest.how_methodical }
|
67
|
+
# Pity on my sysop!
|
68
|
+
#=> [
|
69
|
+
# "One Billion Dollars",
|
70
|
+
# "One Billion Dollars",
|
71
|
+
# "One Billion Dollars"
|
72
|
+
# ]
|
73
|
+
```
|
74
|
+
|
75
|
+
Even though the method gets called 3 times, the code only gets executed once.
|
76
|
+
|
77
|
+
### Arguments
|
78
|
+
|
79
|
+
But what if your method takes arguments? We gotcha covered - memoization is specific to the arguments passed to the method, so a call with the same arguments will return the memoized value, while a call with different arguments will execute the method again.
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
the_classiest.how_methodical(1)
|
83
|
+
# Pity on my sysop!
|
84
|
+
#=> "One Billion Dollars"
|
85
|
+
|
86
|
+
the_classiest.how_methodical(1)
|
87
|
+
#=> "One Billion Dollars"
|
88
|
+
|
89
|
+
the_classiest.how_methodical(2)
|
90
|
+
# Pity on my sysop!
|
91
|
+
#=> "Two Billion Dollars"
|
92
|
+
```
|
93
|
+
|
94
|
+
### Instance State
|
95
|
+
|
96
|
+
Sometimes instances are stateful and mutable. By default, ShortCircuIt will watch an object's state via its `hash` value, so the memoization is broken when its attributes change:
|
97
|
+
|
98
|
+
```ruby
|
99
|
+
the_classiest.how_methodical(1)
|
100
|
+
# Pity on my sysop!
|
101
|
+
#=> "One Billion Dollars"
|
102
|
+
|
103
|
+
the_classiest.how_methodical(1)
|
104
|
+
#=> "One Billion Dollars"
|
105
|
+
|
106
|
+
the_classiest.orders_of_magnitude = 8
|
107
|
+
the_classiest.how_methodical(1)
|
108
|
+
# Pity on my sysop!
|
109
|
+
#=> "One Hundred Million Dollars"
|
110
|
+
```
|
111
|
+
|
112
|
+
### Observables
|
113
|
+
|
114
|
+
Maybe I have a method I'd like memoize on a complex object with many unrelated attributes:
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
class TheAntist
|
118
|
+
attr_accessor :root_beer_floats_are_delicious, :physics, :weight_of_the_universe
|
119
|
+
|
120
|
+
def how_much_ants_can_carry
|
121
|
+
# SCIENCE
|
122
|
+
end
|
123
|
+
memoize :how_much_ants_can_carry, observes: :physics
|
124
|
+
# Can also be
|
125
|
+
memoize :how_much_ants_can_carry, observes: [:physics, :weight_of_the_universe]
|
126
|
+
end
|
127
|
+
|
128
|
+
antist = TheAntist.new
|
129
|
+
antist.how_much_ants_can_carry
|
130
|
+
# => .5 oz
|
131
|
+
|
132
|
+
# When we change an unobserved value, the memoiozation persists:
|
133
|
+
antist.root_beer_floats_are_delicious = true
|
134
|
+
antist.how_much_ants_can_carry
|
135
|
+
# => .5 oz
|
136
|
+
|
137
|
+
# But if we change the observed value, the memoization is broken:
|
138
|
+
antist.physics = :parallel_universe
|
139
|
+
antist.how_much_ants_can_carry
|
140
|
+
# => 100 lbs
|
141
|
+
```
|
26
142
|
|
27
143
|
## Development
|
28
144
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: short_circu_it
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Allen Rettberg
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-04-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - '='
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 0.6.
|
33
|
+
version: 0.6.4
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - '='
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 0.6.
|
40
|
+
version: 0.6.4
|
41
41
|
description: Memoize methods safely with parameter and dependency observation
|
42
42
|
email:
|
43
43
|
- allen.rettberg@freshly.com
|