Module: Ansei::Utils

Defined in:
lib/ansei/utils.rb

Overview

Utilities class for Ansei

Class Method Summary collapse

Class Method Details

.cli_log(message) ⇒ Object

Display a message



5
6
7
# File 'lib/ansei/utils.rb', line 5

def self.cli_log(message)
  STDOUT.puts(message)
end

.dir_empty(dir) ⇒ Object

Delete the contents of a directory



12
13
14
# File 'lib/ansei/utils.rb', line 12

def self.dir_empty(dir)
  FileUtils.rm_rf("#{dir}/.")
end

.dir_glob(dir) ⇒ Object

List all files in a directory

Returns an array of found files



19
20
21
22
23
24
25
26
27
28
29
# 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) ⇒ Object

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

file - filename to parse no_dot - remove the dot?



35
36
37
38
39
40
41
# 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) ⇒ Object

Create an information object for a file

type - ‘standardized’ type of file file - filename for file

Returns a new FileInfo object



49
50
51
# File 'lib/ansei/utils.rb', line 49

def self.file_info(type, file)
  FileInfo.new(type, file)
end

.file_write(file, content) ⇒ Object

Write content to a filename; recursively creates folders if needed

file - filename to write to content - content to write



58
59
60
61
62
63
64
65
66
# 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) ⇒ Object

Convert a string to a YAML object

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

Returns a symbolized hash or an OpenStruct object



74
75
76
77
78
79
80
# 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) ⇒ Object

“Urlify” a string

Examples:

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

Returns a string



90
91
92
93
94
# 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) ⇒ Object

Convert all keys to symbols in an object

obj - object to modify

Returns an object with symbol keys



101
102
103
104
105
106
# 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) ⇒ Object

Convert all keys to symbols in an array

array - array to modify

Returns an array with symbol keys



113
114
115
116
117
# File 'lib/ansei/utils.rb', line 113

def self.symbolize_array(array)
  array.map do |value|
    symbolize(value)
  end
end

.symbolize_hash(hash) ⇒ Object

Convert all keys to symbols in a hash

hash - hash to modify

Returns a hash with symbol keys



124
125
126
127
128
# 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