Module: SlimLint::Utils
- Defined in:
- lib/slim_lint/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.
-
.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.
-
.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.
17 18 19 20 21 22 23 24 |
# File 'lib/slim_lint/utils.rb', line 17 def any_glob_matches?(globs_or_glob, file) path = File.(file) Array(globs_or_glob).any? do |glob| ::File.fnmatch?(File.(glob), path, ::File::FNM_PATHNAME | # Wildcards don't match path separators ::File::FNM_DOTMATCH) # `*` wildcard matches dotfiles end end |
.count_consecutive(items, offset = 0) {|item| ... } ⇒ Integer
Count the number of consecutive items satisfying the given Proc.
63 64 65 66 67 |
# File 'lib/slim_lint/utils.rb', line 63 def count_consecutive(items, offset = 0) count = 1 count += 1 while (offset + count < items.count) && yield(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.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/slim_lint/utils.rb', line 38 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 |
.with_environment(env) ⇒ Object
Calls a block of code with a modified set of environment variables, restoring them once the code has executed.
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/slim_lint/utils.rb', line 73 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 |