memoized_inflectors 0.0.2 → 1.0.0.pre.beta.2

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: 87e05981dfe99775911f9ab2c7d688366a2a99d9
4
- data.tar.gz: c1c8c5dcdc13446e965b8b8d48d39c84a4bdceaa
3
+ metadata.gz: d8f52803c9ce34f7ba6240f090fb715457b09639
4
+ data.tar.gz: 492c4a7d13166a9cb3a36fd6419ac22a8b7e912b
5
5
  SHA512:
6
- metadata.gz: 675b0cd421bdaa214da1c86d8040cbbed909f9d4c8afb83f5abd4121fb32663ef165e6c236bff64dd67bb46ea72f282d8f33d65a5981987d3fead7bd2d83cbbf
7
- data.tar.gz: 5b8266a118bf1a615c330f22b2dbded9b0eb8dc8b30ac2b0096d0d5e9f6eb3359ed1f2f5981732874327806ef3dd92d9fc15d4efa6111cf53f31e19899ea77a3
6
+ metadata.gz: 4b60e5f073cc48607e21207138d6ac0f9c9f0e02cb94d4841bfc9f21528239a250e4e1f5d8c0ed512e7fadcf9a7e8f258d500fd3b68242afe41f7be349367328
7
+ data.tar.gz: 042e921b89e2ad34ca6cb3469fc4325d9acfdd05bc26e05e50c7ef6a9fcc10f2d554bb927afe15537665bd2245c35bc2940474f33dc7f95d3cfcd483199e07c1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014-2016 Andrew Ogzewalla
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,22 @@
1
+ module MemoizedInflectors
2
+ module IntegerMethods
3
+ INFLECTORS = %i[ordinalize ordinal].freeze
4
+
5
+ # Define an instance method for each inflector, e.g. `signularize`, `constantize`, etc.
6
+ (INFLECTORS | ::ActiveSupport::Inflector.instance_methods).each do |inflector|
7
+ define_method(inflector) do |*args|
8
+ return super(*args) if ::MemoizedInflectors.disabled
9
+ cache = ::MemoizedInflectors.cache_for(inflector)
10
+ key = ::MemoizedInflectors.key_for(self, *args)
11
+
12
+ if cache.has_key?(key)
13
+ cache[key]
14
+ else
15
+ cache[key] = super(*args)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ ::Integer.send(:prepend, ::MemoizedInflectors::IntegerMethods)
@@ -0,0 +1,34 @@
1
+ module MemoizedInflectors
2
+ module StringMethods
3
+ # TODO: Maintain a default set of inflectors from ActiveSupport and allow users to add or remove from those. *
4
+ INFLECTORS = [
5
+ *%i[
6
+ camelize classify dasherize deconstantize
7
+ demodulize foreign_key humanize parameterize
8
+ pluralize singularize tableize titleize
9
+ to_param underscore
10
+ ].freeze,
11
+ *DUP_UNSAFE_INFLECTORS = %i[constantize safe_constantize].freeze
12
+ ]
13
+
14
+ # Define an instance method for each inflector, e.g. `signularize`, `constantize`, etc.
15
+ (INFLECTORS | ::ActiveSupport::Inflector.instance_methods).each do |inflector|
16
+ define_method(inflector) do |*args|
17
+ return super(*args) if ::MemoizedInflectors.disabled
18
+ cache = ::MemoizedInflectors.cache_for(inflector)
19
+ key = ::MemoizedInflectors.key_for(self, *args)
20
+
21
+ result =
22
+ if cache.has_key?(key)
23
+ cache[key]
24
+ else
25
+ cache[key] = super(*args)
26
+ end
27
+
28
+ (DUP_UNSAFE_INFLECTORS.include?(inflector) || result.nil?) ? result : result.dup
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ ::String.send(:prepend, ::MemoizedInflectors::StringMethods)
@@ -1,3 +1,3 @@
1
1
  module MemoizedInflectors
2
- VERSION = "0.0.2"
2
+ VERSION = "1.0.0-beta.2"
3
3
  end
@@ -1,32 +1,62 @@
1
1
  require "active_support"
2
2
  require "active_support/inflector"
3
3
  require "active_support/core_ext/string"
4
+ require "active_support/core_ext/integer"
5
+ require "lru_redux"
6
+
7
+ require "memoized_inflectors/string_methods"
8
+ require "memoized_inflectors/integer_methods"
4
9
 
5
10
  module MemoizedInflectors
6
- INFLECTOR_METHODS = [
7
- :camelize, :classify, :constantize, :dasherize, :deconstantize, :demodulize, :foreign_key,
8
- :humanize, :inflections, :ordinal, :ordinalize, :parameterize, :pluralize, :safe_constantize,
9
- :singularize, :tableize, :titleize, :transliterate, :underscore
10
- ].freeze
11
-
12
- def self.prepended(klass)
13
- klass.class_eval do
14
- @memoized_inflectors = {}
15
- INFLECTOR_METHODS.each do |inflector_name|
16
- unless instance_variable_get("@#{ inflector_name }")
17
- @memoized_inflectors[inflector_name] = instance_variable_set("@#{ inflector_name }", {})
18
- end
11
+ class << self
12
+ attr_accessor :disabled
13
+ end
14
+
15
+ def self.new_cache_instance
16
+ ::LruRedux::ThreadSafeCache.new(1000) # TODO: allow users to configure the class (e.g. ThreadSafe vs non-ThreadSafe) and max size *
17
+ end
18
+
19
+ def self.caches
20
+ @caches ||= ::Hash.new { |h,k| h[k] = new_cache_instance }
21
+ end
22
+
23
+ # Returns the cache for the given inflector. Currently inflector names must
24
+ # be globally unique which works because there are no name collisions between
25
+ # the String and Integer inflectors.
26
+ #
27
+ #=== Example
28
+ #
29
+ # MemoizedInflectors.cache_for(:classify)
30
+ def self.cache_for(inflector)
31
+ caches[inflector]
32
+ end
33
+
34
+ # Returns a unique key for the given arguments.
35
+ def self.key_for(*args)
36
+ # Using Object#hash rather than MD5 or SHA because it works on arrays, it is faster, and it
37
+ # also works correctly with hashes without manipulation. I.e. it is simply easier. This method
38
+ # works since there is no intention of persisting the cache or sharing it across VM instances.
39
+ args.hash
40
+ end
41
+
42
+ # Clears the cache for the specified inflector(s). If no inflector
43
+ # is specified, then all caches are cleared.
44
+ #
45
+ #=== Examples
46
+ # MemoizedInflectors.clear_cache # Clears everything.
47
+ # MemoizedInflectors.clear_cache(:classify) # Clear only the :classify cache.
48
+ # MemoizedInflectors.clear_cache(:classify, :underscore) # Clears both the :classify and :underscore caches.
49
+ def self.clear_cache(*inflectors)
50
+ if inflectors.any?
51
+ inflectors.each do |inflector|
52
+ cache_for(inflector).clear
19
53
  end
54
+ else
55
+ caches.values.each(&:clear)
20
56
  end
21
57
  end
22
58
 
23
- INFLECTOR_METHODS.each do |inflector_name|
24
- define_method inflector_name do |*args|
25
- memoized_inflections = self.class.instance_variable_get("@#{ inflector_name }")
26
- key = [self, *args].hash
27
- memoized_inflections.has_key?(key) ? memoized_inflections[key] : memoized_inflections[key] = super(*args)
28
- end
59
+ def self.inflector_methods
60
+ ::ActiveSupport::Inflector.instance_methods
29
61
  end
30
62
  end
31
-
32
- String.send(:prepend, MemoizedInflectors)
metadata CHANGED
@@ -1,42 +1,77 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: memoized_inflectors
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 1.0.0.pre.beta.2
5
5
  platform: ruby
6
6
  authors:
7
- - Andy Ogzewalla
7
+ - WizardOfOgz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-19 00:00:00.000000000 Z
11
+ date: 2016-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '4.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '6'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - ~>
27
+ - - ">="
25
28
  - !ruby/object:Gem::Version
26
29
  version: '4.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '6'
33
+ - !ruby/object:Gem::Dependency
34
+ name: lru_redux
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.1'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.1'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '3.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '3.0'
27
61
  description: |
28
- Memoizes inflected strings.
62
+ Memoizes ActiveSupport inflector methods.
29
63
  email:
30
64
  - andyogzewalla@gmail.com
31
65
  executables: []
32
66
  extensions: []
33
67
  extra_rdoc_files: []
34
68
  files:
35
- - lib/memoized_inflectors/version.rb
36
- - lib/memoized_inflectors.rb
37
- - lib/tasks/memoized_inflectors_tasks.rake
38
- - MIT-LICENSE
69
+ - LICENSE.txt
39
70
  - Rakefile
71
+ - lib/memoized_inflectors.rb
72
+ - lib/memoized_inflectors/integer_methods.rb
73
+ - lib/memoized_inflectors/string_methods.rb
74
+ - lib/memoized_inflectors/version.rb
40
75
  homepage: https://github.com/WizardOfOgz/memoized_inflectors
41
76
  licenses:
42
77
  - MIT
@@ -47,18 +82,18 @@ require_paths:
47
82
  - lib
48
83
  required_ruby_version: !ruby/object:Gem::Requirement
49
84
  requirements:
50
- - - '>='
85
+ - - ">="
51
86
  - !ruby/object:Gem::Version
52
87
  version: '0'
53
88
  required_rubygems_version: !ruby/object:Gem::Requirement
54
89
  requirements:
55
- - - '>='
90
+ - - ">"
56
91
  - !ruby/object:Gem::Version
57
- version: '0'
92
+ version: 1.3.1
58
93
  requirements: []
59
94
  rubyforge_project:
60
- rubygems_version: 2.0.4
95
+ rubygems_version: 2.4.5.1
61
96
  signing_key:
62
97
  specification_version: 4
63
- summary: Memoizes inflected strings.
98
+ summary: Memoizes ActiveSupport inflector methods.
64
99
  test_files: []
data/MIT-LICENSE DELETED
@@ -1,20 +0,0 @@
1
- Copyright 2014 Andrew Ogzewalla
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :memoized_inflectors do
3
- # # Task goes here
4
- # end