Module: Gaskit::Helpers

Defined in:
lib/gaskit/helpers.rb

Class Method Summary collapse

Class Method Details

.deep_compact(hash) ⇒ Hash

Applies deep compat on the provided hash.

Parameters:

  • hash (Hash)

    The original hash to compact.

Returns:

  • (Hash)

    The compacted hash.



22
23
24
25
26
27
# File 'lib/gaskit/helpers.rb', line 22

def deep_compact(hash)
  hash.each_with_object({}) do |(k, v), result|
    compacted = v.is_a?(Hash) ? deep_compact(v) : v
    result[k.to_sym] = compacted unless compacted.nil?
  end
end

.resolve_name(source) ⇒ String

Resolves the provide class’s name.

Parameters:

  • source (Class, Object, String, Symbol)

Returns:

  • (String)

    The resolved class name.



33
34
35
36
37
38
39
40
41
42
# File 'lib/gaskit/helpers.rb', line 33

def resolve_name(source)
  case source
  when String, Symbol
    source.to_s
  when Class
    source.name
  else
    source.class.name
  end
end

.time_execution { ... } ⇒ Array<Float, Object>

Measures the time taken for execution.

Yields:

  • The block containing the logic to time.

Returns:

  • (Array<Float, Object>)

    The duration and the result.



10
11
12
13
14
15
16
# File 'lib/gaskit/helpers.rb', line 10

def time_execution
  start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  result = yield
  duration = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time)

  [format("%.6f", duration), result]
end