Module: Dieses::Support::Kernel

Included in:
Dieses::Support
Defined in:
lib/dieses/support/kernel.rb

Instance Method Summary collapse

Instance Method Details

#kwargs(kwargs, *keys, **defaults) ⇒ Object

rubocop:disable Metrics/MethodLength



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/dieses/support/kernel.rb', line 17

def kwargs(kwargs, *keys, **defaults) # rubocop:disable Metrics/MethodLength
  return (kwargs = kwargs.to_h) if keys.empty? && defaults.empty?

  hash = {}
  [*keys, *defaults.keys].each do |key|
    hash[key = key.to_sym] = if kwargs.key?(key)
                               kwargs[key]
                             elsif defaults.key?(key)
                               defaults[key]
                             else
                               Undefined
                             end
  end
  hash
end

#kwargs_to_s(**kwargs) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/dieses/support/kernel.rb', line 6

def kwargs_to_s(**kwargs)
  new_kwargs = kwargs.transform_values do |value|
    case value
    when Array, Set              then value.to_a.join(' ')
    when String, Symbol, Numeric then value.to_s
    else                              raise ArgumentError, "Unexpected value type #{value.class}: #{value}"
    end
  end
  new_kwargs.map { |key, value| "#{key}='#{value}'" }.join ' '
end