Class: SiteMaps::Primitives::String

Inherits:
String
  • Object
show all
Defined in:
lib/site_maps/primitives/string.rb

Instance Method Summary collapse

Instance Method Details

#classifyObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/site_maps/primitives/string.rb', line 17

def classify
  new_str = if defined?(Dry::Inflector)
    Dry::Inflector.new.classify(self)
  elsif defined?(ActiveSupport::Inflector)
    ActiveSupport::Inflector.classify(self)
  else
    split("_").map(&:capitalize).join
  end

  self.class.new(new_str)
end

#underscoreObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/site_maps/primitives/string.rb', line 29

def underscore
  new_str = sub(/^::/, "")
    .gsub("::", "/")
    .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
    .gsub(/([a-z\d])([A-Z])/, '\1_\2')
    .tr("-", "_")
    .tr(".", "_")
    .gsub(/\s/, "_")
    .gsub(/__+/, "_")
    .downcase

  self.class.new(new_str)
end