memoist 0.12.0 → 0.13.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
2
  SHA1:
3
- metadata.gz: b2940374d92fdc408f67414dc6aefaee449b0722
4
- data.tar.gz: 1e695236b23a8a12790732c8f00fbe0bf7251fda
3
+ metadata.gz: 5f737a677ee84ae6e38db47c459ce5151d87d57b
4
+ data.tar.gz: 454244a7dc119201a0339227a5e6c54a8ec64e06
5
5
  SHA512:
6
- metadata.gz: 714ba49dd47d39cfaef3d19ff8e0d56e115d059760016fd43a6d6379701422e58fb10a8b6481972d4108fc7e6db35cf82339a494dcff810f743363db6de757e8
7
- data.tar.gz: 9a645417282d73779cdfabaad7222b6b5bc769e9d1a0968ffa51f901eb66ce3bdbadb715398a7c7853ad4607da3c63fffad3b97b684b22b69740612d47712c08
6
+ metadata.gz: 67f7764129d2f645a6d9e4d586bf8fed469460b10af0eb56bf7906191ce271a1d1ca7413e2f67cab12519862342b516c305994f2e1f73d263443f48e79dfffc9
7
+ data.tar.gz: 2b7d05ccffe29ce0cd42b93aa430ed682fbe90dae8badfe632f6e2573f9daa1705aabb2fb16bf360ba7b649128caecddd9a7ce8cfebcedf055beefd5d690db07
data/README.md CHANGED
@@ -14,72 +14,81 @@ Usage
14
14
 
15
15
  Just extend with the Memoist module
16
16
 
17
- require 'memoist'
18
- class Person
19
- extend Memoist
20
-
21
- def social_security
22
- decrypt_social_security
23
- end
24
- memoize :social_security
25
- end
17
+ ```ruby
18
+ require 'memoist'
19
+ class Person
20
+ extend Memoist
21
+
22
+ def social_security
23
+ decrypt_social_security
24
+ end
25
+ memoize :social_security
26
+ end
27
+ ```
26
28
 
27
29
  And person.social_security will only be calculated once.
28
30
 
29
31
  Every memoized function (which initially was not accepting any arguments) has a ```(reload)```
30
32
  argument you can pass in to bypass and reset the memoization:
31
33
 
32
- def some_method
33
- Time.now
34
- end
35
- memoize :some_method
34
+ ```ruby
35
+ def some_method
36
+ Time.now
37
+ end
38
+ memoize :some_method
39
+ ```
36
40
 
37
41
  Calling ```some_method``` will be memoized, but calling ```some_method(true)``` will rememoize each time.
38
42
 
39
43
  You can even memoize method that takes arguments.
40
44
 
41
-
42
- class Person
43
- def taxes_due(income)
44
- income * 0.40
45
- end
46
- memoize :taxes_due
47
- end
45
+ ```ruby
46
+ class Person
47
+ def taxes_due(income)
48
+ income * 0.40
49
+ end
50
+ memoize :taxes_due
51
+ end
52
+ ```
48
53
 
49
54
  This will only be calculated once per value of income.
50
55
 
51
56
  You can also memoize class methods.
52
57
 
53
- class Person
54
-
55
- class << self
56
- extend Memoist
57
- def with_overdue_taxes
58
- # ...
59
- end
60
- memoize :with_overdue_taxes
61
- end
58
+ ```ruby
59
+ class Person
62
60
 
61
+ class << self
62
+ extend Memoist
63
+ def with_overdue_taxes
64
+ # ...
63
65
  end
66
+ memoize :with_overdue_taxes
67
+ end
68
+
69
+ end
70
+ ```
64
71
 
65
72
  When a sub-class overrides one of its parent's methods and you need to memoize both.
66
73
  Then you can use the `:identifier` parameter in order to help _Memoist_ distinguish between the two.
67
74
 
68
- class Clock
69
- extend Memoist
70
- def now
71
- "The time now is #{Time.now.hour} o'clock and #{Time.now.min} minutes"
72
- end
73
- memoize :now
74
- end
75
-
76
- class AccurateClock < Clock
77
- extend Memoist
78
- def now
79
- "#{super} and #{Time.now.sec} seconds"
80
- end
81
- memoize :now, :identifier => :accurate_clock
82
- end
75
+ ```ruby
76
+ class Clock
77
+ extend Memoist
78
+ def now
79
+ "The time now is #{Time.now.hour} o'clock and #{Time.now.min} minutes"
80
+ end
81
+ memoize :now
82
+ end
83
+
84
+ class AccurateClock < Clock
85
+ extend Memoist
86
+ def now
87
+ "#{super} and #{Time.now.sec} seconds"
88
+ end
89
+ memoize :now, :identifier => :accurate_clock
90
+ end
91
+ ```
83
92
 
84
93
 
85
94
  Reload
@@ -87,17 +96,23 @@ Reload
87
96
 
88
97
  Each memoized function comes with a way to flush the existing value.
89
98
 
90
- person.social_security # returns the memoized value
91
- person.social_security(true) # bypasses the memoized value and rememoizes it
99
+ ```ruby
100
+ person.social_security # returns the memoized value
101
+ person.social_security(true) # bypasses the memoized value and rememoizes it
102
+ ```
92
103
 
93
104
  This also works with a memoized method with arguments
94
105
 
95
- person.taxes_due(100_000) # returns the memoized value
96
- person.taxes_due(100_000, true) # bypasses the memoized value and rememoizes it
106
+ ```ruby
107
+ person.taxes_due(100_000) # returns the memoized value
108
+ person.taxes_due(100_000, true) # bypasses the memoized value and rememoizes it
109
+ ```
97
110
 
98
111
  If you want to flush the entire memoization cache for an object
99
112
 
100
- person.flush_cache
113
+ ```ruby
114
+ person.flush_cache
115
+ ```
101
116
 
102
117
  Authors
103
118
  ===========
@@ -3,23 +3,40 @@ require 'memoist/core_ext/singleton_class'
3
3
  module Memoist
4
4
 
5
5
  def self.memoized_ivar_for(method_name, identifier=nil)
6
- ["@#{memoized_prefix(identifier)}", escape_punctuation(method_name.to_s)].join("_")
6
+ "@#{memoized_prefix(identifier)}_#{escape_punctuation(method_name.to_s)}"
7
7
  end
8
8
 
9
9
  def self.unmemoized_method_for(method_name, identifier=nil)
10
- [unmemoized_prefix(identifier), method_name].join("_").to_sym
10
+ "#{unmemoized_prefix(identifier)}_#{method_name}".to_sym
11
11
  end
12
12
 
13
13
  def self.memoized_prefix(identifier=nil)
14
- ["_memoized", identifier].compact.join("_")
14
+ if identifier
15
+ "_memoized_#{identifier}"
16
+ else
17
+ "_memoized".freeze
18
+ end
15
19
  end
16
20
 
17
21
  def self.unmemoized_prefix(identifier=nil)
18
- ["_unmemoized", identifier].compact.join("_")
22
+ if identifier
23
+ "_unmemoized_#{identifier}"
24
+ else
25
+ "_unmemoized".freeze
26
+ end
19
27
  end
20
28
 
21
29
  def self.escape_punctuation(string)
22
- string.sub(/\?\Z/, '_query').sub(/!\Z/, '_bang')
30
+ return string unless string.end_with?('?'.freeze, '!'.freeze)
31
+
32
+ string = string.dup
33
+
34
+ # A String can't end in both ? and !
35
+ if string.sub!(/\?\Z/, '_query'.freeze)
36
+ else
37
+ string.sub!(/!\Z/, '_bang'.freeze)
38
+ end
39
+ string
23
40
  end
24
41
 
25
42
  def self.memoist_eval(klass, *args, &block)
@@ -47,15 +64,7 @@ module Memoist
47
64
  end
48
65
 
49
66
  def prime_cache(*method_names)
50
- if method_names.empty?
51
- prefix = Memoist.unmemoized_prefix+"_"
52
- method_names = methods.collect do |method_name|
53
- if method_name.to_s.start_with?(prefix)
54
- method_name[prefix.length..-1]
55
- end
56
- end.compact
57
- end
58
-
67
+ method_names = self.class.memoized_methods if method_names.empty?
59
68
  method_names.each do |method_name|
60
69
  if method(Memoist.unmemoized_method_for(method_name)).arity == 0
61
70
  __send__(method_name)
@@ -67,14 +76,7 @@ module Memoist
67
76
  end
68
77
 
69
78
  def flush_cache(*method_names)
70
- if method_names.empty?
71
- prefix = Memoist.unmemoized_prefix+"_"
72
- method_names = (methods + private_methods + protected_methods).collect do |method_name|
73
- if method_name.to_s.start_with?(prefix)
74
- method_name[prefix.length..-1]
75
- end
76
- end.compact
77
- end
79
+ method_names = self.class.memoized_methods if method_names.empty?
78
80
 
79
81
  method_names.each do |method_name|
80
82
  ivar = Memoist.memoized_ivar_for(method_name)
@@ -88,6 +90,13 @@ module Memoist
88
90
  identifier = method_names.pop[:identifier]
89
91
  end
90
92
 
93
+ Memoist.memoist_eval(self) do
94
+ def self.memoized_methods
95
+ require 'set'
96
+ @_memoized_methods ||= Set.new
97
+ end
98
+ end
99
+
91
100
  method_names.each do |method_name|
92
101
  unmemoized_method = Memoist.unmemoized_method_for(method_name, identifier)
93
102
  memoized_ivar = Memoist.memoized_ivar_for(method_name, identifier)
@@ -101,6 +110,7 @@ module Memoist
101
110
  end
102
111
  alias_method unmemoized_method, method_name
103
112
 
113
+ self.memoized_methods << method_name
104
114
  if instance_method(method_name).arity == 0
105
115
 
106
116
  # define a method like this;
@@ -1,3 +1,3 @@
1
1
  module Memoist
2
- VERSION = "0.12.0"
2
+ VERSION = "0.13.0"
3
3
  end
@@ -32,7 +32,7 @@ Gem::Specification.new do |spec|
32
32
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
33
33
  spec.require_paths = ["lib"]
34
34
 
35
- spec.add_development_dependency "bundler", "~> 1.5"
35
+ spec.add_development_dependency "bundler"
36
36
  spec.add_development_dependency "rake"
37
- spec.add_development_dependency "minitest", "~> 3.0"
37
+ spec.add_development_dependency "minitest", "~> 5.5.1"
38
38
  end
@@ -1,7 +1,7 @@
1
1
  require 'test_helper'
2
2
  require 'memoist'
3
3
 
4
- class MemoistTest < Minitest::Unit::TestCase
4
+ class MemoistTest < Minitest::Test
5
5
 
6
6
  class CallCounter
7
7
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: memoist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.0
4
+ version: 0.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Peek
@@ -19,22 +19,22 @@ authors:
19
19
  autorequire:
20
20
  bindir: bin
21
21
  cert_chain: []
22
- date: 2015-04-13 00:00:00.000000000 Z
22
+ date: 2015-11-26 00:00:00.000000000 Z
23
23
  dependencies:
24
24
  - !ruby/object:Gem::Dependency
25
25
  name: bundler
26
26
  requirement: !ruby/object:Gem::Requirement
27
27
  requirements:
28
- - - "~>"
28
+ - - ">="
29
29
  - !ruby/object:Gem::Version
30
- version: '1.5'
30
+ version: '0'
31
31
  type: :development
32
32
  prerelease: false
33
33
  version_requirements: !ruby/object:Gem::Requirement
34
34
  requirements:
35
- - - "~>"
35
+ - - ">="
36
36
  - !ruby/object:Gem::Version
37
- version: '1.5'
37
+ version: '0'
38
38
  - !ruby/object:Gem::Dependency
39
39
  name: rake
40
40
  requirement: !ruby/object:Gem::Requirement
@@ -55,14 +55,14 @@ dependencies:
55
55
  requirements:
56
56
  - - "~>"
57
57
  - !ruby/object:Gem::Version
58
- version: '3.0'
58
+ version: 5.5.1
59
59
  type: :development
60
60
  prerelease: false
61
61
  version_requirements: !ruby/object:Gem::Requirement
62
62
  requirements:
63
63
  - - "~>"
64
64
  - !ruby/object:Gem::Version
65
- version: '3.0'
65
+ version: 5.5.1
66
66
  description:
67
67
  email:
68
68
  - josh@joshpeek.com
@@ -113,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
113
  version: '0'
114
114
  requirements: []
115
115
  rubyforge_project:
116
- rubygems_version: 2.4.5
116
+ rubygems_version: 2.4.8
117
117
  signing_key:
118
118
  specification_version: 4
119
119
  summary: memoize methods invocation