Last active
March 14, 2017 11:48
-
-
Save prodis/4288774 to your computer and use it in GitHub Desktop.
Ruby Fundamental - Usando recursividade para alterar valores de hash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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>[email protected]</email> | |
| <date_birth>1910-09-01</date_birth> | |
| </customer> | |
| </transaction> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" => "[email protected]", | |
| "date_birth" => "1910-09-01" | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" => "[email protected]", | |
| "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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" => "[email protected]", | |
| "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