Module: Buda::Utils

Defined in:
lib/buda/utils.rb

Overview

A handful of run-of-the-mill utilities

Class Method Summary collapse

Class Method Details

.flatten(sequences) ⇒ Object

Get a flat Array out of a list of lists of Iterables(Enumerators)



10
11
12
# File 'lib/buda/utils.rb', line 10

def flatten(sequences)
  Enumerator::Chain.new(*sequences).to_a
end

.pick(dict_, key) ⇒ Hash

If the key exists, you will get that key-value pair.

Parameters:

  • dict_ (Hash)

    the hash to pick from

  • key (String)

    the key to look at

Returns:

  • (Hash)

    the key-value pair or an empty hash



19
20
21
22
23
24
25
26
# File 'lib/buda/utils.rb', line 19

def pick(dict_, key)
  key = key.to_sym
  if dict_.key?(key)
    { "#{key}": dict_[key] }
  else
    {}
  end
end

.pluralize(amount, noun, suffix = 's') ⇒ String

Get a pluralized noun with its appropiate quantifier

Parameters:

  • amount (Integer)
  • noun (String)

    the noun to pluralize

  • suffix (String) (defaults to: 's')

Returns:

  • (String)


34
35
36
37
# File 'lib/buda/utils.rb', line 34

def pluralize(amount, noun, suffix = 's')
  quantifier = amount or 'no'
  "#{quantifier} #{amount == 1 ? noun : noun + suffix}"
end

.snake_to_pascal(name) ⇒ String

Transform a snake-cased name to its pascal-cased version.

Parameters:

  • name (String)

Returns:

  • (String)


43
44
45
# File 'lib/buda/utils.rb', line 43

def snake_to_pascal(name)
  name.split('_').map(&:capitalize).join
end