memist 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/lib/memist.rb +5 -0
- data/lib/memist/object.rb +91 -0
- data/lib/memist/railtie.rb +9 -0
- metadata +47 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 72a40de13f874f8261ab7c2f0f2098f1bbd568bc
|
4
|
+
data.tar.gz: e66501e8087fe19589282c3fd6b7dc1615d6fd45
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 758e80604bb05ae541f668d7cf4254dbff6010216d4e9690ce25815df1fe95da4f56ee62c61cf3e5705448d1df2aeb03143dea65d8612d945e296cf0dfc2c55e
|
7
|
+
data.tar.gz: edb7b787e2e0bb334f01e79892bf41b80b2a1a1a0acde17b0d7cb775f57382e1a4bfd042816ca02c96c820459a8f7688483db0abe2200ca5aab2d280904f08e8
|
data/lib/memist.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
module Memist
|
2
|
+
|
3
|
+
module ClassMethods
|
4
|
+
def memoize(method, options = {})
|
5
|
+
if self.instance_method(method).arity > 0
|
6
|
+
raise "Cannot memoize `#{method}` method because it accepts an argument."
|
7
|
+
end
|
8
|
+
|
9
|
+
define_method "#{method}_with_memoization" do
|
10
|
+
@memoized_values ||= {}
|
11
|
+
if @memoized_values.has_key?(method)
|
12
|
+
@memoized_values[method].memoized!
|
13
|
+
else
|
14
|
+
@memoized_values[method] = send("#{method}_without_memoization")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
self.memoized_methods[method] = options
|
19
|
+
|
20
|
+
if options[:uses].is_a?(Array)
|
21
|
+
options[:uses].each do |field|
|
22
|
+
self.attributes_to_clear_memoization[field] ||= []
|
23
|
+
self.attributes_to_clear_memoization[field] << method
|
24
|
+
|
25
|
+
# Automatically create an override setter to flush the memoization cache
|
26
|
+
# whenever the value is set.
|
27
|
+
if instance_methods.include?("#{field}=".to_sym) && !instance_methods.include?("#{field}_with_memoization_flush=")
|
28
|
+
define_method "#{field}_with_memoization_flush=" do |value|
|
29
|
+
self.flush_memoization_by_attribute(field)
|
30
|
+
send("#{field}_without_memoization_flush=", value)
|
31
|
+
end
|
32
|
+
alias_method "#{field}_without_memoization_flush=", "#{field}="
|
33
|
+
alias_method "#{field}=", "#{field}_with_memoization_flush="
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
alias_method "#{method}_without_memoization", method
|
40
|
+
alias_method method, "#{method}_with_memoization"
|
41
|
+
end
|
42
|
+
|
43
|
+
def attributes_to_clear_memoization
|
44
|
+
@attributes_to_clear_memoization ||= {}
|
45
|
+
end
|
46
|
+
|
47
|
+
def memoized_methods
|
48
|
+
@memoized_methods ||= {}
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
module InstanceMethods
|
54
|
+
def memoized!
|
55
|
+
@memoized = true unless frozen?
|
56
|
+
self
|
57
|
+
end
|
58
|
+
|
59
|
+
def memoized?
|
60
|
+
!!@memoized
|
61
|
+
end
|
62
|
+
|
63
|
+
def write_attribute_with_memoization_flush(attr_name, value)
|
64
|
+
flush_memoization_by_attribute(attr_name)
|
65
|
+
write_attribute_without_memoization_flush(attr_name, value)
|
66
|
+
end
|
67
|
+
|
68
|
+
def flush_memoization_by_attribute(attribute)
|
69
|
+
if methods = self.class.attributes_to_clear_memoization[attribute.to_sym]
|
70
|
+
methods.each do |method|
|
71
|
+
self.flush_memoization_cache(method)
|
72
|
+
self.flush_memoization_by_attribute(method)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def flush_memoization_cache(method = nil)
|
78
|
+
if method
|
79
|
+
if @memoized_values
|
80
|
+
@memoized_values.delete(method)
|
81
|
+
end
|
82
|
+
else
|
83
|
+
@memoized_values = nil
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
Object.send :include, InstanceMethods
|
89
|
+
Object.extend ClassMethods
|
90
|
+
|
91
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: memist
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adam Cooke
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-03 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A Ruby Memoization Helper
|
14
|
+
email:
|
15
|
+
- me@adamcooke.io
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/memist.rb
|
21
|
+
- lib/memist/object.rb
|
22
|
+
- lib/memist/railtie.rb
|
23
|
+
homepage: https://github.com/adamcooke/memist
|
24
|
+
licenses:
|
25
|
+
- MIT
|
26
|
+
metadata: {}
|
27
|
+
post_install_message:
|
28
|
+
rdoc_options: []
|
29
|
+
require_paths:
|
30
|
+
- lib
|
31
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 2.4.5
|
44
|
+
signing_key:
|
45
|
+
specification_version: 4
|
46
|
+
summary: A Ruby Memoization Helper
|
47
|
+
test_files: []
|