Sha256: 0820cb597da396f5f248448d1b83c2d5f8387cd7fa9b69f9631dba944eceb6e9
Contents?: true
Size: 1.85 KB
Versions: 12
Compression:
Stored size: 1.85 KB
Contents
require 'plezi/activation' require 'plezi/router/router' module Plezi module_function # attempts to convert a string's encoding to UTF-8, but only when the string is a valid UTF-8 encoded string. def try_utf8!(string, encoding = ::Encoding::UTF_8) return nil unless string string.force_encoding(::Encoding::ASCII_8BIT) unless string.force_encoding(encoding).valid_encoding? string end # Sanitizes hash data, attempting to "rubyfy" any Hash Strings to reversible Ruby objects. i.e: # # {"go" => "<tag>home</tag>", "now" => "false", "count" => "8", "float" => "0.7", "empty" => ""} # # Will become: # # {go: "<tag>home</tag>", now: false, count: 8, float: "0.7", empty: nil} # # As you may notice, float Strings aren't "rubyfied". Any "rubyfied" object will revert back to the same String form using `to_s`. def rubyfy(hash) case hash when Hash cpy = Hash.new(&hash_proc_4symstr) hash.each { |k, v| cpy[k.is_a?(String) ? k.to_sym : k] = rubyfy(v) } hash = cpy when String hash = if hash.empty? nil elsif hash.to_i.to_s == hash hash.to_i elsif hash == 'true'.freeze true elsif hash == 'false'.freeze false else ERB::Util.h try_utf8!(hash) end when Array hash = hash.map { |i| rubyfy(i) } end hash end # Catches String/Symbol mixups. Add this to any Hash using Hash#default_proc= def hash_proc_4symstr @hash_proc_4symstr ||= proc do |hash, key| case key when String tmp = key.to_sym hash.key?(tmp) ? hash[tmp] : nil when Symbol tmp = key.to_s hash.key?(tmp) ? hash[tmp] : nil end end end end
Version data entries
12 entries across 12 versions & 1 rubygems