Sha256: 5d9ba4f36152fa06a8cc46984b71512d166f25123ce4222936a4014b4678f0b5

Contents?: true

Size: 809 Bytes

Versions: 12

Compression:

Stored size: 809 Bytes

Contents

require "dry/inflector"

module Coprl::Symbol
  # Helper module for converting symbol naming to string.
  # It provides common conversions for variable naming (snake_case) and class naming (class_name)
  module ToStr
    # Maps symbols to strings
    def sym_to_str(name)
      return name if name.is_a? ::String
      name.to_s
    end

    # Converts a namespaced string or symbol to snake_case
    def snake_case(str)
      inflector.underscore(sym_to_str(str).tr('.', '_'))
    end

    # Converts a namespaced symbol or string to a proper class name with modules
    def class_name(classname)
      classname = sym_to_str(classname)
      classname.split('.').map { |m| inflector.camelize(m) }.join('::')
    end

    private
    def inflector
      @_inflector_ ||= Dry::Inflector.new
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
coprl-3.0.0.beta.12 lib/coprl/symbol/to_str.rb
coprl-3.0.0.beta.11 lib/coprl/symbol/to_str.rb
coprl-3.0.0.beta.10 lib/coprl/symbol/to_str.rb
coprl-3.0.0.beta.9 lib/coprl/symbol/to_str.rb
coprl-3.0.0.beta.8 lib/coprl/symbol/to_str.rb
coprl-3.0.0.beta.7 lib/coprl/symbol/to_str.rb
coprl-3.0.0.beta.6 lib/coprl/symbol/to_str.rb
coprl-3.0.0.beta.5 lib/coprl/symbol/to_str.rb
coprl-3.0.0.beta.4 lib/coprl/symbol/to_str.rb
coprl-3.0.0.beta.3 lib/coprl/symbol/to_str.rb
coprl-3.0.0.beta.2 lib/coprl/symbol/to_str.rb
coprl-3.0.0.beta.1 lib/coprl/symbol/to_str.rb