jsonable 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 0
4
- :patch: 1
5
- :build:
4
+ :patch: 2
5
+ :build: !!null
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "jsonable"
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Travis Reeder"]
@@ -23,6 +23,7 @@ Gem::Specification.new do |s|
23
23
  "lib/jsonable.rb",
24
24
  "test/Gemfile",
25
25
  "test/my_class.rb",
26
+ "test/my_other_class.rb",
26
27
  "test/test_base.rb",
27
28
  "test/test_json_class.rb"
28
29
  ]
@@ -10,7 +10,7 @@ module Jsonable
10
10
  def json_create(object)
11
11
  puts 'object=' + object.inspect
12
12
  obj = new
13
- object.each_pair do |key, value|
13
+ object.each do |key, value|
14
14
  next if key == 'json_class'
15
15
  #puts "setting #{key}: #{value}"
16
16
  obj.instance_variable_set key, value
@@ -25,21 +25,20 @@ module Jsonable
25
25
 
26
26
  end
27
27
 
28
+ # sets the variables on the current object after json parsing the string.
28
29
  def from_json!(string)
29
30
  JSON.parse(string).each do |var, val|
30
31
  self.instance_variable_set var, val
31
32
  end
32
33
  end
33
34
 
34
- def to_json(options={})
35
- puts 'SimpleRecord as_json called with options: ' + options.inspect
35
+ def to_json(*a)
36
36
  result = {}
37
- result['json_class'] = self.class.name unless options && options[:exclude_json_class]
38
-
37
+ result['json_class'] = self.class.name
39
38
  self.instance_variables.each do |var|
40
39
  result[var] = self.instance_variable_get var
41
40
  end
42
- result.to_json
41
+ result.to_json(*a)
43
42
  end
44
43
 
45
44
  end
@@ -4,6 +4,6 @@ require_relative '../lib/jsonable'
4
4
  class MyClass
5
5
  include Jsonable
6
6
 
7
- attr_accessor :x, :y
7
+ attr_accessor :x, :y, :h, :other_class
8
8
 
9
- end
9
+ end
@@ -0,0 +1,9 @@
1
+
2
+ require_relative '../lib/jsonable'
3
+
4
+ class MyOtherClass
5
+ include Jsonable
6
+
7
+ attr_accessor :a, :b
8
+
9
+ end
@@ -3,6 +3,7 @@ require 'test/unit'
3
3
  require 'yaml'
4
4
  require_relative 'test_base'
5
5
  require_relative 'my_class'
6
+ require_relative 'my_other_class'
6
7
 
7
8
  class IronCacheTests < TestBase
8
9
  def setup
@@ -14,6 +15,7 @@ class IronCacheTests < TestBase
14
15
  c = MyClass.new
15
16
  c.x = "abc"
16
17
  c.y = 123
18
+ c.h = {'hx'=>"def", 'hy'=>456}
17
19
 
18
20
  json = c.to_json
19
21
  p json
@@ -24,6 +26,27 @@ class IronCacheTests < TestBase
24
26
  assert_equal c2.class.name, c.class.name
25
27
  assert_equal c2.x, c.x
26
28
  assert_equal c2.y, c.y
29
+ assert_equal c2.h, c.h
30
+ assert_nil c2.other_class
31
+
32
+
33
+ # Now for subclasses
34
+ c.other_class = MyOtherClass.new
35
+ c.other_class.a = "hi"
36
+ c.other_class.b = [1,2]
37
+ json = c.to_json
38
+ p json
39
+
40
+ c2 = JSON.parse(json)
41
+ p c2
42
+
43
+ assert_equal c2.class.name, c.class.name
44
+ assert_equal c2.x, c.x
45
+ assert_equal c2.y, c.y
46
+ assert_equal c2.h, c.h
47
+ assert_equal c2.other_class.a, c.other_class.a
48
+ assert_equal c2.other_class.b, c.other_class.b
49
+
27
50
 
28
51
  end
29
52
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -25,6 +25,7 @@ files:
25
25
  - lib/jsonable.rb
26
26
  - test/Gemfile
27
27
  - test/my_class.rb
28
+ - test/my_other_class.rb
28
29
  - test/test_base.rb
29
30
  - test/test_json_class.rb
30
31
  homepage: http://www.iron.io