Skip to content
This repository
  • 2 commits
  • 4 files changed
  • 0 comments
  • 1 contributor
28  lib/schema_tools/modules/hash.rb
@@ -40,9 +40,7 @@ def from_schema(obj, opts={})
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 @@ def from_schema(obj, opts={})
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
2  lib/schema_tools/version.rb
... ...
@@ -1,3 +1,3 @@
1 1
 module SchemaBuilder
2  
-  VERSION = '0.0.5'
  2
+  VERSION = '0.0.6'
3 3
 end
19  spec/fixtures/lead.json
@@ -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
 }
37  spec/schema_tools/hash_spec.rb
@@ -2,7 +2,7 @@
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 @@ class Client
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
 

No commit comments for this range

Something went wrong with that request. Please try again.