json_schema_tools 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -40,9 +40,7 @@ module SchemaTools
40
40
  fields = opts[:fields]
41
41
  # get objects class name without inheritance
42
42
  real_class_name = obj.class.name.split('::').last.underscore
43
- class_name = opts[:class_name] || real_class_name
44
- # directly return array & hash values
45
- return obj if ['array', 'hash'].include? class_name
43
+ class_name = opts[:class_name] || real_class_name
46
44
 
47
45
  data = {}
48
46
  # get schema
@@ -50,19 +48,37 @@ module SchemaTools
50
48
  # iterate over the defined schema fields
51
49
  schema['properties'].each do |field, prop|
52
50
  next if fields && !fields.include?(field)
51
+
53
52
  if prop['type'] == 'array'
53
+
54
54
  data[field] = [] # always set an empty array
55
55
  if obj.respond_to?( field ) && rel_objects = obj.send( field )
56
56
  rel_objects.each do |rel_obj|
57
- data[field] << from_schema(rel_obj, opts)
57
+ data[field] << if prop['properties'] && prop['properties']['$ref']
58
+ #got schema describing the objects
59
+ from_schema(rel_obj, opts)
60
+ else
61
+ rel_obj
62
+ end
58
63
  end
59
64
  end
65
+
60
66
  elsif prop['type'] == 'object' # a singular related object
67
+
61
68
  data[field] = nil # always set empty val
62
69
  if obj.respond_to?( field ) && rel_obj = obj.send( field )
63
- #dont nest field to prevent => client=>{client=>{data} }
64
- data[field] = from_schema(rel_obj, opts)
70
+ if prop['properties'] && prop['properties']['$ref']
71
+ data[field] = from_schema(rel_obj, opts)
72
+ else
73
+ # NO recursion directly get values from related object. Does
74
+ # NOT allow deeper nesting so you MUST define an own schema to be save
75
+ data[field] = {}
76
+ prop['properties'].each do |fld, prp|
77
+ data[field][fld] = rel_obj.send(fld) if obj.respond_to?(field)
78
+ end
79
+ end
65
80
  end
81
+
66
82
  else # a simple field is only added if the object knows it
67
83
  data[field] = obj.send(field) if obj.respond_to?(field)
68
84
  end
@@ -1,3 +1,3 @@
1
1
  module SchemaBuilder
2
- VERSION = '0.0.5'
2
+ VERSION = '0.0.6'
3
3
  end
@@ -8,6 +8,25 @@
8
8
  "description": "Where did the person come from",
9
9
  "type":"string",
10
10
  "maxLength": 255
11
+ },
12
+ "links_clicked":{
13
+ "description": "Timestamps of clicks. Just an example to test nested array values without object reference",
14
+ "type":"array",
15
+ "properties":{
16
+ "type": "string"
17
+ }
18
+ },
19
+ "conversion":{
20
+ "description": "Just an example to test nested object value without object reference",
21
+ "type":"object",
22
+ "properties":{
23
+ "from": {
24
+ "type": "string"
25
+ },
26
+ "to": {
27
+ "type": "string"
28
+ }
29
+ }
11
30
  }
12
31
  }
13
32
  }
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  class Client
4
4
  attr_accessor :first_name, :last_name, :addresses, :id
5
- end
5
+ end
6
6
 
7
7
  describe SchemaTools::Hash do
8
8
 
@@ -41,5 +41,40 @@ describe SchemaTools::Hash do
41
41
  hash['client']['first_name'].should be_nil
42
42
  end
43
43
  end
44
+
45
+ context 'with plain nested values' do
46
+
47
+ class Lead < Client
48
+ attr_accessor :links_clicked, :conversion
49
+ end
50
+
51
+ class Conversion
52
+ attr_accessor :from, :to
53
+ end
54
+
55
+
56
+ let(:lead){Lead.new}
57
+ before :each do
58
+ lead.links_clicked = ['2012-12-12', '2012-12-15', '2012-12-16']
59
+ conversion = Conversion.new
60
+ conversion.from = 'whatever'
61
+ conversion.to = 'whatever'
62
+ lead.conversion = conversion
63
+ @hash = SchemaTools::Hash.from_schema(lead)
64
+ end
65
+ after :each do
66
+ SchemaTools::Reader.registry_reset
67
+ end
68
+
69
+ it 'should create array with values' do
70
+ @hash['lead']['links_clicked'].should == lead.links_clicked
71
+ end
72
+
73
+ it 'should create object with values' do
74
+ @hash['lead']['conversion']['from'].should == lead.conversion.from
75
+ @hash['lead']['conversion']['to'].should == lead.conversion.to
76
+ end
77
+
78
+ end
44
79
  end
45
80
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_schema_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-30 00:00:00.000000000 Z
12
+ date: 2012-12-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -140,7 +140,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
140
140
  version: '0'
141
141
  segments:
142
142
  - 0
143
- hash: -301574637564117079
143
+ hash: 77879165624642168
144
144
  required_rubygems_version: !ruby/object:Gem::Requirement
145
145
  none: false
146
146
  requirements:
@@ -149,7 +149,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
149
149
  version: '0'
150
150
  segments:
151
151
  - 0
152
- hash: -301574637564117079
152
+ hash: 77879165624642168
153
153
  requirements: []
154
154
  rubyforge_project:
155
155
  rubygems_version: 1.8.24