Module: Iriq::Inflector
- Defined in:
- lib/iriq/inflector.rb
Overview
Singularization with a swappable adapter.
By default uses ActiveSupport’s inflector if it can be required, otherwise falls back to BuiltinAdapter. Override globally:
Iriq::Inflector.adapter = MyAdapter # must respond to .singularize(String)
And reset to default with ‘Iriq::Inflector.reset_adapter!`.
Defined Under Namespace
Modules: ActiveSupportAdapter, BuiltinAdapter
Constant Summary collapse
- CACHE_MAX =
Vocabulary is bounded in practice; cache + cap matches the SegmentClassifier strategy.
10_000
Class Method Summary collapse
- .adapter ⇒ Object
- .adapter=(value) ⇒ Object
- .default_adapter ⇒ Object
- .reset_adapter! ⇒ Object
- .singularize(word) ⇒ Object
Class Method Details
.adapter ⇒ Object
27 28 29 |
# File 'lib/iriq/inflector.rb', line 27 def adapter @adapter ||= default_adapter end |
.adapter=(value) ⇒ Object
31 32 33 34 |
# File 'lib/iriq/inflector.rb', line 31 def adapter=(value) @adapter = value @cache = {} # different adapter could singularize differently end |
.default_adapter ⇒ Object
41 42 43 44 45 46 |
# File 'lib/iriq/inflector.rb', line 41 def default_adapter require "active_support/inflector" ActiveSupportAdapter rescue LoadError BuiltinAdapter end |
.reset_adapter! ⇒ Object
36 37 38 39 |
# File 'lib/iriq/inflector.rb', line 36 def reset_adapter! @adapter = nil @cache = {} end |
.singularize(word) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/iriq/inflector.rb', line 18 def singularize(word) cache = (@cache ||= {}) cached = cache[word] return cached if cached cache.clear if cache.size >= CACHE_MAX cache[word] = adapter.singularize(word) end |