json_schema_tools 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -31,12 +31,21 @@ module SchemaTools
31
31
  # @param [String|Symbol] schema name to be read from schema path directory
32
32
  # @param [String] path
33
33
  # @return[HashWithIndifferentAccess] schema as hash
34
- def read(schema, path=nil)
35
- schema = schema.to_sym
36
- return registry[schema] if registry[schema]
37
- file_path = File.join(path || SchemaTools.schema_path, "#{schema}.json")
34
+ def read(schema_name, path=nil)
35
+ schema_name = schema_name.to_sym
36
+ return registry[schema_name] if registry[schema_name]
37
+ file_path = File.join(path || SchemaTools.schema_path, "#{schema_name}.json")
38
38
  plain_data = File.open(file_path, 'r'){|f| f.read}
39
- registry[schema] = ActiveSupport::JSON.decode(plain_data).with_indifferent_access
39
+ schema = ActiveSupport::JSON.decode(plain_data).with_indifferent_access
40
+ if schema[:extends]
41
+ extends = schema[:extends].is_a?(Array) ? schema[:extends] : [ schema[:extends] ]
42
+ extends.each do |ext_name|
43
+ ext = read(ext_name, path)
44
+ # current schema props win
45
+ schema[:properties] = ext[:properties].merge(schema[:properties])
46
+ end
47
+ end
48
+ registry[ schema_name ] = schema
40
49
  end
41
50
 
42
51
  # Read all available schemas from a given path(folder) and return
@@ -1,3 +1,3 @@
1
1
  module SchemaBuilder
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -1,7 +1,7 @@
1
1
  { "type":"object",
2
2
  "title": "address",
3
3
  "name": "address",
4
- "description":"An address in SK is maintained within it's parent object(client, company). The first address(default_address) is used inside the address field of new documents. With multiple addresses sorting(first) is done by date, newest first. So if you add a new adddress it will be the new default one. See order and type property for details about ordering and accessing parcel work or home addresses.",
4
+ "description":"An address .. Example partially taken from SalesKing",
5
5
  "properties":{
6
6
  "id":{
7
7
  "description":"Unique identifier - UUID",
@@ -22,63 +22,20 @@
22
22
  "type":"string",
23
23
  "maxLength": 100
24
24
  },
25
- "address2":{
26
- "description": "Additional address information, like floor number",
27
- "type":"string",
28
- "maxLength": 100
29
- },
30
- "pobox":{
31
- "description": "Post office box number",
32
- "type":"string",
33
- "maxLength": 10
34
- },
35
25
  "zip":{
36
26
  "description": "Zip number of the city. Length must be between 4..10",
37
27
  "type":"string",
38
28
  "maxLength": 10
39
29
  },
40
- "state":{
41
- "description": "Country state of address",
42
- "type":"string",
43
- "maxLength": 100
44
- },
45
30
  "country":{
46
31
  "description": "Country of the address.",
47
32
  "type":"string",
48
33
  "maxLength": 100
49
34
  },
50
- "created_at":{
51
- "description": "Date the object was created in SK. Never changes afterwards",
52
- "format":"date-time",
53
- "readonly":true,
54
- "type":"string"
55
- },
56
- "updated_at":{
57
- "description": "Date the object was edited in SK.",
58
- "format":"date-time",
59
- "readonly":true,
60
- "type":"string"
61
- },
62
35
  "address_type":{
63
36
  "description": "Type of the address, as seen by vCard definition. There can only be one type. Inside of SK you can use placeholders like client.parcel_address.city to access the first parcel adr(Same for work_ / home_). <br/>Besides the placeholder default_address, always returns the first address found. Sorting is done by the order(if set) else by date, newest first.",
64
37
  "enum":["work","home", "parcel"],
65
38
  "type":"string"
66
- },
67
- "order":{
68
- "description": "Addresses are normally sorted by date, newest first. If you need to strongly rely on their sorting e.g. default_address (always first) or have multiple addresses of one type(3 parcel), use this field for all adrs. <br/>Single adrs are used in placeholders like: addresses.2.city, returning the second adr found(not necessarily one with order=2).",
69
- "type":"integer"
70
- },
71
- "lat":{
72
- "description": "Geo-Location latitude",
73
- "type":"number"
74
- },
75
- "long":{
76
- "description": "Geo-Location longitude",
77
- "type":"number"
78
- },
79
- "_destroy":{
80
- "description": "When set an existing address will be deleted. This switch is only used when addresses are passed-in nested inside their parent object(a contact).",
81
- "type":"boolean"
82
39
  }
83
40
  },
84
41
  "links":[]
@@ -1,4 +1,4 @@
1
- {"type":"object",
1
+ { "type":"object",
2
2
  "title": "Contact",
3
3
  "name": "contact",
4
4
  "description": "A simple contact",
@@ -0,0 +1,13 @@
1
+ { "type":"object",
2
+ "title": "Lead",
3
+ "name": "lead",
4
+ "extends": "contact",
5
+ "description": "A lead, inherits fields from contact schema",
6
+ "properties":{
7
+ "lead_source":{
8
+ "description": "Where did the person come from",
9
+ "type":"string",
10
+ "maxLength": 255
11
+ }
12
+ }
13
+ }
@@ -3,15 +3,28 @@ require 'spec_helper'
3
3
  describe SchemaTools::Reader do
4
4
 
5
5
  context 'class methods' do
6
+
6
7
  after :each do
7
8
  SchemaTools::Reader.registry_reset
8
9
  end
10
+
9
11
  it 'should read a single schema' do
10
12
  schema = SchemaTools::Reader.read(:page)
11
13
  schema[:name].should == 'page'
12
14
  schema[:properties].should_not be_empty
13
15
  SchemaTools::Reader.registry.should_not be_empty
14
16
  end
17
+
18
+ it 'should read a schema with inheritance' do
19
+ schema = SchemaTools::Reader.read(:lead) # extends contact
20
+
21
+ SchemaTools::Reader.registry[:contact].should_not be_empty
22
+ SchemaTools::Reader.registry[:lead].should_not be_empty
23
+
24
+ schema[:properties][:first_name].should_not be_empty
25
+ schema[:properties][:lead_source].should_not be_empty
26
+ end
27
+
15
28
  end
16
29
 
17
30
  context 'instance methods' do
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.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -119,6 +119,7 @@ files:
119
119
  - spec/fixtures/address.json
120
120
  - spec/fixtures/client.json
121
121
  - spec/fixtures/contact.json
122
+ - spec/fixtures/lead.json
122
123
  - spec/fixtures/page.json
123
124
  - spec/schema_tools/cleaner_spec.rb
124
125
  - spec/schema_tools/hash_spec.rb
@@ -139,7 +140,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
139
140
  version: '0'
140
141
  segments:
141
142
  - 0
142
- hash: -438392315177719693
143
+ hash: 1443781473730246135
143
144
  required_rubygems_version: !ruby/object:Gem::Requirement
144
145
  none: false
145
146
  requirements:
@@ -148,7 +149,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
149
  version: '0'
149
150
  segments:
150
151
  - 0
151
- hash: -438392315177719693
152
+ hash: 1443781473730246135
152
153
  requirements: []
153
154
  rubyforge_project:
154
155
  rubygems_version: 1.8.24