memist 1.0.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 72a40de13f874f8261ab7c2f0f2098f1bbd568bc
4
- data.tar.gz: e66501e8087fe19589282c3fd6b7dc1615d6fd45
2
+ SHA256:
3
+ metadata.gz: 87d64e83cf6fcd474d07bb4bd88e592620e6f888fee450da956b394cc25e9271
4
+ data.tar.gz: 7465bac42705e1783047eb7962bc970cafd77a1ae163e3a36d298af15b9bf2dd
5
5
  SHA512:
6
- metadata.gz: 758e80604bb05ae541f668d7cf4254dbff6010216d4e9690ce25815df1fe95da4f56ee62c61cf3e5705448d1df2aeb03143dea65d8612d945e296cf0dfc2c55e
7
- data.tar.gz: edb7b787e2e0bb334f01e79892bf41b80b2a1a1a0acde17b0d7cb775f57382e1a4bfd042816ca02c96c820459a8f7688483db0abe2200ca5aab2d280904f08e8
6
+ metadata.gz: 055e3a715c240a1dbdd25d84e4a9cde9329344fb6fab22f21f7f5900fbfbc116c32a07253fe0d367aaa124a7b9efd2418809eea376874a38ecc8818314e8b659
7
+ data.tar.gz: 55bc4d7837f98cfba7779f650a1695067aca9ebd288348b85c7d19156b0084573dc44421f85dd4050bdec001635775114955ed450dea0d83bb5eca30eab7021d
Binary file
Binary file
@@ -1,5 +1 @@
1
- require 'memist/object'
2
-
3
- if defined?(Rails)
4
- require 'memist/railtie'
5
- end
1
+ require 'memist/memoizable'
@@ -0,0 +1,21 @@
1
+ module Memist
2
+ module ClassMethods
3
+ def memoize(method)
4
+ if instance_method(method).arity > 1
5
+ raise ArgumentError, "Cannot memoize `#{method}` method because it accepts more than one argument."
6
+ end
7
+
8
+ define_method "#{method}_with_memoization" do |arg = nil|
9
+ @memoized_values ||= Hash.new { |hash, key| hash[key] = {} }
10
+ if @memoized_values.key?(method) && @memoized_values[method].key?(arg)
11
+ @memoized_values[method][arg]
12
+ else
13
+ @memoized_values[method][arg] = send("#{method}_without_memoization", *arg)
14
+ end
15
+ end
16
+
17
+ alias_method "#{method}_without_memoization", method
18
+ alias_method method, "#{method}_with_memoization"
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ module Memist
2
+ module InstanceMethods
3
+ def flush_memoization(method = nil, arg = nil)
4
+ return if @memoized_values.nil?
5
+
6
+ if method && @memoized_values.key?(method.to_sym)
7
+ if arg.nil?
8
+ @memoized_values.delete(method.to_sym)
9
+ else
10
+ @memoized_values[method.to_sym].delete(arg)
11
+ end
12
+ else
13
+ @memoized_values = nil
14
+ end
15
+ end
16
+
17
+ def memoized?(method)
18
+ !!(@memoized_values && @memoized_values.include?(method.to_sym))
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,11 @@
1
+ require 'memist/class_methods'
2
+ require 'memist/instance_methods'
3
+
4
+ module Memist
5
+ module Memoizable
6
+ def self.included(base)
7
+ base.extend ClassMethods
8
+ base.include InstanceMethods
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module Memist
2
+ VERSION = '2.0.0'.freeze
3
+ end
metadata CHANGED
@@ -1,14 +1,36 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: memist
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Cooke
8
8
  autorequire:
9
9
  bindir: bin
10
- cert_chain: []
11
- date: 2015-08-03 00:00:00.000000000 Z
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIDZDCCAkygAwIBAgIBATANBgkqhkiG9w0BAQUFADA8MQswCQYDVQQDDAJtZTEZ
14
+ MBcGCgmSJomT8ixkARkWCWFkYW1jb29rZTESMBAGCgmSJomT8ixkARkWAmlvMB4X
15
+ DTE5MDUxNDEzNTIxM1oXDTIwMDUxMzEzNTIxM1owPDELMAkGA1UEAwwCbWUxGTAX
16
+ BgoJkiaJk/IsZAEZFglhZGFtY29va2UxEjAQBgoJkiaJk/IsZAEZFgJpbzCCASIw
17
+ DQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMUohRlPw3iIOhWZq+qf5N1ATm1H
18
+ 7gBO4TpsUrw/FL/+urFExzt1+4MPfiKjILge48vKpjoTfuZusRsOQebaFidOfmhk
19
+ sEqa941CvN3OeUYARA53ORlmoLDLmdcrxq430+woFp4uuSYwim/2YQgIMdgiOTqs
20
+ cHaM9yh/xUGMnH4lB9bBDNfggMmkSFb6P8Ax4rvdX3EVv5P58RHwHszd+UI4fyy9
21
+ 0W143m6ntNmqena4ZOc7HtWtRyDHHXXzlGgmghKEZgOA+/LO53VHp+cM0JqB7lq5
22
+ ZxN43fQrIT5yY9Dy7dRBeiDo53WNJPspa5soEivCBVYstMqfd+LGk/BnsyMCAwEA
23
+ AaNxMG8wCQYDVR0TBAIwADALBgNVHQ8EBAMCBLAwHQYDVR0OBBYEFGlRGerNfr6J
24
+ Dprgl6DQ3kLvgVvPMBoGA1UdEQQTMBGBD21lQGFkYW1jb29rZS5pbzAaBgNVHRIE
25
+ EzARgQ9tZUBhZGFtY29va2UuaW8wDQYJKoZIhvcNAQEFBQADggEBAK2TQPMeW9qh
26
+ NDNoVbbplfSc8/uscSP2DfssCbhXQqeDfF2z+kQpxv8iAc++KTlotqOaX5A6RvLb
27
+ NvuwMHPJRQJ2e8rbuN8Sh3tUjbkAEv3SFw4hqbKmtp0j2oKBU0dxHWNfp+5ulh2l
28
+ UXnQAt4zg3v1hTD1VrwLqG/hyk9xAaWB38lEDBuPhLrDIdDJklg9bD1E2TUvoMrg
29
+ L6TIbdP1TRrxINO1D9GzboR+OuWos7qMLBEEbjat/fQchYrW1KLcHIUCyrGkZTLm
30
+ 3wUJNGnT5XYq+qvTqmjkTSTfdGvZCM63C6bGdN5CAyMokGOOatGqyCMAONolWnfC
31
+ gm3t2GWWrxY=
32
+ -----END CERTIFICATE-----
33
+ date: 2019-10-17 00:00:00.000000000 Z
12
34
  dependencies: []
13
35
  description: A Ruby Memoization Helper
14
36
  email:
@@ -18,8 +40,10 @@ extensions: []
18
40
  extra_rdoc_files: []
19
41
  files:
20
42
  - lib/memist.rb
21
- - lib/memist/object.rb
22
- - lib/memist/railtie.rb
43
+ - lib/memist/class_methods.rb
44
+ - lib/memist/instance_methods.rb
45
+ - lib/memist/memoizable.rb
46
+ - lib/memist/version.rb
23
47
  homepage: https://github.com/adamcooke/memist
24
48
  licenses:
25
49
  - MIT
@@ -39,8 +63,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
39
63
  - !ruby/object:Gem::Version
40
64
  version: '0'
41
65
  requirements: []
42
- rubyforge_project:
43
- rubygems_version: 2.4.5
66
+ rubygems_version: 3.0.6
44
67
  signing_key:
45
68
  specification_version: 4
46
69
  summary: A Ruby Memoization Helper
Binary file
@@ -1,91 +0,0 @@
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
@@ -1,9 +0,0 @@
1
- module Memist
2
- class Railtie < ::Rails::Railtie
3
- initializer 'memist.initialize' do
4
- ActiveSupport.on_load(:active_record) do
5
- ActiveRecord::Base.alias_method_chain :write_attribute, :memoization_flush
6
- end
7
- end
8
- end
9
- end