michie 0.1.0 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1f6ef1a836a13383202e735dd16366a9e171183bf8405f25bfb812d68560155b
4
- data.tar.gz: c343ea5eb201adaea073ac2406a87d0a570b2adc7c93d93c8ace4473ae9e118c
3
+ metadata.gz: 40d6d7b81b0093d54b3a6f19f1c0581fa5f4033370a68824ae06a82e4dc4e6b9
4
+ data.tar.gz: 3799a5eab4afe92769e1e7e83c2de26f3ce3b96df4880befd51d084a5b43b36e
5
5
  SHA512:
6
- metadata.gz: 0162b216d0e66f5ea0ef7d4925f51063bc7cd73b48e02444fa9bdd42e9a29db045d7dc5a84c7a0c4a8dd4495fcbd03c471eed860d0628778f83d6afbf6011e95
7
- data.tar.gz: 79eeae6b562a5973534ce7d80aaa1f2385e72311c67a87ebe1abc30ef201b32a08864296dc8060f6de1d451f7b2c263a24e0768aa96784f9ee4d8197033cc51b
6
+ metadata.gz: 8e4d1e6eef0910a3754c32bc98422fb0e797f3295e138fa09e7d891f219ceca4cad73fb8e9ce5dd529246855cd805d3171a33b17663de66b86e4f6c96eddc206
7
+ data.tar.gz: 4d65ac8ec4d97603fcf8d4db99119b3f40a1860637f9e677383dc1a1bcdcf2dbcf39f22bd1d23ae4a59dc7bc800c21602747233b57e3d59573b7f809c5a3c1ee
data/CHANGELOG.md ADDED
@@ -0,0 +1,12 @@
1
+ # Michie Changelog
2
+
3
+ ## 0.x
4
+
5
+ ### 0.2.0
6
+
7
+ - Support passing method names to `memoize`
8
+ - Use method type prefix to avoid instance variable name collisions
9
+
10
+ ### 0.1.0
11
+
12
+ Initial release.
data/README.md CHANGED
@@ -1,8 +1,15 @@
1
1
  # Michie
2
2
 
3
- Michie (pronounced /ˈmɪki/, like “Mickey”) memoizes methods defined in a block.
4
- Unlike other meomization libraries, Michie encapsulates its memoization in
5
- a single module which it prepends over the original method.
3
+ [![Gem Version](https://badge.fury.io/rb/michie.svg)][gem]
4
+ [![Build Status](https://github.com/shioyama/michie/actions/workflows/ruby.yml/badge.svg)][actions]
5
+
6
+ [gem]: https://rubygems.org/gems/michie
7
+ [actions]: https://github.com/shioyama/michie/actions
8
+
9
+ Michie (pronounced /ˈmɪki/, like “Mickey”) memoizes methods either passed by
10
+ method name or defined in a block. Unlike other meomization libraries, Michie
11
+ encapsulates its memoization in a single module which it prepends over the
12
+ original method.
6
13
 
7
14
  ## Usage
8
15
 
@@ -15,10 +22,26 @@ class BillingApi
15
22
 
16
23
  memoize do
17
24
  def fetch_aggregate_data
18
- # returns all data from remote server
19
- end
25
+ # returns all data from remote server
26
+ end
20
27
  end
21
28
  end
29
+ ```
30
+
31
+ Alternatively, you can pass the method name(s) to `memoize` after they have
32
+ been defined:
33
+
34
+ ```ruby
35
+ class BillingApi
36
+ extend Michie
37
+
38
+ def fetch_aggregate_data
39
+ # returns all data from remote server
40
+ end
41
+ memoize :fetch_aggregate_data
42
+ end
43
+ ```
44
+
22
45
 
23
46
  api = BillingApi.new
24
47
  api.fetch_aggregate_data
@@ -56,9 +79,12 @@ def fetch_aggregate_data
56
79
  end
57
80
  ```
58
81
 
59
- By default, Michie prepends `__michie_` to the method name to generate the
60
- instance variable name. This can be changed by passing a `prefix` option to
61
- `memoize` (see specs for details).
82
+ By default, Michie generates an instance variable name prefix combining a
83
+ "base" string `__michie_` with either an `m_` (normal methods), `b_` (bang
84
+ methods ending in `!`) or `q_` (query methods ending in `?`). This prefix is
85
+ combined with the method name of the method to be memoized to generate the
86
+ instance variable name. The base prefix can be changed by passing a `prefix`
87
+ option to `memoize` (see specs for details).
62
88
 
63
89
  Since Michie uses the presence of an instance variable to signal memoization,
64
90
  `false` and `nil` values can be memoized (unlike techniques which use `||=`).
@@ -74,8 +100,8 @@ class BillingApi
74
100
 
75
101
  memoize(eager: true) do
76
102
  def fetch_aggregate_data
77
- # returns all data from remote server
78
- end
103
+ # returns all data from remote server
104
+ end
79
105
  end
80
106
  end
81
107
 
@@ -1,3 +1,3 @@
1
1
  module Michie
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/michie.rb CHANGED
@@ -4,10 +4,16 @@ require "michie/version"
4
4
  module Michie
5
5
  DEFAULT_IVAR_PREFIX = "__michie"
6
6
 
7
- def memoize(eager: false, prefix: DEFAULT_IVAR_PREFIX, &block)
8
- methods_before = instance_methods(false) + private_instance_methods(false)
9
- block.call
10
- methods_to_memoize = instance_methods(false) + private_instance_methods(false) - methods_before
7
+ def memoize(*args, eager: false, prefix: DEFAULT_IVAR_PREFIX)
8
+ if block_given? && !args.empty?
9
+ raise ArgumentError, "memoize takes method names or a block defining methods, not both."
10
+ elsif block_given?
11
+ methods_before = instance_methods(false) + private_instance_methods(false)
12
+ yield
13
+ methods_to_memoize = instance_methods(false) + private_instance_methods(false) - methods_before
14
+ elsif !args.empty?
15
+ methods_to_memoize = args
16
+ end
11
17
 
12
18
  memoizer = eager ? EagerMemoizer : Memoizer
13
19
 
@@ -67,13 +73,15 @@ module Michie
67
73
 
68
74
  module Helpers
69
75
  def ivar_name(method_name, memoization_prefix)
70
- string = "#{memoization_prefix}_#{method_name.to_s}"
76
+ string = String === method_name ? method_name.dup : method_name.to_s
77
+ method_type_prefix = "m"
71
78
 
72
- if string.end_with?("?", "!")
73
- string = string.dup
74
- string.sub!(/\?\Z/, "_query") || string.sub!(/!\Z/, "_bang")
79
+ if string.chomp!("?")
80
+ method_type_prefix = "q"
81
+ elsif string.chomp!("!")
82
+ method_type_prefix = "b"
75
83
  end
76
- "@#{string}"
84
+ "@#{memoization_prefix}_#{method_type_prefix}_#{string}"
77
85
  end
78
86
  module_function :ivar_name
79
87
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: michie
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Salzberg
@@ -45,6 +45,7 @@ executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
+ - CHANGELOG.md
48
49
  - Gemfile
49
50
  - Gemfile.lock
50
51
  - LICENSE.txt