Class: Siba::StringHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/siba/helpers/string_helper.rb

Class Method Summary collapse

Class Method Details

.camelize(str) ⇒ Object

Convers a string to CamelCase. Based on Rails ActiveSupport::Inflector.camelize.



18
19
20
21
# File 'lib/siba/helpers/string_helper.rb', line 18

def camelize(str)
  str = str.capitalize
  str.gsub(/(?:_|-)([a-z\d]*)/i) { "#{$1.capitalize}" }
end

.escape_for_yaml(str) ⇒ Object



23
24
25
# File 'lib/siba/helpers/string_helper.rb', line 23

def escape_for_yaml(str)
  str.gsub("\\","\\\\\\").gsub("\"","\\\"")
end

.format_time(time) ⇒ Object



27
28
29
# File 'lib/siba/helpers/string_helper.rb', line 27

def format_time(time)
  time.strftime("%B %e, %Y")
end

.nil_or_empty(str) ⇒ Object



13
14
15
# File 'lib/siba/helpers/string_helper.rb', line 13

def nil_or_empty(str)
  str.nil? || str.strip.empty?
end

.str_to_alphnumeric(str) ⇒ Object

Helps to use a string in URLs and file names by replacing all non-alphanumeric characters with ‘-’ and converting to lowercase



9
10
11
# File 'lib/siba/helpers/string_helper.rb', line 9

def str_to_alphnumeric(str)
  str.downcase.gsub(/[^ a-z0-9]/,' ').strip.gsub(/ {1,}/,'-')
end