Class: Esse::Hooks::Primitives::String

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

Instance Method Summary collapse

Instance Method Details

#classifyObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/esse/hooks/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("/").collect do |c|
      c.split("_").collect(&:capitalize).join
    end.join("::")
  end

  self.class.new(new_str)
end

#constantizeObject



31
32
33
34
35
36
37
38
39
# File 'lib/esse/hooks/primitives/string.rb', line 31

def constantize
  if defined?(Dry::Inflector)
    Dry::Inflector.new.constantize(self)
  elsif defined?(ActiveSupport::Inflector)
    ActiveSupport::Inflector.constantize(self)
  else
    Object.const_get(self)
  end
end

#underscoreObject



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/esse/hooks/primitives/string.rb', line 41

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