Module: LintTrappings::Utils
- Defined in:
- lib/lint_trappings/utils.rb
Overview
Miscellaneus collection of helper functions.
Class Method Summary collapse
-
.any_glob_matches?(globs_or_glob, file) ⇒ Boolean
Returns whether a glob pattern (or any of a list of patterns) matches the specified file.
-
.camel_case(str) ⇒ String
Converts a string containing underscores/hyphens/spaces into CamelCase.
-
.count_consecutive(items, offset = 0) {|item| ... } ⇒ Integer
Count the number of consecutive items satisfying the given Proc.
-
.for_consecutive_items(items, satisfies, min_consecutive = 2) {|group| ... } ⇒ Object
Find all consecutive items satisfying the given block of a minimum size, yielding each group of consecutive items to the provided block.
-
.normalize_indent(code) ⇒ Object
Strips off excess leading indentation from each line so we can use Heredocs for writing code without having the leading indentation count.
-
.pluralize(word, count) ⇒ String
Returns the plural of the word if necessary based on the given count.
-
.snake_case(str) ⇒ String
Convert a class name or CamelCase string into snake_case.
-
.with_environment(env) ⇒ Object
Calls a block of code with a modified set of environment variables, restoring them once the code has executed.
Class Method Details
.any_glob_matches?(globs_or_glob, file) ⇒ Boolean
Returns whether a glob pattern (or any of a list of patterns) matches the specified file.
This is defined here so our file globbing options are consistent everywhere we perform globbing.
15 16 17 18 19 20 21 |
# File 'lib/lint_trappings/utils.rb', line 15 def any_glob_matches?(globs_or_glob, file) Array(globs_or_glob).any? do |glob| ::File.fnmatch?(glob, file, ::File::FNM_PATHNAME | # Wildcards don't match path separators ::File::FNM_DOTMATCH) # `*` wildcard matches dotfiles end end |
.camel_case(str) ⇒ String
Converts a string containing underscores/hyphens/spaces into CamelCase.
86 87 88 |
# File 'lib/lint_trappings/utils.rb', line 86 def camel_case(str) str.split(/_|-| /).map { |part| part.sub(/^\w/, &:upcase) }.join end |
.count_consecutive(items, offset = 0) {|item| ... } ⇒ Integer
Count the number of consecutive items satisfying the given Proc.
60 61 62 63 64 |
# File 'lib/lint_trappings/utils.rb', line 60 def count_consecutive(items, offset = 0, &block) count = 1 count += 1 while (offset + count < items.count) && block.call(items[offset + count]) count end |
.for_consecutive_items(items, satisfies, min_consecutive = 2) {|group| ... } ⇒ Object
Find all consecutive items satisfying the given block of a minimum size, yielding each group of consecutive items to the provided block.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/lint_trappings/utils.rb', line 35 def for_consecutive_items(items, satisfies, min_consecutive = 2) current_index = -1 while (current_index += 1) < items.count next unless satisfies[items[current_index]] count = count_consecutive(items, current_index, &satisfies) next unless count >= min_consecutive # Yield the chunk of consecutive items yield items[current_index...(current_index + count)] current_index += count # Skip this patch of consecutive items to find more end end |
.normalize_indent(code) ⇒ Object
Strips off excess leading indentation from each line so we can use Heredocs for writing code without having the leading indentation count.
102 103 104 105 |
# File 'lib/lint_trappings/utils.rb', line 102 def normalize_indent(code) leading_indent = code[/^(\s*)/, 1] code.lstrip.gsub(/\n#{leading_indent}/, "\n") end |
.pluralize(word, count) ⇒ String
Returns the plural of the word if necessary based on the given count.
96 97 98 |
# File 'lib/lint_trappings/utils.rb', line 96 def pluralize(word, count) count == 1 ? word : "#{word}s" end |
.snake_case(str) ⇒ String
Convert a class name or CamelCase string into snake_case.
73 74 75 76 77 78 79 |
# File 'lib/lint_trappings/utils.rb', line 73 def snake_case(str) str.gsub(/::/, '/') .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z\d])([A-Z])/, '\1_\2') .tr('-', '_') .downcase end |
.with_environment(env) ⇒ Object
Calls a block of code with a modified set of environment variables, restoring them once the code has executed.
111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/lint_trappings/utils.rb', line 111 def with_environment(env) old_env = {} env.each do |var, value| old_env[var] = ENV[var.to_s] ENV[var.to_s] = value end yield ensure old_env.each { |var, value| ENV[var.to_s] = value } end |