module Eco module Language def sym_str_transform(value, mode = HashTransformModifier.new) v = value if value && (value.is_a?(Symbol) || value.is_a?(String)) if mode.symbolize? && !value.is_a?(Symbol) colon = mode.colon? && !value.include?(" ") && ( starts = value.start_with?(":") || ends = value.end_with?(":") ) if !mode.colon? || colon value = value.slice(1, value.length) if colon && starts value = value.slice(0, -1) if colon && ends v = (value.to_sym rescue value) || value end elsif !value.is_a?(String) # stringify v = (value.to_s rescue value) || value v = ":" + v if mode.colon? end end v end def hash_transform(hash, mode = HashTransformModifier.new) hash = (mode.clone?) ? Marshal.load(Marshal.dump(hash)) : hash hash.keys.each do |key| value = (mode.keys?) ? hash.delete(key) : hash[key] value = (value.is_a?(Hash) && mode.recurse?) ? hash_transform(value, mode) : value value = sym_str_transform(value, mode) if mode.values? key = sym_str_transform(key, mode) if mode.keys? hash[key] = value end hash end def symbolize_keys(hash, mode = HashTransformModifier.new) mode = mode.new.symbolize.keys hash_transform(hash, mode) end def stringify_keys(hash, mode = HashTransformModifier.new) mode = mode.new.stringify.keys hash_transform(hash, mode) end def symbolize_values(hash, mode = HashTransformModifier.new) mode = mode.new.symbolize.values hash_transform(hash, mode) end def stringify_values(hash, mode = HashTransformModifier.new) mode = mode.new.stringify.values hash_transform(hash, mode) end def symbolize_hash(hash, mode = HashTransformModifier.new) mode = mode.new.symbolize.keys.values hash_transform(hash, mode) end def stringify_hash(hash, mode = HashTransformModifier.new) mode = mode.new.stringify.keys.values hash_transform(hash, mode) end end end