Class: GemBootstrap::MiscUtils
- Inherits:
-
Object
- Object
- GemBootstrap::MiscUtils
- Defined in:
- lib/gem_bootstrap/misc_utils.rb
Constant Summary collapse
- CustomError =
Class.new(StandardError)
Class Method Summary collapse
-
.camel_case(str) ⇒ Object
Receives a string and convert it to camel case.
-
.files(args = {}) ⇒ Object
List files base on some extension.
-
.no_extension_files(base_dir, wildcard, non_exts = []) ⇒ Object
List files that do not have the extension.
-
.shell(commands = []) ⇒ String
Wrapper function to call the ‘popen3’ and return the result.
-
.snake_case(str) ⇒ Object
Receives a string and convert it to snake case.
Class Method Details
.camel_case(str) ⇒ Object
Receives a string and convert it to camel case. camel_case returns CamelCase.
21 22 23 24 |
# File 'lib/gem_bootstrap/misc_utils.rb', line 21 def camel_case(str) return str if str !~ /_/ && str =~ /[A-Z]+.*/ str.split("_").map { |i| i.capitalize }.join end |
.files(args = {}) ⇒ Object
List files base on some extension
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/gem_bootstrap/misc_utils.rb', line 27 def files(args = {}) opts = { base_dir: Dir.pwd, recursive: false, exts: [], non_exts: [] }.merge(args) base_dir = opts[:base_dir] fail CustomError, "The directory #{base_dir} is not valid or or not readable!" unless File.exist?(base_dir) wildcard = opts[:recursive] ? "**" : "" exts = opts[:exts] non_exts = opts[:non_exts] file_with_extension = Dir.glob(File.join(base_dir, wildcard, "*.{#{exts.join(",")}}")) file_with_no_extension = no_extension_files(base_dir, wildcard, non_exts) (file_with_extension + file_with_no_extension).sort end |
.no_extension_files(base_dir, wildcard, non_exts = []) ⇒ Object
List files that do not have the extension
51 52 53 54 55 56 57 |
# File 'lib/gem_bootstrap/misc_utils.rb', line 51 def no_extension_files(base_dir, wildcard, non_exts = []) list = [] unless non_exts.empty? list = Dir.glob(File.join(base_dir, wildcard, "{#{non_exts.join(",")}}")) end list end |
.shell(commands = []) ⇒ String
Wrapper function to call the ‘popen3’ and return the result
63 64 65 66 67 68 69 70 71 |
# File 'lib/gem_bootstrap/misc_utils.rb', line 63 def shell(commands = []) begin command = commands.join(" ") stdin, _stderr, _status = Open3.capture3(command) rescue => e raise "Problem processing #{command}, #{e.message}" end stdin end |
.snake_case(str) ⇒ Object
Receives a string and convert it to snake case. SnakeCase returns snake_case.
11 12 13 14 15 |
# File 'lib/gem_bootstrap/misc_utils.rb', line 11 def snake_case(str) return str.downcase if str =~ /^[A-Z_]+$/ str.gsub(/\B[A-Z]/, '_\&').squeeze("_") =~ /_*(.*)/ $+.downcase end |