Created
April 22, 2013 18:54
-
-
Save carsontang/5437518 to your computer and use it in GitHub Desktop.
JSObject
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 './jsobject' | |
# js = JSObject.new('{"carson":"tang"}') | |
require 'json' | |
class JSObject | |
attr_accessor :js_object | |
def initialize(json) | |
hash = JSON.parse(json) | |
@js_object = hash2jsobject(hash) | |
end | |
private | |
def hash2jsobject(hash) | |
key_object = Class.new do | |
klass = self | |
hash.keys.each do |key| | |
klass.send(:attr_accessor, key) | |
# if key.kind_of?(Hash) | |
# else | |
# end | |
end | |
end | |
js_obj = key_object.new | |
hash.keys.each do |key| | |
js_obj.instance_variable_set("@#{key}", hash[key]) | |
end | |
js_obj | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment