Module: Riffer::Helpers::Identifier
- Extended by:
- Identifier
- Included in:
- Riffer, Identifier
- Defined in:
- lib/riffer/helpers/identifier.rb
Overview
Helper module for deriving snake_case identifiers from class names.
Instance Method Summary collapse
-
#derive(class_name) ⇒ Object
Derives a snake_case identifier from a class name string.
-
#for(klass) ⇒ Object
Derives and memoizes the identifier for a class or module.
Instance Method Details
#derive(class_name) ⇒ Object
Derives a snake_case identifier from a class name string.
--
12 13 14 15 16 17 18 19 |
# 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 |
#for(klass) ⇒ Object
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.
--
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/riffer/helpers/identifier.rb', line 28 def for(klass) cached = klass.instance_variable_get(:@derived_identifier) #: String? return cached if cached # Tool classes shadow Module#name with the identifier DSL, so the real # class-path name must come from Module's own implementation. real_name = Module.instance_method(:name).bind_call(klass) #: String? return "" if real_name.nil? derived = derive(real_name) klass.instance_variable_set(:@derived_identifier, derived) derived end |