Method: Wgit::Utils.to_h

Defined in:
lib/wgit/utils.rb

.to_h(obj, ignore: [], use_strings_as_keys: true) ⇒ Hash

Returns a Hash created from obj's instance vars and values.

Parameters:

  • obj (Object)

    The object to process.

  • ignore (Array<String>) (defaults to: [])

    Attributes to ignore e.g. [':@html'].

  • use_strings_as_keys (Boolean) (defaults to: true)

    Whether to use Strings or Symbols as keys.

Returns:

  • (Hash)

    A Hash created from obj's instance vars and values.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/wgit/utils.rb', line 20

def self.to_h(obj, ignore: [], use_strings_as_keys: true)
  obj.instance_variables.reduce({}) do |hash, var|
    next hash if ignore.include?(var.to_s)

    key = var.to_s[1..] # Remove the @ prefix.
    key = key.to_sym unless use_strings_as_keys
    hash[key] = obj.instance_variable_get(var)

    hash
  end
end