Module: Navo::Utils

Defined in:
lib/navo/utils.rb

Overview

A miscellaneous set of utility functions.

Class Method Summary collapse

Class Method Details

.camel_case(string) ⇒ String

Converts a string containing underscores/hyphens/spaces into CamelCase.

Parameters:

  • string (String)

Returns:

  • (String)


13
14
15
16
17
# File 'lib/navo/utils.rb', line 13

def camel_case(string)
  string.split(/_|-| /)
        .map { |part| part.sub(/^\w/) { |c| c.upcase } }
        .join
end

.human_time(time) ⇒ String

Returns the given Time as a human-readable string based on that time relative to the current time.

Parameters:

  • time (Time)

Returns:

  • (String)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/navo/utils.rb', line 24

def human_time(time)
  date = time.to_date

  if date == Date.today
    time.strftime('%l:%M %p')
  elsif date == Date.today - 1
    'Yesterday'
  elsif date > Date.today - 7
    time.strftime('%A') # Sunday
  elsif date.year == Date.today.year
    time.strftime('%b %e') # Jun 22
  else
    time.strftime('%b %e, %Y') # Jun 22, 2015
  end
end

.snake_case(string) ⇒ String

Convert string containing camel case or spaces into snake case.

Parameters:

  • string (String)

Returns:

  • (String)

See Also:

  • Navo::Utils.stackoverflowstackoverflow.com/questions/1509915/converting-camel-case-to-underscore-case-in-ruby


46
47
48
49
50
51
52
# File 'lib/navo/utils.rb', line 46

def snake_case(string)
  string.gsub(/::/, '/')
        .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
        .gsub(/([a-z\d])([A-Z])/, '\1_\2')
        .tr('-', '_')
        .downcase
end