Sha256: 63b535a308dcb172252c4eac40a1c87043e37cfca8f1577ee3751fb58b378171
Contents?: true
Size: 1.17 KB
Versions: 3
Compression:
Stored size: 1.17 KB
Contents
class JSONable def to_json hash = {} self.instance_variables.each do |var| hash[var] = self.instance_variable_get var end hash.keys.each do |k| if(k[0,1] == '@') #k[1] = k[1].upcase tempUp = k[1].upcase hash[tempUp + k[2, k.length - 1]] = hash[k] hash.delete(k) #puts k[0] end end hash.to_json end def to_h instance_variables.map do |iv| value = instance_variable_get(:"#{iv}") [ iv.to_s[1..-1], # name without leading `@` case value when JSONable then value.to_h # Base instance? convert deeply when Array # Array? convert elements value.map do |e| e.respond_to?(:to_h) ? e.to_h : e end else value # seems to be non-convertable, put as is end ] end.to_h end def from_json! string JSON.load(string).each do |var, val| #need to replace first letter with '@' + <firstletter> fChar = "@" + var[0].downcase varTrim = var[1..-1] var = fChar + varTrim #puts 'VARR: ' + var + '\n' #puts val self.instance_variable_set var, val end end end
Version data entries
3 entries across 3 versions & 2 rubygems
Version | Path |
---|---|
CroemincRubyGem-0.1.2 | lib/Helpers/jso_nable.rb |
MetropagoRubyGem-0.1.1 | lib/Helpers/jso_nable.rb |
MetropagoRubyGem-0.1.0 | lib/Helpers/jso_nable.rb |