memoized_inflectors 1.0.0.pre.beta.2 → 1.0.0.pre.beta.3

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
  SHA1:
3
- metadata.gz: d8f52803c9ce34f7ba6240f090fb715457b09639
4
- data.tar.gz: 492c4a7d13166a9cb3a36fd6419ac22a8b7e912b
3
+ metadata.gz: d75ec47e89b1a793c642361840b36abd715bc591
4
+ data.tar.gz: 71890ca96c1bb6cc8d268250fb1e289bbf162399
5
5
  SHA512:
6
- metadata.gz: 4b60e5f073cc48607e21207138d6ac0f9c9f0e02cb94d4841bfc9f21528239a250e4e1f5d8c0ed512e7fadcf9a7e8f258d500fd3b68242afe41f7be349367328
7
- data.tar.gz: 042e921b89e2ad34ca6cb3469fc4325d9acfdd05bc26e05e50c7ef6a9fcc10f2d554bb927afe15537665bd2245c35bc2940474f33dc7f95d3cfcd483199e07c1
6
+ metadata.gz: 872c46644d278547a6b51f1eab7be2869b488d80b280d3ed094a4270c7e697ed5a06ba266ea891153f4ea58b3c7756a35f1fdfa29d5b1bbca949f41473db2b81
7
+ data.tar.gz: d3ec9a2c722ad461483903b934a3b4d6871144786cf5288b71042458e6a2dc652144999575c6b8633f933e6138aac94a1874d34d417cf0d1352c9ab4a1eae528
@@ -8,24 +8,48 @@ module MemoizedInflectors
8
8
  pluralize singularize tableize titleize
9
9
  to_param underscore
10
10
  ].freeze,
11
- *DUP_UNSAFE_INFLECTORS = %i[constantize safe_constantize].freeze
11
+ *CLASS_INFLECTORS = %i[constantize safe_constantize].freeze # Inflectors which return class objects need to be specially handled.
12
12
  ]
13
13
 
14
+ def self.sanitize_for_key(s, inflector)
15
+ # the class inflectors do not differentiate between names beginning with the
16
+ # root namespace prefix (::) and those without.
17
+ #
18
+ if CLASS_INFLECTORS.include?(inflector)
19
+ s.sub(/\A:*/, "")
20
+ else
21
+ s
22
+ end
23
+ end
24
+
25
+ # TODO: Create separate methods for class inflectors??? *
14
26
  # Define an instance method for each inflector, e.g. `signularize`, `constantize`, etc.
15
27
  (INFLECTORS | ::ActiveSupport::Inflector.instance_methods).each do |inflector|
16
28
  define_method(inflector) do |*args|
17
29
  return super(*args) if ::MemoizedInflectors.disabled
18
30
  cache = ::MemoizedInflectors.cache_for(inflector)
19
- key = ::MemoizedInflectors.key_for(self, *args)
31
+ key = ::MemoizedInflectors.key_for(
32
+ ::MemoizedInflectors::StringMethods.sanitize_for_key(self, inflector),
33
+ *args
34
+ )
20
35
 
21
36
  result =
22
37
  if cache.has_key?(key)
23
38
  cache[key]
24
39
  else
25
- cache[key] = super(*args)
40
+ # Result is not cached. First fetch the result by calling the super method.
41
+ result = super(*args)
42
+
43
+ # Cache the result unless the result is nil and the inflector result is
44
+ # expected to be a class. The reason for the exception is that return
45
+ # value of `safe_constantize` may change from `nil` to another value.
46
+ # If constants are removed then it is recommended that you clear the
47
+ # caches for `constantize` and `safe_constantize`
48
+ cache[key] = result unless result.nil? && CLASS_INFLECTORS.include?(inflector)
26
49
  end
27
50
 
28
- (DUP_UNSAFE_INFLECTORS.include?(inflector) || result.nil?) ? result : result.dup
51
+ # It is not safe to dup classes and not possible to dup nil.
52
+ (CLASS_INFLECTORS.include?(inflector) || result.nil?) ? result : result.dup
29
53
  end
30
54
  end
31
55
  end
@@ -1,3 +1,3 @@
1
1
  module MemoizedInflectors
2
- VERSION = "1.0.0-beta.2"
2
+ VERSION = "1.0.0-beta.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: memoized_inflectors
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.pre.beta.2
4
+ version: 1.0.0.pre.beta.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - WizardOfOgz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-18 00:00:00.000000000 Z
11
+ date: 2016-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport