mize 0.2.1 → 0.3.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: 71f0cd0d6b8c6fc9e9886b434dc951794c10ef34
4
- data.tar.gz: 3e9932b466d6c234dd4a68fc088df93b4cab5b18
3
+ metadata.gz: 6e9034cdc4e48b4728a4fc0e15332af968e79a79
4
+ data.tar.gz: 483494fa7a728960a031bcd3dc1febea921d28f4
5
5
  SHA512:
6
- metadata.gz: 981c09c71f2abb0fc8c43e18043007a86ea8f47fb97036feddf56fd35f85c11ea59511a35786a75d062e1683abea56fc124a02b51ba79599db21d797bd927632
7
- data.tar.gz: a2ac96737adac8ce91dabb99fab4329a42a4afbf650609ea37c2cadae9511429edff4a6c089e090379b74576f5257c376dcdc3e180768da03fa15ff5a0464560
6
+ metadata.gz: 25ee593f7690ee48fe44def237a2c006797b23f0236779d65fab254656666a65c4b6ccf8c2c116c6e9a2c8c131a0e08bd0933f6b73938ee89748ccf26d25548b
7
+ data.tar.gz: 25fe53fcbf4802c07beafc0cd8f1b2fdf76e72c3c9b1404fea676172fca927c90a176e1a3577914c26ae9f6e6e29a60c5bc1d900e2ecb3d84659eaede43bc8ad
data/README.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  ## Description
4
4
 
5
+ TODO
5
6
 
6
7
  ## Installation
7
8
 
@@ -17,12 +18,7 @@ and bundle.
17
18
 
18
19
  ## Usage
19
20
 
20
- ### Configuration
21
-
22
-
23
- ## Changes
24
-
25
- * 2015-12-10 Release 0.0.0
21
+ TODO
26
22
 
27
23
  ## Download
28
24
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.0
@@ -9,14 +9,16 @@ module Mize
9
9
  # memoized results do NOT ONLY depend on the arguments, but ALSO on the
10
10
  # object the method is called on. In the latter the memoized results ONLY
11
11
  # depend on the arguments given to the function. If the +freeze+ argument
12
- # is true, the result is frozen if possible to make it immutable.
13
- def memoize(method: nil, function: nil, freeze: false)
12
+ # is true, the result is frozen if possible to make it immutable. If the
13
+ # store_nil argument is false do not store nil results and always recompute
14
+ # otherwise don't which is the default.
15
+ def memoize(method: nil, function: nil, freeze: false, store_nil: true)
14
16
  if method && function
15
17
  raise ArgumentError, 'memoize a method xor a function'
16
18
  elsif method
17
- wrap_method method, freeze: freeze
19
+ wrap_method method, freeze: freeze, store_nil: store_nil
18
20
  elsif function
19
- wrap_method function, function: true, freeze: freeze
21
+ wrap_method function, function: true, freeze: freeze, store_nil: store_nil
20
22
  else
21
23
  raise ArgumentError, 'missing keyword: method/function'
22
24
  end
@@ -24,7 +26,23 @@ module Mize
24
26
 
25
27
  private
26
28
 
27
- def wrap_method(method_id, freeze: false, function: false)
29
+ class << self
30
+ private
31
+
32
+ def compute_result(orig_method, key, context, args, freeze)
33
+ result = orig_method.bind(context).call(*args)
34
+ if $DEBUG
35
+ warn "#{context.class} cached method "\
36
+ "#{method_id}(#{args.inspect unless args.empty?}) = "\
37
+ "#{result.inspect} [#{__id__}]"
38
+ end
39
+ freeze and result.freeze rescue nil
40
+ result
41
+ end
42
+ end
43
+
44
+ def wrap_method(method_id, freeze: false, function: false, store_nil: true)
45
+ verbose, $VERBOSE = $VERBOSE, nil
28
46
  include CacheMethods
29
47
 
30
48
  function and mc = __mize_cache__
@@ -39,22 +57,22 @@ module Mize
39
57
  __send__(:define_method, method_id) do |*args|
40
58
  function or mc = __mize_cache__
41
59
  key = build_key(method_id, *args)
42
- if mc.exist?(key) and result = mc.read(key)
43
- result
60
+ if mc.exist?(key)
61
+ mc.read(key)
44
62
  else
45
- result = orig_method.bind(self).call(*args)
46
- mc.write(key, result)
47
- if $DEBUG
48
- warn "#{self.class} cached method "\
49
- "#{method_id}(#{args.inspect unless args.empty?}) = "\
50
- "#{result.inspect} [#{__id__}]"
63
+ result = Mize::Memoize.send(
64
+ :compute_result, orig_method, key, self, args, freeze
65
+ )
66
+ if store_nil || !result.nil?
67
+ mc.write(key, result)
51
68
  end
52
- freeze and result.freeze rescue result
69
+ result
53
70
  end
54
- result
55
71
  end
56
72
  end
57
73
  method_id
74
+ ensure
75
+ $VERBOSE = verbose
58
76
  end
59
77
  end
60
78
  end
@@ -1,6 +1,6 @@
1
1
  module Mize
2
2
  # Mize version
3
- VERSION = '0.2.1'
3
+ VERSION = '0.3.0'
4
4
  VERSION_ARRAY = VERSION.split('.').map(&:to_i) # :nodoc:
5
5
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
6
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
@@ -1,14 +1,14 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: mize 0.2.1 ruby lib
2
+ # stub: mize 0.3.0 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "mize".freeze
6
- s.version = "0.2.1"
6
+ s.version = "0.3.0"
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib".freeze]
10
10
  s.authors = ["Florian Frank".freeze]
11
- s.date = "2017-01-23"
11
+ s.date = "2017-02-27"
12
12
  s.description = "Library that provides memoziation for methods and functions for Ruby.".freeze
13
13
  s.email = "flori@ping.de".freeze
14
14
  s.extra_rdoc_files = ["README.md".freeze, "lib/mize.rb".freeze, "lib/mize/cache_methods.rb".freeze, "lib/mize/cache_protocol.rb".freeze, "lib/mize/configure.rb".freeze, "lib/mize/default_cache.rb".freeze, "lib/mize/global_clear.rb".freeze, "lib/mize/memoize.rb".freeze, "lib/mize/railtie.rb".freeze, "lib/mize/reload.rb".freeze, "lib/mize/version.rb".freeze]
@@ -17,7 +17,7 @@ Gem::Specification.new do |s|
17
17
  s.licenses = ["MIT".freeze]
18
18
  s.rdoc_options = ["--title".freeze, "Mize - Library that provides memoziation for methods and functions".freeze, "--main".freeze, "README.md".freeze]
19
19
  s.required_ruby_version = Gem::Requirement.new(">= 2".freeze)
20
- s.rubygems_version = "2.6.8".freeze
20
+ s.rubygems_version = "2.6.10".freeze
21
21
  s.summary = "Library that provides memoziation for methods and functions".freeze
22
22
  s.test_files = ["spec/mize/global_clear_spec.rb".freeze, "spec/mize/memoize_method_spec.rb".freeze, "spec/mize/memoize_reload_spec.rb".freeze, "spec/mize/version_spec.rb".freeze, "spec/spec_helper.rb".freeze]
23
23
 
@@ -1,12 +1,24 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  class FooBar
4
- def self.reset
5
- @@foo = nil
6
- @@footsie = nil
7
- @@bar = nil
8
- @@baz = nil
9
- Mize.cache_clear
4
+ class << self
5
+ def reset
6
+ @@foo = nil
7
+ @@footsie = nil
8
+ @@bar = nil
9
+ @@baz = nil
10
+ @@foo_nil_stored = nil
11
+ @@foo_nil_not_stored = nil
12
+ Mize.cache_clear
13
+ end
14
+
15
+ def foo_nil_stored
16
+ @@foo_nil_stored
17
+ end
18
+
19
+ def foo_nil_not_stored
20
+ @@foo_nil_not_stored
21
+ end
10
22
  end
11
23
 
12
24
  def foo(*a)
@@ -28,6 +40,20 @@ class FooBar
28
40
  end
29
41
  memoize function: :bar
30
42
 
43
+ def foo_nil_stored(*a)
44
+ @@foo_nil_stored ||= 0
45
+ @@foo_nil_stored += 1
46
+ nil
47
+ end
48
+ memoize method: :foo_nil_stored, store_nil: true
49
+
50
+ def foo_nil_not_stored(*a)
51
+ @@foo_nil_not_stored ||= 0
52
+ @@foo_nil_not_stored += 1
53
+ nil
54
+ end
55
+ memoize method: :foo_nil_not_stored, store_nil: false
56
+
31
57
  private
32
58
 
33
59
  def baz(*a)
@@ -101,6 +127,27 @@ describe Mize do
101
127
  expect(fb1.__send__(:footsie, 1, 2)).to eq 1
102
128
  expect(fb1.__send__(:foo, 1, 2)).to eq 2
103
129
  end
130
+
131
+ it 'can store nil' do
132
+ expect(fb1.__send__(:__mize_cache__)).to be_empty
133
+ expect(FooBar.foo_nil_stored).to be_nil
134
+ expect(fb1.foo_nil_stored(1, 2)).to be_nil
135
+ expect(fb1.__send__(:__mize_cache__)).not_to be_empty
136
+ expect(FooBar.foo_nil_stored).to eq 1
137
+ expect(fb1.foo_nil_stored(1, 2)).to be_nil
138
+ expect(FooBar.foo_nil_stored).to eq 1
139
+ end
140
+
141
+ it 'can skip storing nil' do
142
+ expect(fb1.__send__(:__mize_cache__)).to be_empty
143
+ expect(FooBar.foo_nil_not_stored).to be_nil
144
+ expect(fb1.foo_nil_not_stored(1, 2)).to be_nil
145
+ expect(fb1.__send__(:__mize_cache__)).to be_empty
146
+ expect(FooBar.foo_nil_not_stored).to eq 1
147
+ expect(fb1.foo_nil_not_stored(1, 2)).to be_nil
148
+ expect(fb1.__send__(:__mize_cache__)).to be_empty
149
+ expect(FooBar.foo_nil_not_stored).to eq 2
150
+ end
104
151
  end
105
152
 
106
153
  context 'memoize function' do
@@ -4,10 +4,6 @@ if ENV['START_SIMPLECOV'].to_i == 1
4
4
  add_filter "#{File.basename(File.dirname(__FILE__))}/"
5
5
  end
6
6
  end
7
- if ENV['CODECLIMATE_REPO_TOKEN']
8
- require "codeclimate-test-reporter"
9
- CodeClimate::TestReporter.start
10
- end
11
7
  require 'rspec'
12
8
  begin
13
9
  require 'byebug'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Frank
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-23 00:00:00.000000000 Z
11
+ date: 2017-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gem_hadar
@@ -161,7 +161,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
161
161
  version: '0'
162
162
  requirements: []
163
163
  rubyforge_project:
164
- rubygems_version: 2.6.8
164
+ rubygems_version: 2.6.10
165
165
  signing_key:
166
166
  specification_version: 4
167
167
  summary: Library that provides memoziation for methods and functions