module HashableAttributes

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.

Public Instance Methods

to_h()
Alias for: to_hash
to_hash() click to toggle source

@return [Hash] Converts an object to a hash with keys representing

each attribute (as symbols) and their corresponding values
# 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
Also aliased as: to_h
to_json(*options) click to toggle source
# File lib/vanagon/extensions/hashable.rb, line 16
def to_json(*options)
  to_hash.to_json options
end