Module: CoreExtensions::String

Included in:
String
Defined in:
lib/extlib_lite/core_extensions/string.rb

Instance Method Summary collapse

Instance Method Details

#from_queryHash

Convert to hash from a query string.

“text_message%5Bto%5D=61447100308&text_message%5Bfrom%5D=61447100547&text_message%5Bmsg%5D=Edin%20knif%20lie%20km&text_message%5Bdate%5D=2010-05-13%2023%3A59%3A58”.from_query #=> {

"text_message"=>{
  "to"=>"61447100308",
  "from"=>"61447100547",
  "msg"=>"Edin knif lie km",
  "date"=>"2010-05-13 23:59:58"
}

}

Returns:

  • (Hash)

    Query string converted to hash.



19
20
21
22
23
# File 'lib/extlib_lite/core_extensions/string.rb', line 19

def from_query
  uri = Addressable::URI.new
  uri.query = self
  uri.query_values
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.



48
49
50
51
52
53
# File 'lib/extlib_lite/core_extensions/string.rb', line 48

def snake_case
  return downcase if match(/\A[A-Z]+\z/)
  gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
  gsub(/([a-z])([A-Z])/, '\1_\2').
  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.



34
35
36
# File 'lib/extlib_lite/core_extensions/string.rb', line 34

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