Class: Highway::Utilities

Inherits:
Object
  • Object
show all
Defined in:
lib/highway/utilities.rb

Overview

This class contains a collection of utility functions used throughout the codebase.

Class Method Summary collapse

Class Method Details

.hash_map(subject, &transform) ⇒ Hash

Map pairs of keys and values and combine them again into a Hash.

Parameters:

  • An input hash.

  • A transformation block.

Returns:



20
21
22
# File 'lib/highway/utilities.rb', line 20

def self.hash_map(subject, &transform)
  Hash[subject.map(&transform)]
end

.keypath_to_s(keypath) ⇒ String

Join keypath into a string.

Parameters:

  • A keypath.

Returns:



29
30
31
# File 'lib/highway/utilities.rb', line 29

def self.keypath_to_s(keypath)
  keypath.join(".")
end

.recursive_include?(subject, element) ⇒ Boolean

Recursively check whether the subject includes an element.

Parameters:

  • A haystack.

  • A needle.

Returns:



39
40
41
42
43
44
45
46
47
# File 'lib/highway/utilities.rb', line 39

def self.recursive_include?(subject, element)
  if subject.is_a?(Hash)
    recursive_include?(subject.values, element)
  elsif subject.respond_to?(:any?)
    subject.any? { |value| recursive_include?(value, element) }
  else
    subject == element
  end
end