memoit 0.2.0 → 0.4.1

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: f8ece57c9bea6440805f8ccd28b6bae60c977d40
4
- data.tar.gz: cd5fd4c30fd3c6a9fa040c6a85510a88b8fbd00d
2
+ SHA256:
3
+ metadata.gz: '068ad78c5ca6426134080648451b94d4243130e39e985b3cc98edae3044a5f20'
4
+ data.tar.gz: e498ebe3a522f57a0fcfdd4e0ea7e1ccb096ec26adb6f73dce0badc1ee7cf935
5
5
  SHA512:
6
- metadata.gz: 4091ce16342522f9321735939e83d78f17f401c77480fb572c2d5b20de87f94ee58db4f6b0d1ca87a9c207b0df4a1eed1f32a738596a9e43c895dada06b05b99
7
- data.tar.gz: cbe78faf3d28a6d100f14df8c90994170ded87f95991e7c8a607f4593d36ff13cd9ac8e5ce9063589d8d654847e015be3540bc5a20dad0d7caac996351517b77
6
+ metadata.gz: be5728d2629e1a192a3e6f89f089159f0fad2eadc778213836474fa52688e509414b2b0b048e38665e2c267dc30ac3e8669d4edf27cfdae64a4f81dc6b465156
7
+ data.tar.gz: 2fd4ae72e455d1e29f43451c9bbde94aa9381fcc02c9c5843628db3a6eb9fcf00a0623a55a683f90d8bad75edb30d52064ea9d7c929fff53b0a6ae65eba28636
@@ -0,0 +1,28 @@
1
+ name: Ruby CI
2
+
3
+ on:
4
+ push:
5
+ pull_request:
6
+
7
+ jobs:
8
+ test:
9
+
10
+ runs-on: ubuntu-latest
11
+
12
+ strategy:
13
+ matrix:
14
+ ruby-version:
15
+ - 3.1
16
+ - "3.0"
17
+ - 2.7
18
+ - jruby-9.3
19
+
20
+ steps:
21
+ - uses: actions/checkout@v3
22
+ - name: Set up Ruby ${{ matrix.ruby-version }}
23
+ uses: ruby/setup-ruby@v1
24
+ with:
25
+ ruby-version: ${{ matrix.ruby-version }}
26
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
27
+ - name: Run tests
28
+ run: bundle exec rake
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Memoit
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/memoit.svg)](http://badge.fury.io/rb/memoit)
4
+ [![Build Status](https://github.com/jnicklas/memoit/actions/workflows/ci.yml/badge.svg)](https://github.com/jnicklas/memoit/actions/workflows/ci.yml)
5
+
3
6
  Memoizes methods.
4
7
 
5
8
  ## Installation
@@ -28,6 +31,14 @@ end
28
31
 
29
32
  [Yes](https://news.ycombinator.com/item?id=3067434).
30
33
 
34
+ ## Development
35
+
36
+ ```sh
37
+ gem install bundler
38
+ bundle install
39
+ rspec
40
+ ```
41
+
31
42
  ## License
32
43
 
33
- [MIT](License.txt)
44
+ [MIT](LICENSE.txt)
data/Rakefile CHANGED
@@ -1,2 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
2
3
 
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -1,3 +1,3 @@
1
1
  module Memoit
2
- VERSION = "0.2.0"
2
+ VERSION = "0.4.1"
3
3
  end
data/lib/memoit.rb CHANGED
@@ -11,12 +11,13 @@ module Memoit
11
11
  # end
12
12
  # end
13
13
  def memoize(name)
14
- ivar_name = "@_memo_#{name}".to_sym
14
+ ivar_method_name = name.to_s.sub("?", "__questionmark").sub("!", "__bang")
15
+ ivar_name = "@_memo_#{ivar_method_name}".to_sym
15
16
  mod = Module.new do
16
- define_method(name) do |*args, &block|
17
- return super(*args, &block) if block
17
+ define_method(name) do |*args, **kwargs, &block|
18
+ return super(*args, **kwargs, &block) if block
18
19
  cache = instance_variable_get(ivar_name) || instance_variable_set(ivar_name, {})
19
- cache.fetch(args.hash) { |hash| cache[hash] = super(*args) }
20
+ cache.fetch([args, kwargs].hash) { |hash| cache[hash] = super(*args, **kwargs) }
20
21
  end
21
22
  end
22
23
  prepend mod
data/memoit.gemspec CHANGED
@@ -13,13 +13,12 @@ Gem::Specification.new do |spec|
13
13
  spec.license = "MIT"
14
14
 
15
15
  spec.files = `git ls-files -z`.split("\x0")
16
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
16
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
17
  spec.require_paths = ["lib"]
19
18
 
20
19
  spec.required_ruby_version = ">= 2.1.0"
21
20
 
22
- spec.add_development_dependency "bundler", "~> 1.7"
23
- spec.add_development_dependency "rake", "~> 10.0"
24
- spec.add_development_dependency "rspec", "~> 3.0"
21
+ spec.add_development_dependency "bundler", "~> 2.3"
22
+ spec.add_development_dependency "rake", "~> 13.0"
23
+ spec.add_development_dependency "rspec", "~> 3.10"
25
24
  end
data/spec/memoit_spec.rb CHANGED
@@ -15,10 +15,38 @@ describe Memoit do
15
15
  rand
16
16
  end
17
17
 
18
+ memoize def baz(pos = nil, hash = {}, kwak: nil)
19
+ rand
20
+ end
21
+
22
+ memoize def qux(pos = nil, kwak: nil)
23
+ rand
24
+ end
25
+
26
+ memoize def quux(kwak: nil)
27
+ rand
28
+ end
29
+
30
+ memoize def corge(hash = {}, kwak: nil)
31
+ rand
32
+ end
33
+
18
34
  memoize def falsy
19
35
  foo
20
36
  false
21
37
  end
38
+
39
+ memoize def query?
40
+ rand
41
+ end
42
+
43
+ memoize def bang!
44
+ rand
45
+ end
46
+
47
+ memoize def ☃
48
+ rand
49
+ end
22
50
  end
23
51
  end
24
52
  let(:instance) { klass.new }
@@ -37,6 +65,28 @@ describe Memoit do
37
65
  expect(instance.bar(a, 1, :foo, "bar")).not_to eq(instance.bar(Object.new, 1, :foo, "bar"))
38
66
  end
39
67
 
68
+ it "caches results when positional, hash and keyword arguments are used" do
69
+ a = Object.new
70
+ expect(instance.baz(a, { hash_key: "hash_value" }, kwak: "kwav")).to eq(instance.baz(a, { hash_key: "hash_value" }, kwak: "kwav"))
71
+ end
72
+
73
+ it "caches results when positional and keyword arguments are used" do
74
+ a = Object.new
75
+ expect(instance.qux(a, kwak: "kwav")).to eq(instance.qux(a, kwak: "kwav"))
76
+ end
77
+
78
+ it "caches results when keyword arguments are used" do
79
+ expect(instance.quux(kwak: "kwav")).to eq(instance.quux(kwak: "kwav"))
80
+ end
81
+
82
+ it "ignores cache if keyword arguments differ" do
83
+ expect(instance.quux(kwak: "1")).not_to eq(instance.quux(kwak: "2"))
84
+ end
85
+
86
+ it "caches results when hash and keyword arguments are used" do
87
+ expect(instance.corge({ hash_key: "hash_value" }, kwak: "kwav")).to eq(instance.corge({ hash_key: "hash_value" }, kwak: "kwav"))
88
+ end
89
+
40
90
  it "ignores cache when block given" do
41
91
  expect(instance.foo { }).not_to eq(instance.foo { })
42
92
  end
@@ -46,6 +96,18 @@ describe Memoit do
46
96
  expect(instance.falsy).to eq(instance.falsy)
47
97
  end
48
98
 
99
+ it "handles question-mark methods" do
100
+ expect(instance.query?).to eq(instance.query?)
101
+ end
102
+
103
+ it "handles bang methods" do
104
+ expect(instance.bang!).to eq(instance.bang!)
105
+ end
106
+
107
+ it "handles non-ASCII-name methods" do
108
+ expect(instance.☃).to eq(instance.☃)
109
+ end
110
+
49
111
  it "returns the name of the method" do
50
112
  name = nil
51
113
  Class.new do
@@ -54,6 +116,25 @@ describe Memoit do
54
116
  end
55
117
  expect(name).to eq(:blah)
56
118
  end
119
+
120
+ it "works in a mixin" do
121
+ mod = Module.new do
122
+ memoize def cname
123
+ self.class.name
124
+ end
125
+ end
126
+
127
+ Foo = Class.new do
128
+ include mod
129
+ end
130
+
131
+ Bar = Class.new do
132
+ include mod
133
+ end
134
+
135
+ expect(Foo.new.cname).to eq("Foo")
136
+ expect(Bar.new.cname).to eq("Bar")
137
+ end
57
138
  end
58
139
 
59
140
  describe ".memoize_class_method" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: memoit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Nicklas
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-09 00:00:00.000000000 Z
11
+ date: 2022-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,49 +16,50 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.7'
19
+ version: '2.3'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.7'
26
+ version: '2.3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '10.0'
33
+ version: '13.0'
34
34
  type: :development
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: '10.0'
40
+ version: '13.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '3.0'
47
+ version: '3.10'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '3.0'
55
- description:
54
+ version: '3.10'
55
+ description:
56
56
  email:
57
57
  - jonas.nicklas@gmail.com
58
58
  executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
+ - ".github/workflows/ci.yml"
62
63
  - ".gitignore"
63
64
  - Gemfile
64
65
  - LICENSE.txt
@@ -72,7 +73,7 @@ homepage: ''
72
73
  licenses:
73
74
  - MIT
74
75
  metadata: {}
75
- post_install_message:
76
+ post_install_message:
76
77
  rdoc_options: []
77
78
  require_paths:
78
79
  - lib
@@ -87,11 +88,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
88
  - !ruby/object:Gem::Version
88
89
  version: '0'
89
90
  requirements: []
90
- rubyforge_project:
91
- rubygems_version: 2.4.5
92
- signing_key:
91
+ rubygems_version: 3.3.7
92
+ signing_key:
93
93
  specification_version: 4
94
94
  summary: Memoize is back!
95
95
  test_files:
96
96
  - spec/memoit_spec.rb
97
- has_rdoc: