module Ansei::Utils

Utilities class for Ansei

Public Class Methods

cli_log(message) click to toggle source

Display a message

# File lib/ansei/utils.rb, line 5
def self.cli_log(message)
  STDOUT.puts(message)
end
dir_empty(dir) click to toggle source

Delete the contents of a directory

@param directory [String]

# File lib/ansei/utils.rb, line 12
def self.dir_empty(dir)
  FileUtils.rm_rf("#{dir}/.")
end
dir_glob(dir) click to toggle source

List all files in a directory

Returns an array of found files

# File lib/ansei/utils.rb, line 19
def self.dir_glob(dir)
  files = []

  Dir.glob("#{dir}/**/*").each do |file|
    next if File.directory?(file)

    files << file
  end

  files
end
file_ext(file, no_dot = false) click to toggle source

Get the extension for a file, with or without the dot

file - filename to parse no_dot - remove the dot?

# File lib/ansei/utils.rb, line 35
def self.file_ext(file, no_dot = false)
  ext = File.extname(file)

  return ext unless no_dot

  ext.sub(/\A\./, '')
end
file_info(type, file) click to toggle source

Create an information object for a file

type - 'standardized' type of file file - filename for file

Returns a new FileInfo object

# File lib/ansei/utils.rb, line 49
def self.file_info(type, file)
  FileInfo.new(type, file)
end
file_write(file, content) click to toggle source

Write content to a filename; recursively creates folders if needed

file - filename to write to content - content to write

# File lib/ansei/utils.rb, line 58
def self.file_write(file, content)
  dir = file.sub(File.basename(file), '')

  FileUtils.mkdir_p(dir) unless Dir.exist?(dir)

  File.open(file, 'wb') do |f|
    f.write(content)
  end
end
string_to_yaml(string, struct = true) click to toggle source

Convert a string to a YAML object

string - string to convert struct - create an OpenStruct object?

Returns a symbolized hash or an OpenStruct object

# File lib/ansei/utils.rb, line 74
def self.string_to_yaml(string, struct = true)
  yaml = YAML.safe_load(string)

  return OpenStruct.new(yaml) if struct

  symbolize(yaml)
end
string_urlify(string) click to toggle source

“Urlify” a string

Examples:

string_urlify('/a/b/c')
# => '/a/b/c/index

Returns a string

# File lib/ansei/utils.rb, line 90
def self.string_urlify(string)
  return string if string =~ /(\Aindex\z|\/index\z)/

  "#{string}/index"
end
symbolize(obj) click to toggle source

Convert all keys to symbols in an object

obj - object to modify

Returns an object with symbol keys

# File lib/ansei/utils.rb, line 101
def self.symbolize(obj)
  return symbolize_array(obj) if obj.is_a?(Array)
  return symbolize_hash(obj) if obj.is_a?(Hash)

  obj
end
symbolize_array(array) click to toggle source

Convert all keys to symbols in an array

array - array to modify

Returns an array with symbol keys

# File lib/ansei/utils.rb, line 113
def self.symbolize_array(array)
  array.map do |value|
    symbolize(value)
  end
end
symbolize_hash(hash) click to toggle source

Convert all keys to symbols in a hash

hash - hash to modify

Returns a hash with symbol keys

# File lib/ansei/utils.rb, line 124
def self.symbolize_hash(hash)
  hash.map do |key, value|
    [key.to_sym, symbolize(value)]
  end.to_h
end