Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ruby Fundamental - Usando recursividade para alterar valores de hash
<transaction>
<id>456</id>
<status>Aprovada</status>
<order_number>F2457</order_number>
<price>33.21</price>
<date_transaction>2012-12-13T12:35:30</date_transaction>
<date_release></date_release>
<payment>
<payment_method>Mastercard</payment_method>
<date_approval>2012-12-13T12:35:31</date_approval>
<date_payment>2012-12-13T12:35:31</date_payment>
<tid>1355409331</tid>
</payment>
<customer>
<name>Pedro Bonamides</name>
<email>pedro@bo.com.br</email>
<date_birth>1910-09-01</date_birth>
</customer>
</transaction>
hash = {
"transaction" => {
"id" => "456",
"status" => "Aprovada",
"order_number" => "F2457",
"price" => "33.21",
"date_transaction" => "2012-12-13T12:35:30",
"date_release" => nil,
"payment" => {
"payment_method" => "Mastercard",
"date_approval" => "2012-12-13T12:35:31",
"date_payment" => "2012-12-13T12:35:31",
"tid" => "1355409331"
},
"customer" => {
"name" => "Pedro Bonamides",
"email" => "pedro@bo.com.br",
"date_birth" => "1910-09-01"
}
}
}
require 'active_support/core_ext'
hash["transaction"]["date_transaction"].class # => String
hash["transaction"]["date_transaction"] # => 2012-12-13T12:35:30
hash["transaction"]["date_transaction"] = hash["transaction"]["date_transaction"].to_time
hash["transaction"]["date_transaction"].class # => Time
hash["transaction"]["date_transaction"] # => 2012-12-13 12:35:30 UTC
require 'active_support/core_ext'
def date_to_time!(hash)
hash.each do |key, value|
if key.to_s.starts_with?("date_") && value
puts "Converting #{key}"
hash[key] = (value.to_time rescue value) || value
end
end
end
date_to_time! hash["transaction"]
# => "Converting date_transaction"
hash["transaction"]["date_transaction"].class # => Time
hash["transaction"]["date_release"].class # => NilClass
hash["transaction"]["payment"]["date_approval"].class # => String
hash["transaction"]["payment"]["date_payment"].class # => String
hash["transaction"]["customer"]["date_birth"].class # => String
require 'active_support/core_ext'
def date_to_time!(hash)
hash.each do |key, value|
date_to_time!(value) if value.is_a?(Hash)
if key.to_s.starts_with?("date_") && value
puts "Converting #{key}"
hash[key] = (value.to_time rescue value) || value
end
end
end
date_to_time! hash
# => Converting date_transaction
# => Converting date_approval
# => Converting date_payment
# => Converting date_birth
hash["transaction"]["date_transaction"].class # => Time
hash["transaction"]["date_release"].class # => NilClass
hash["transaction"]["payment"]["date_approval"].class # => Time
hash["transaction"]["payment"]["date_payment"].class # => Time
hash["transaction"]["customer"]["date_birth"].class # => Time
hash = {
"transaction" => {
"id" => "456",
"status" => "Aprovada",
"order_number" => "F2457",
"price" => "33.21",
"date_transaction" => "2012-12-13T12:35:30",
"date_release" => nil,
"payment" => {
"payment_method" => "Mastercard",
"date_approval" => "2012-12-13T12:35:31",
"date_payment" => "2012-12-13T12:35:31",
"tid" => "1355409331"
},
"customer" => {
"name" => "Pedro Bonamides",
"email" => "pedro@bo.com.br",
"date_birth" => "1910-09-01",
"fake" => {
"date_fake" => "2012-12-17"
}
}
}
}
date_to_time! hash
# => Converting date_transaction
# => Converting date_approval
# => Converting date_payment
# => Converting date_birth
# => Converting date_fake
hash["transaction"]["date_transaction"].class # => Time
hash["transaction"]["date_release"].class # => NilClass
hash["transaction"]["payment"]["date_approval"].class # => Time
hash["transaction"]["payment"]["date_payment"].class # => Time
hash["transaction"]["customer"]["date_birth"].class # => Time
hash["transaction"]["customer"]["fake"]["date_fake"].class # => Time
require 'active_support/core_ext'
class HashConverter
def self.date_to_time!(hash)
hash.each do |key, value|
date_to_time!(value) if value.is_a?(Hash)
if key.to_s.starts_with?("date_") && value
puts "Converting #{key}"
hash[key] = (value.to_time rescue value) || value
end
end
end
end
hash = {
"transaction" => {
"id" => "456",
"status" => "Aprovada",
"order_number" => "F2457",
"price" => "33.21",
"date_transaction" => "2012-12-13T12:35:30",
"date_release" => nil,
"payment" => {
"payment_method" => "Mastercard",
"date_approval" => "2012-12-13T12:35:31",
"date_payment" => "2012-12-13T12:35:31",
"tid" => "1355409331"
},
"customer" => {
"name" => "Pedro Bonamides",
"email" => "pedro@bo.com.br",
"date_birth" => "1910-09-01",
"fake" => {
"date_fake" => "2012-12-17"
}
}
}
}
HashConverter.date_to_time! hash
# => Converting date_transaction
# => Converting date_approval
# => Converting date_payment
# => Converting date_birth
# => Converting date_fake
hash["transaction"]["date_transaction"].class # => Time
hash["transaction"]["date_release"].class # => NilClass
hash["transaction"]["payment"]["date_approval"].class # => Time
hash["transaction"]["payment"]["date_payment"].class # => Time
hash["transaction"]["customer"]["date_birth"].class # => Time
hash["transaction"]["customer"]["fake"]["date_fake"].class # => Time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment