memery 0.6.0 → 1.0.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
  SHA256:
3
- metadata.gz: 2592e62955d94ec822ac01546d7914e4a1b1bac3cf767f133c0bb9d2740d797c
4
- data.tar.gz: d55627dd3396a960fb16509986b7843b2507cdc522f04b1c16a1b78c2e367dc8
3
+ metadata.gz: 3a2db1b80dc2da0ad38dea657f09a1f4a029017d5c67610e27a71af3aeb9aef4
4
+ data.tar.gz: b2e8bc1c499d2462096b0b4045c41de110ea8ea27b5750e23cbdf1a74c57d9f9
5
5
  SHA512:
6
- metadata.gz: edbfaefbbfd51dcac0b99a690e065c291f66b20534e5252fa074ee8c7cc83f3256c5cae63abd63f2d1de8c6fb1ca1253e1969ac36c9098ec8fad518d60b39795
7
- data.tar.gz: 50617bebf3b45b586a55d41267835dbbecba826bcb9be8c5a7392b6661a7019eaa1de95745a024c3e437796df7e94537b2affe6942597b5a855bcfa1d3c7c30c
6
+ metadata.gz: 813057c9b895b2b789a75c48ea49366a9a951494e90d07a21a628b46ed2bda35e3f9a8407a5efcbf0bf73712ddaff6002d8685e083d6b767e8c52d3102b65743
7
+ data.tar.gz: 24a8b9ef7c10341336e301f6e9c09644101fd7c593f9535483cdbf8360ac8ac003d94869200303269fb8d3c6b7103675f7691a7c5d46b31acfd74f5f40b19a59
@@ -0,0 +1,9 @@
1
+ root = true
2
+
3
+ [*]
4
+ indent_style = space
5
+ indent_size = 2
6
+ end_of_line = lf
7
+ charset = utf-8
8
+ trim_trailing_whitespace = true
9
+ insert_final_newline = true
data/.gitignore CHANGED
@@ -6,6 +6,7 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ .ruby-version
9
10
 
10
11
  # rspec failure tracking
11
12
  .rspec_status
@@ -4,3 +4,9 @@ inherit_gem:
4
4
  AllCops:
5
5
  DisplayCopNames: true
6
6
  TargetRubyVersion: 2.5
7
+
8
+ Naming/UncommunicativeMethodParamName:
9
+ AllowedNames: ["x", "y", "z"]
10
+
11
+ RSpec/EmptyLineAfterHook:
12
+ Enabled: false
@@ -0,0 +1,24 @@
1
+ ## [Unreleased]
2
+
3
+ ## [1.0.0] - 2018-08-31
4
+ ### Added
5
+ - Add `:condition` option for `.memoize` method ([@AlexWayfer]) [#7]
6
+
7
+ ## [0.6.0] - 2018-04-20
8
+ ### Added
9
+ - Readme example for memoizing class methods. ([@AlexWayfer]) [#3]
10
+ - Memery raises `ArgumentError` if method is not defined when you call `memoize`.
11
+
12
+ ## [0.5.0] - 2017-16-12
13
+ - Initial public version.
14
+
15
+ [0.5.0]: https://github.com/tycooon/memery/tree/v0.5.0
16
+ [0.6.0]: https://github.com/tycooon/memery/compare/v0.5.0...v0.6.0
17
+ [1.0.0]: https://github.com/tycooon/memery/compare/v0.6.0...v1.0.0
18
+ [Unreleased]: https://github.com/tycooon/memery/compare/v0.6.0...HEAD
19
+
20
+ [@tycooon]: https://github.com/tycooon
21
+ [@AlexWayfer]: https://github.com/AlexWayfer
22
+
23
+ [#3]: https://github.com/tycooon/memery/pull/3
24
+ [#7]: https://github.com/tycooon/memery/pull/7
data/Gemfile CHANGED
@@ -2,3 +2,5 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
  gemspec
5
+
6
+ gem "rubocop-config-umbrellio", github: "umbrellio/code-style"
data/README.md CHANGED
@@ -81,6 +81,41 @@ B.call # => 42
81
81
  # Text will be printed only once.
82
82
  ```
83
83
 
84
+ For conditional memoization:
85
+
86
+ ```ruby
87
+ class A
88
+ include Memery
89
+
90
+ attr_accessor :environment
91
+
92
+ def call
93
+ puts "calculating"
94
+ 42
95
+ end
96
+
97
+ memoize :call, condition: -> { environment == 'production' }
98
+ end
99
+
100
+ a = A.new
101
+ a.environment = 'development'
102
+ a.call # => 42
103
+ # calculating
104
+ a.call # => 42
105
+ # calculating
106
+ a.call # => 42
107
+ # calculating
108
+ # Text will be printed every time because result of condition block is `false`.
109
+
110
+ a.environment = 'production'
111
+ a.call # => 42
112
+ # calculating
113
+ a.call # => 42
114
+ a.call # => 42
115
+ # Text will be printed only once because there is memoization
116
+ # with `true` result of condition block.
117
+ ```
118
+
84
119
  ## Difference with other gems
85
120
  Memery is very similar to [Memoist](https://github.com/matthewrudy/memoist). The difference is that it doesn't override methods, instead it uses Ruby 2 `Module.prepend` feature. This approach is cleaner and it allows subclasses' methods to work properly: if you redefine a memoized method in a subclass, it's not memoized by default, but you can memoize it normally (without using awkward `identifier: ` argument) and it will just work:
86
121
 
@@ -136,6 +171,9 @@ a.users {}
136
171
 
137
172
  However, this solution is kind of hacky.
138
173
 
174
+ ## Contributing
175
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tycooon/memery.
176
+
139
177
  ## License
140
178
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
141
179
 
@@ -20,9 +20,9 @@ module Memery
20
20
  end
21
21
 
22
22
  module ClassMethods
23
- def memoize(method_name)
23
+ def memoize(method_name, condition: nil)
24
24
  prepend_memery_module!
25
- define_memoized_method!(method_name)
25
+ define_memoized_method!(method_name, condition: condition)
26
26
  end
27
27
 
28
28
  private
@@ -33,14 +33,16 @@ module Memery
33
33
  prepend @_memery_module
34
34
  end
35
35
 
36
- def define_memoized_method!(method_name)
36
+ def define_memoized_method!(method_name, condition: nil)
37
37
  mod_id = @_memery_module.object_id
38
38
  visibility = Memery.method_visibility(self, method_name)
39
39
  raise ArgumentError, "Method #{method_name} is not defined on #{self}" unless visibility
40
40
 
41
41
  @_memery_module.module_eval do
42
42
  define_method(method_name) do |*args, &block|
43
- return super(*args, &block) if block
43
+ if block || (condition && !instance_exec(&condition))
44
+ return super(*args, &block)
45
+ end
44
46
 
45
47
  @_memery_memoized_values ||= {}
46
48
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Memery
4
- VERSION = "0.6.0"
4
+ VERSION = "1.0.0"
5
5
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- lib = File.expand_path("../lib", __FILE__)
3
+ lib = File.expand_path("lib", __dir__)
4
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
  require "memery/version"
6
6
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: memery
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuri Smirnov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-20 00:00:00.000000000 Z
11
+ date: 2018-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -115,10 +115,12 @@ executables: []
115
115
  extensions: []
116
116
  extra_rdoc_files: []
117
117
  files:
118
+ - ".editorconfig"
118
119
  - ".gitignore"
119
120
  - ".rspec"
120
121
  - ".rubocop.yml"
121
122
  - ".travis.yml"
123
+ - CHANGELOG.md
122
124
  - Gemfile
123
125
  - LICENSE.txt
124
126
  - README.md
@@ -146,7 +148,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
148
  version: '0'
147
149
  requirements: []
148
150
  rubyforge_project:
149
- rubygems_version: 2.7.6
151
+ rubygems_version: 2.7.7
150
152
  signing_key:
151
153
  specification_version: 4
152
154
  summary: A gem for memoization.