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

.path_hash(path) ⇒ String?

Returns a hash of the contents of the given file/directory.

Parameters:

  • path (String)

Returns:

  • (String, nil)


44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/navo/utils.rb', line 44

def path_hash(path)
  if File.exist?(path)
    %x{
      { cd cookbooks;
       export LC_ALL=C;
       find #{path} -type f -exec md5sum {} + | sort; echo;
       find #{path} -type d | sort;
       find #{path} -type d | sort | md5sum;
      } | md5sum
    }.split(' ', 2).first
  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


63
64
65
66
67
68
69
# File 'lib/navo/utils.rb', line 63

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