Sha256: 306bacccaa4313ba4cfb4d506b63856d22b50060f60ff86d4d1f455977d7a87d

Contents?: true

Size: 1.1 KB

Versions: 20

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

require 'singleton'
# module Mutils
module Mutils
  module Lib
    # Helper: caching expensive repetitive operations
    class Helper
      include Singleton

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

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

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

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

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

      private

      attr_accessor :inflector_object, :pluralize_cache, :underscore_cache, :constantize_cache
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
mutils-1.3.3 lib/mutils/lib/helper.rb
mutils-1.3.2 lib/mutils/lib/helper.rb
mutils-1.3.1 lib/mutils/lib/helper.rb
mutils-1.3.0 lib/mutils/lib/helper.rb
mutils-1.2.6 lib/mutils/lib/helper.rb
mutils-1.2.5 lib/mutils/lib/helper.rb
mutils-1.2.2 lib/mutils/lib/helper.rb
mutils-1.2.1 lib/mutils/lib/helper.rb
mutils-1.2.0 lib/mutils/lib/helper.rb
mutils-1.1.8 lib/mutils/lib/helper.rb
mutils-1.1.7 lib/mutils/lib/helper.rb
mutils-1.1.6 lib/mutils/lib/helper.rb
mutils-1.1.5 lib/mutils/lib/helper.rb
mutils-1.1.4 lib/mutils/lib/helper.rb
mutils-1.1.3 lib/mutils/lib/helper.rb
mutils-1.1.2 lib/mutils/lib/helper.rb
mutils-1.1.1 lib/mutils/lib/helper.rb
mutils-1.1.0 lib/mutils/lib/helper.rb
mutils-1.0.1 lib/mutils/lib/helper.rb
mutils-1.0.0 lib/mutils/lib/helper.rb