Module: ChefAPI::Util

Extended by:
Util
Included in:
Util
Defined in:
lib/chef-api/util.rb

Instance Method Summary collapse

Instance Method Details

#camelize(string) ⇒ String

Convert an underscored string to it’s camelcase equivalent constant.

Parameters:

  • string (String)

    the string to convert

Returns:

  • (String)


32
33
34
35
36
37
38
# File 'lib/chef-api/util.rb', line 32

def camelize(string)
  string
    .to_s
    .split("_")
    .map(&:capitalize)
    .join
end

#fast_collect(collection, &block) ⇒ Array

Quickly iterate over a collection using native Ruby threads, preserving the original order of elements and being all thread-safe and stuff.

Examples:

Parse a collection of JSON files


fast_collect(Dir['**/*.json']) do |item|
  JSON.parse(File.read(item))
end

Parameters:

  • collection (#each)

    the collection to iterate

  • block (Proc)

    the block to evaluate (typically an expensive operation)

Returns:

  • (Array)

    the result of the iteration



108
109
110
111
112
113
114
115
116
117
# File 'lib/chef-api/util.rb', line 108

def fast_collect(collection, &block)
  collection.map do |item|
    Thread.new do
      Thread.current[:result] = block.call(item)
    end
  end.collect do |thread|
    thread.join
    thread[:result]
  end
end

#safe_read(path) ⇒ Array<String>

“Safely” read the contents of a file on disk, catching any permission errors or not found errors and raising a nicer exception.

Examples:

Reading a file that does not exist

safe_read('/non-existent/file') #=> Error::FileNotFound

Reading a file with improper permissions

safe_read('/bad-permissions') #=> Error::InsufficientFilePermissions

Reading a regular file

safe_read('my-file.txt') #=> ["my-file", "..."]

Parameters:

  • path (String)

    the path to the file on disk

Returns:

  • (Array<String>)

    A array where the first value is the basename of the file and the second value is the literal contents from File.read.



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/chef-api/util.rb', line 78

def safe_read(path)
  path     = File.expand_path(path)
  name     = File.basename(path, ".*")
  contents = File.read(path)

  [name, contents]
rescue Errno::EACCES
  raise Error::InsufficientFilePermissions.new(path: path)
rescue Errno::ENOENT
  raise Error::FileNotFound.new(path: path)
end

#truncate(string, options = {}) ⇒ Object

Truncate the given string to a certain number of characters.

Parameters:

  • string (String)

    the string to truncate

  • options (Hash) (defaults to: {})

    the list of options (such as length)



48
49
50
51
52
53
54
55
56
# File 'lib/chef-api/util.rb', line 48

def truncate(string, options = {})
  length = options[:length] || 30

  if string.length > length
    string[0..length - 3] + "..."
  else
    string
  end
end

#underscore(string) ⇒ String

Covert the given CaMelCaSeD string to under_score. Graciously borrowed from stackoverflow.com/questions/1509915.

Parameters:

  • string (String)

    the string to use for transformation

Returns:

  • (String)


14
15
16
17
18
19
20
21
22
# File 'lib/chef-api/util.rb', line 14

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