Class: Mutils::Lib::Helper

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/mutils/lib/helper.rb

Overview

Helper: caching expensive repetitive operations

Instance Method Summary collapse

Constructor Details

#initializeHelper

Returns a new instance of Helper.



11
12
13
14
15
16
# File 'lib/mutils/lib/helper.rb', line 11

def initialize
  self.inflector_object = Dry::Inflector.new
  self.pluralize_cache = {}
  self.underscore_cache = {}
  self.constantize_cache = {}
end

Instance Method Details

#collection?(object) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/mutils/lib/helper.rb', line 33

def collection?(object)
  object.respond_to?(:size) && !object.respond_to?(:each_pair)
end

#constantize(string) ⇒ Object



28
29
30
31
# File 'lib/mutils/lib/helper.rb', line 28

def constantize(string)
  constantize_cache[string] = Object.const_get string unless constantize_cache[string]
  constantize_cache[string]
end

#pluralize(string) ⇒ Object



23
24
25
26
# File 'lib/mutils/lib/helper.rb', line 23

def pluralize(string)
  pluralize_cache[string] = inflector_object.pluralize string unless pluralize_cache[string]
  pluralize_cache[string]
end

#underscore(string) ⇒ Object



18
19
20
21
# File 'lib/mutils/lib/helper.rb', line 18

def underscore(string)
  underscore_cache[string] = inflector_object.underscore string unless underscore_cache[string]
  underscore_cache[string]
end