Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/simple-templater/core_exts.rb

Instance Method Summary collapse

Instance Method Details

#camel_caseString

Convert to camel case.

"foo_bar".camel_case          #=> "FooBar"

Returns:

  • (String)

    Receiver converted to camel case.


47
48
49
50
# File 'lib/simple-templater/core_exts.rb', line 47

def camel_case
  return self if self !~ /_/ && self =~ /[A-Z]+.*/
  split('_').map{|e| e.capitalize}.join
end

#snake_caseString

Convert to snake case.

"FooBar".snake_case           #=> "foo_bar"
"HeadlineCNNNews".snake_case  #=> "headline_cnn_news"
"CNN".snake_case              #=> "cnn"

Returns:

  • (String)

    Receiver converted to snake case.


33
34
35
36
37
# File 'lib/simple-templater/core_exts.rb', line 33

def snake_case
  return self.downcase if self =~ /^[A-Z]+$/
  self.gsub(/([A-Z]+)(?=[A-Z][a-z]?)|\B[A-Z]/, '_\&') =~ /_*(.*)/
    return $+.downcase
end

#to_const_pathString

Convert a constant name to a path, assuming a conventional structure.

"FooBar::Baz".to_const_path # => "foo_bar/baz"

Returns:

  • (String)

    Path to the file containing the constant named by receiver (constantized string), assuming a conventional structure.


73
74
75
# File 'lib/simple-templater/core_exts.rb', line 73

def to_const_path
  snake_case.gsub(/::/, "/")
end

#to_const_stringString

Convert a path string to a constant name.

"merb/core_ext/string".to_const_string #=> "Merb::CoreExt::String"

Returns:

  • (String)

    Receiver converted to a constant name.


60
61
62
# File 'lib/simple-templater/core_exts.rb', line 60

def to_const_string
  gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
end