Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/simple-templater/core_exts.rb
Instance Method Summary collapse
-
#camel_case ⇒ String
Convert to camel case.
-
#snake_case ⇒ String
Convert to snake case.
-
#to_const_path ⇒ String
Convert a constant name to a path, assuming a conventional structure.
-
#to_const_string ⇒ String
Convert a path string to a constant name.
Instance Method Details
#camel_case ⇒ String
Convert to camel case.
"foo_bar".camel_case #=> "FooBar"
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_case ⇒ String
Convert to snake case.
"FooBar".snake_case #=> "foo_bar"
"HeadlineCNNNews".snake_case #=> "headline_cnn_news"
"CNN".snake_case #=> "cnn"
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_path ⇒ String
Convert a constant name to a path, assuming a conventional structure.
"FooBar::Baz".to_const_path # => "foo_bar/baz"
73 74 75 |
# File 'lib/simple-templater/core_exts.rb', line 73 def to_const_path snake_case.gsub(/::/, "/") end |
#to_const_string ⇒ String
Convert a path string to a constant name.
"merb/core_ext/string".to_const_string #=> "Merb::CoreExt::String"
60 61 62 |
# File 'lib/simple-templater/core_exts.rb', line 60 def to_const_string gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } end |