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:

  • subject (Hash)

    An input hash.

  • transform (Proc)

    A transformation block.

Returns:

  • (Hash)


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:

  • keypath (Array<String>)

    A keypath.

Returns:

  • (String)


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:

  • subject (Object)

    A haystack.

  • element (Object)

    A needle.

Returns:

  • (Boolean)


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