Module: Shale::Utils Private
- Defined in:
- lib/shale/utils.rb
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Utitlity functions
Class Method Summary collapse
-
.classify(str) ⇒ Object
private
Convert string to Ruby’s class naming convention.
-
.presence(value) ⇒ Object
private
Return value or nil if value is empty.
-
.snake_case(str) ⇒ Object
private
Convert string to snake case.
-
.underscore(str) ⇒ Object
private
Convert word to under score.
-
.upcase_first(str) ⇒ Object
private
Upcase first letter of a string.
Class Method Details
.classify(str) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Convert string to Ruby’s class naming convention
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/shale/utils.rb', line 32 def self.classify(str) # names may include a period, which will need to be stripped out str = str.to_s.gsub('.', '') str = str.sub(/^[a-z\d]*/) { |match| upcase_first(match) || match } str.gsub('::', '/').gsub(%r{(?:_|-|(/))([a-z\d]*)}i) do word = Regexp.last_match(2) substituted = upcase_first(word) || word Regexp.last_match(1) ? "::#{substituted}" : substituted end end |
.presence(value) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return value or nil if value is empty
88 89 90 91 92 |
# File 'lib/shale/utils.rb', line 88 def self.presence(value) return nil unless value return nil if value.empty? value end |
.snake_case(str) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Convert string to snake case
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/shale/utils.rb', line 54 def self.snake_case(str) # XML elements allow periods and hyphens str = str.to_s.gsub('.', '_') return str.to_s unless /[A-Z-]|::/.match?(str) word = str.to_s.gsub('::', '/') word = word.gsub(/([A-Z]+)(?=[A-Z][a-z])|([a-z\d])(?=[A-Z])/) do "#{Regexp.last_match(1) || Regexp.last_match(2)}_" end word = word.tr('-', '_') word.downcase end |
.underscore(str) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Convert word to under score
75 76 77 |
# File 'lib/shale/utils.rb', line 75 def self.underscore(str) snake_case(str).split('/').last end |
.upcase_first(str) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Upcase first letter of a string
17 18 19 20 21 |
# File 'lib/shale/utils.rb', line 17 def self.upcase_first(str) return '' unless str return '' if str.empty? str[0].upcase + str[1..-1] end |