Module: Jsonable

Defined in:
lib/jsonable.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
# File 'lib/jsonable.rb', line 6

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#from_json!(string) ⇒ Object

sets the variables on the current object after json parsing the string.



30
31
32
33
34
# File 'lib/jsonable.rb', line 30

def from_json!(string)
  JSON.parse(string).each do |var, val|
    self.instance_variable_set var, val
  end
end

#to_json(*a) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/jsonable.rb', line 36

def to_json(*a)
  result = {}
  result['json_class'] = self.class.name
  self.instance_variables.each do |var|
    result[var] = self.instance_variable_get var
  end
  result.to_json(*a)
end