Sha256: d2f60b57acbc18a6bf21cf303347123970f3b0d1efef868755e683f5ac4a585b
Contents?: true
Size: 1.71 KB
Versions: 26
Compression:
Stored size: 1.71 KB
Contents
require 'json' class Hash XML_TYPE_NAMES = { "Symbol" => "symbol", "Fixnum" => "integer", "Bignum" => "integer", "BigDecimal" => "decimal", "Float" => "float", "TrueClass" => "boolean", "FalseClass" => "boolean", "Date" => "date", "DateTime" => "datetime", "Time" => "datetime", "ActiveSupport::TimeWithZone" => "datetime" } XML_FORMATTING = { "symbol" => Proc.new { |symbol| symbol.to_s }, "date" => Proc.new { |date| date.strftime('%d/%m/%Y') }, "datetime" => Proc.new { |time| time.xmlschema }, "yaml" => Proc.new { |yaml| yaml.to_yaml } } def limit_to_keys(limit_keys) dup.limit_to_keys!(limit_keys) end def limit_to_keys!(limit_keys) keys.each { |key| delete(key) unless limit_keys.include? key } self end def soft_delete(key) cloned_hash = deep_clone cloned_hash.delete(key) cloned_hash end def deep_clone Marshal::load(Marshal.dump(self)) end # Return a new hash with all keys converted to strings. def stringify_keys dup.stringify_keys! end # Destructively convert all keys to strings. def stringify_keys! keys.each do |key| self[key.to_s] = delete(key) end self end # Return a new hash with all keys converted to symbols, as long as # they respond to +to_sym+. def symbolize_keys dup.symbolize_keys! end # Destructively convert all keys to symbols, as long as they respond # to +to_sym+. def symbolize_keys! keys.each do |key| self[(key.to_sym rescue key) || key] = delete(key) end self end end
Version data entries
26 entries across 26 versions & 1 rubygems