Sha256: fb04d596c6eb265a2c5b7e8439a02ba034efc10dd5103a308ea6824cd3e7935f
Contents?: true
Size: 1.14 KB
Versions: 5
Compression:
Stored size: 1.14 KB
Contents
# frozen_string_literal: true require 'active_support/inflector' module EacRubyUtils class Inflector class << self VARIABLE_NAME_PATTERN = /[_a-z][_a-z0-9]*/i.freeze # Convert a string to a variable format: first character as a lowercase letter or underscore # and other as a lowercase letter, underscore or numbers. # @param string [String] The source string. # @param validate [Boolean] Affect the outcome when the result builded is not in a valid # variable format. If `true`, it raises a {ArgumentError}. If `false`, return `nil`. # @return [String, nil] # @raise [ArgumentError] def variableize(string, validate = true) # rubocop:disable Style/OptionalBooleanParameter r = ::ActiveSupport::Inflector.transliterate(string).gsub(/[^_a-z0-9]/i, '_') .gsub(/_+/, '_').gsub(/_\z/, '').gsub(/\A_/, '').downcase m = VARIABLE_NAME_PATTERN.match(r) return r if m return nil unless validate raise ::ArgumentError, "Invalid variable name \"#{r}\" was generated " \ "from string \"#{string}\"" end end end end
Version data entries
5 entries across 5 versions & 2 rubygems