memoize_method 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 625db8efd68673a84c9b1f7f5acd20dcc0cd1a2c
4
+ data.tar.gz: aec37329d8f3b6c7ad748147503f7e673a8aa537
5
+ SHA512:
6
+ metadata.gz: a08d034547a8cbc1fd48d78ea8426e24ab1a221c36fb8e77a9c59674f721a3daf2549172fdddcfe85508e0d320b934ebb91cc623fb5bf089e85a153b8b8589e5
7
+ data.tar.gz: 4b0c739fc9ef0cf11ad39fe882226d6c2d034c2f75ec76ea872ce7eb5d2fa7212d35eb64a5b5222fd43b76b75ffbf982d66818127421ab1c70a3d4f5e4c18afb
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in memoize_method.gemspec
4
+ gemspec
@@ -0,0 +1,89 @@
1
+ # MemoizeMethod
2
+
3
+ Memoize Method lets you memoize the return value of methods methods with a single word.
4
+
5
+ ```ruby
6
+ cache def my_expensive_computation
7
+ ...
8
+ end
9
+ ```
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'memoize_method'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install memoize_method
26
+
27
+ ## Usage
28
+
29
+ You are going to want to extend MemoizeMethod in your class
30
+
31
+ ```ruby
32
+ class A
33
+ extend MemoizeMethod
34
+ end
35
+
36
+ irb> A.methods - Object.methods
37
+ => [:cache]
38
+ ```
39
+
40
+ Now lets cache our method.
41
+
42
+ ```ruby
43
+ class A
44
+ extend MemoizeMethod
45
+ cache def foo
46
+ Time.now
47
+ end
48
+ end
49
+
50
+ irb> A.new.methods - Object.new.methods
51
+ => [:foo, :cacheless_foo, :recompute_foo]
52
+ ```
53
+
54
+ Now you have 3 methods where you defined `foo`,
55
+ - `foo` is will compute the first time, then cache after that.
56
+ - `cacheless_foo` lets you get at the current value without busting the cache.
57
+ - `recompute_foo` will bust the cache and assign it to the new value.
58
+
59
+ Example:
60
+ ```ruby
61
+ irb> a = A.new
62
+ => #<A:0x007f98212599f8>
63
+ irb> a.foo
64
+ => 2015-11-15 10:38:09 -0800
65
+ irb> a.foo
66
+ => 2015-11-15 10:38:09 -0800
67
+ irb> a.foo == a.foo
68
+ => true
69
+ irb> a.cacheless_foo
70
+ => 2015-11-15 10:38:41 -0800
71
+ irb> a.cacheless_foo != a.foo
72
+ => true
73
+ irb(main):018:0> a.recompute_foo
74
+ => 2015-11-15 10:41:01 -0800
75
+ irb(main):019:0> a.foo
76
+ => 2015-11-15 10:41:01 -0800
77
+ irb(main):020:0> a.recompute_foo == a.foo
78
+ => true
79
+ ```
80
+
81
+ ## Development
82
+
83
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
84
+
85
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
86
+
87
+ ## Contributing
88
+
89
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hparker/memoize_method.
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "memoize_method"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,18 @@
1
+ require "memoize_method/version"
2
+
3
+ module MemoizeMethod
4
+ def cache(method)
5
+ cacheless_method = "cacheless_" + method.to_s
6
+ recompute_method = "recompute_" + method.to_s
7
+ cache_var = "@" + method.to_s + "_cache"
8
+ alias_method cacheless_method, method
9
+ define_method(method) { |*args|
10
+ instance_eval(cache_var) ||
11
+ instance_eval("#{cache_var} ||= send(cacheless_method, *args)")
12
+ }
13
+
14
+ define_method(recompute_method) { |*args|
15
+ instance_eval("#{cache_var} = send(cacheless_method, *args)")
16
+ }
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module MemoizeMethod
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'memoize_method/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "memoize_method"
8
+ spec.version = MemoizeMethod::VERSION
9
+ spec.authors = ["Adam Hess"]
10
+ spec.email = ["adamhess1991@gmail.com"]
11
+
12
+ spec.summary = %q{allow simple caching of ruby methods.}
13
+ spec.description = %q{will expose your method, which is cached, a cacheless version and a recompute version}
14
+ spec.homepage = "https://github.com/HParker/ruby_memoize_method"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: memoize_method
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Hess
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: will expose your method, which is cached, a cacheless version and a recompute
56
+ version
57
+ email:
58
+ - adamhess1991@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/memoize_method.rb
71
+ - lib/memoize_method/version.rb
72
+ - memoize_method.gemspec
73
+ homepage: https://github.com/HParker/ruby_memoize_method
74
+ licenses: []
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.4.5.1
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: allow simple caching of ruby methods.
96
+ test_files: []