Module: HashableAttributes

Included in:
Vanagon::Common::Pathname, Vanagon::Component, Vanagon::Patch, Vanagon::Platform
Defined in:
lib/vanagon/extensions/hashable.rb

Overview

This is bad, and I feel bad. This will let you append broadly useful Hash and JSON casting on any class that includes it. But it’s pretty naive, in that it just turns attributes names into keys while retaining their associated values.

Instance Method Summary collapse

Instance Method Details

#to_hashHash Also known as: to_h

Returns Converts an object to a hash with keys representing each attribute (as symbols) and their corresponding values.

Returns:

  • (Hash)

    Converts an object to a hash with keys representing each attribute (as symbols) and their corresponding values



9
10
11
12
13
# File 'lib/vanagon/extensions/hashable.rb', line 9

def to_hash
  instance_variables.each_with_object({}) do |var, hash|
    hash[var.to_s.delete("@")] = instance_variable_get(var)
  end
end

#to_json(*options) ⇒ Object



16
17
18
# File 'lib/vanagon/extensions/hashable.rb', line 16

def to_json(*options)
  to_hash.to_json options
end