Module: Substation::Utils

Defined in:
lib/substation/support/utils.rb

Overview

A collection of utility methods

Class Method Summary collapse

Class Method Details

.coerce_callable(handler) ⇒ Class, Proc

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Coerce the given handler object

Parameters:

  • handler (Symbol, String, Proc)

    a name denoting a const that responds to ‘#call(object)`, or a proc

Returns:

  • (Class, Proc)

    the callable action handler



56
57
58
59
60
61
62
63
64
65
# File 'lib/substation/support/utils.rb', line 56

def self.coerce_callable(handler)
  case handler
  when Symbol, String
    Utils.const_get(handler)
  when Proc, Class, Chain
    handler
  else
    raise(ArgumentError)
  end
end

.const_get(name) ⇒ Class?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the constant for the given FQN

Parameters:

  • name (#to_s)

    the FQN denoting a constant

Returns:

  • (Class, nil)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/substation/support/utils.rb', line 14

def self.const_get(name)
  list = name.to_s.split("::")
  list.shift if list.first.empty?
  obj = Object
  list.each do |const|
    # This is required because const_get tries to look for constants in the
    # ancestor chain, but we only want constants that are HERE
    obj =
      if obj.const_defined?(const)
        obj.const_get(const)
      else
        obj.const_missing(const)
      end
  end
  obj
end

.symbolize_keys(hash) ⇒ Hash<Symbol, Object>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts string keys into symbol keys

Parameters:

  • hash (Hash<#to_sym, Object>)

    a hash with keys that respond to ‘#to_sym`

Returns:

  • (Hash<Symbol, Object>)

    a hash with symbol keys



40
41
42
43
44
45
# File 'lib/substation/support/utils.rb', line 40

def self.symbolize_keys(hash)
  hash.each_with_object({}) { |(key, value), normalized_hash|
    normalized_value = value.is_a?(Hash) ? symbolize_keys(value) : value
    normalized_hash[key.to_sym] = normalized_value
  }
end