Module: Ellipses::Support
- Extended by:
- SanitizePath
- Defined in:
- lib/ellipses/support/ui.rb,
lib/ellipses/support/shell.rb,
lib/ellipses/support/digest.rb,
lib/ellipses/support/entropy.rb,
lib/ellipses/support/to_range.rb,
lib/ellipses/support/writelines.rb,
lib/ellipses/support/expand_path.rb,
lib/ellipses/support/refinements.rb,
lib/ellipses/support/search_path.rb,
lib/ellipses/support/updatelines.rb,
lib/ellipses/support/deflate_path.rb,
lib/ellipses/support/sanitize_path.rb,
lib/ellipses/support/intersperse_arrays.rb,
lib/ellipses/support/prefixize_non_blank.rb
Defined Under Namespace
Modules: Color, Refinements, SanitizePath, Shell, UI
Class Method Summary
collapse
extended
Class Method Details
.deflate_path(path, rootdir = nil) ⇒ Object
9
10
11
12
|
# File 'lib/ellipses/support/deflate_path.rb', line 9
def deflate_path(path, rootdir = nil)
base = Pathname.new(rootdir || '.').expand_path
Pathname.new(path).cleanpath.expand_path.relative_path_from(base).to_s
end
|
.digest(*args) ⇒ Object
9
10
11
|
# File 'lib/ellipses/support/digest.rb', line 9
def digest(*args)
::Digest::SHA256.hexdigest args.map(&:to_s).join
end
|
.entropy(string) ⇒ Object
8
9
10
11
12
13
14
15
|
# File 'lib/ellipses/support/entropy.rb', line 8
def entropy(string)
string
.each_char
.group_by(&:to_s)
.values
.map { |x| x.length / string.length.to_f }
.reduce(0) { |e, x| e - (x * Math.log2(x)) }
end
|
.expand_path(path, rootdir = nil) ⇒ Object
9
10
11
|
# File 'lib/ellipses/support/expand_path.rb', line 9
def expand_path(path, rootdir = nil)
Pathname.new(::File.join(rootdir || '.', path)).cleanpath.to_s
end
|
.intersperse_arrays(arrays, intersperse) ⇒ Object
7
8
9
10
11
12
|
# File 'lib/ellipses/support/intersperse_arrays.rb', line 7
def intersperse_arrays(arrays, intersperse)
[].tap do |interspersed|
arrays.each { |array| interspersed.append(array, intersperse) }
interspersed.pop
end
end
|
.prefixize_non_blank(string, prefix, excludes: nil) ⇒ Object
7
8
9
10
11
12
13
14
|
# File 'lib/ellipses/support/prefixize_non_blank.rb', line 7
def prefixize_non_blank(string, prefix, excludes: nil)
stripped = string.strip
return string if prefix.empty? || stripped.empty?
return string if excludes && [*excludes].include?(stripped)
"#{prefix}#{string}"
end
|
.search_path(patterns, starting_dir = '.') ⇒ Object
9
10
11
12
13
14
15
16
17
|
# File 'lib/ellipses/support/search_path.rb', line 9
def search_path(patterns, starting_dir = '.')
Pathname.new(starting_dir).expand_path.ascend.each do |dir|
[*patterns].each do |pattern|
next unless ::File.exist?(file = ::File.join(dir, pattern))
return [dir.to_s, file]
end
end
end
|
.to_range(index_or_range) ⇒ Object
7
8
9
10
11
12
13
|
# File 'lib/ellipses/support/to_range.rb', line 7
def to_range(index_or_range)
case index_or_range
when Range then index_or_range
when Integer then Range.new(index_or_range, index_or_range)
else raise ArgumentError, "Integer or a Range expected where found: #{index_or_range.class}"
end
end
|
.updatelines(file, lines) ⇒ Object
7
8
9
10
11
12
13
14
15
16
17
|
# File 'lib/ellipses/support/updatelines.rb', line 7
def updatelines(file, lines)
old = ::File.exist?(file) ? ::File.read(file) : nil
new = "#{[*lines].join.chomp}\n"
if old && old == new
false
else
::File.write(file, new)
true
end.tap { |status| yield(status) if block_given? }
end
|
.writelines(file, lines) ⇒ Object
7
8
9
|
# File 'lib/ellipses/support/writelines.rb', line 7
def writelines(file, lines)
::File.write(file, "#{[*lines].join.chomp}\n")
end
|