module Riffer::Helpers::Identifier
Helper module for deriving snake_case identifiers from class names.
Public Instance Methods
Source
# File lib/riffer/helpers/identifier.rb, line 12 def derive(class_name) class_name. to_s. gsub("::", "/"). gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2'). gsub(/([a-z\d])([A-Z])/, '\1_\2'). downcase end
Derives a snake_case identifier from a class name string.
Source
# File lib/riffer/helpers/identifier.rb, line 28 def for(klass) cached = klass.instance_variable_get(:@derived_identifier) #: String? return cached if cached real_name = real_name(klass) return "" if real_name.nil? derived = derive(real_name) klass.instance_variable_set(:@derived_identifier, derived) derived end
Derives and memoizes the identifier for a class or module. Anonymous classes return ββ without caching, so a class named later still derives its real identifier β a guard that must travel with the cache, so callers never memoize their own.
Source
# File lib/riffer/helpers/identifier.rb, line 46 def real_name(klass) Module.instance_method(:name).bind_call(klass) #: String? end
Returns the class-path name of a class or module, or nil when anonymous. Tool classes shadow Module#name with the identifier DSL, so the real name must come from Moduleβs own implementation.