Module: JSONObject

Defined in:
lib/json_object.rb,
lib/json_object/version.rb

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

.new(json, class_name = "object") ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/json_object.rb', line 6

def self.new json, class_name = "object"
  json_obj = JSON.parse(json)
  
  Object.const_set(class_name.classify, Class.new) unless class_name == "object"

  klass = class_name.classify.constantize 

  klass.instance_eval do
    attr_accessor *(json_obj.keys)
  end

  klass = klass.new

  json_obj.each do |key, value|
    klass.instance_variable_set "@#{key}".to_sym, value
  end

  return klass
end