json_schema_tools 0.0.8 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -26,6 +26,10 @@ schema.json files are located.
26
26
  Read a single schema:
27
27
 
28
28
  schema = SchemaTools::Reader.read :client
29
+
30
+ Read a schema from an existing Ruby hash:
31
+
32
+ schema = SchemaTools::Reader.read :client, { ... }
29
33
 
30
34
  Read multiple schemas, all *.json files in schema path
31
35
 
@@ -46,7 +50,6 @@ Don't like the global path and registry? Go local:
46
50
  reader.read :client, 'from/path'
47
51
  reader.registry
48
52
 
49
-
50
53
  ## Object to Schema JSON
51
54
 
52
55
  A schema provides a (public) contract about an object definition. Therefore an
@@ -17,8 +17,8 @@ Gem::Specification.new do |s|
17
17
  s.require_paths = ["lib"]
18
18
 
19
19
  s.add_dependency 'json'
20
- s.add_dependency 'activesupport'
21
- s.add_dependency 'activemodel'
20
+ s.add_dependency 'activesupport', '~> 3.2.13'
21
+ s.add_dependency 'activemodel', '~> 3.2.13'
22
22
 
23
23
  s.add_development_dependency 'rake'
24
24
  s.add_development_dependency 'simplecov'
@@ -29,13 +29,25 @@ module SchemaTools
29
29
  # round-trips. The cache can be resented with #registry_reset
30
30
  #
31
31
  # @param [String|Symbol] schema name to be read from schema path directory
32
- # @param [String] path
32
+ # @param [String|Hash] either the path to retrieve schema_name from,
33
+ # or a Schema in Ruby hash form
33
34
  # @return[HashWithIndifferentAccess] schema as hash
34
- def read(schema_name, path=nil)
35
+ def read(schema_name, path_or_schema=nil)
35
36
  schema_name = schema_name.to_sym
36
37
  return registry[schema_name] if registry[schema_name]
37
- file_path = File.join(path || SchemaTools.schema_path, "#{schema_name}.json")
38
- plain_data = File.open(file_path, 'r'){|f| f.read}
38
+
39
+ if path_or_schema.is_a? ::Hash
40
+ path = nil
41
+ plain_data = path_or_schema.to_json
42
+ elsif path_or_schema.is_a?(::String) || path_or_schema.nil?
43
+ path = path_or_schema
44
+ file_path = File.join(path || SchemaTools.schema_path, "#{schema_name}.json")
45
+ else
46
+ raise ArgumentError, "Second parameter must be a path or a schema!"
47
+ end
48
+
49
+ plain_data ||= File.open(file_path, 'r'){|f| f.read}
50
+
39
51
  schema = ActiveSupport::JSON.decode(plain_data).with_indifferent_access
40
52
  if schema[:extends]
41
53
  extends = schema[:extends].is_a?(Array) ? schema[:extends] : [ schema[:extends] ]
@@ -64,4 +76,4 @@ module SchemaTools
64
76
  end
65
77
  end
66
78
  end
67
- end
79
+ end
@@ -1,3 +1,3 @@
1
1
  module SchemaTools
2
- VERSION = '0.0.8'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -1,6 +1,24 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe SchemaTools::Reader do
4
+ let(:ruby_schema) do
5
+ {
6
+ "type"=> "object",
7
+ "name"=> "numbers",
8
+ "properties"=> {
9
+ "numbers"=> {
10
+ "type"=> "array",
11
+ "items"=> {
12
+ "type"=> "number",
13
+ "minimum"=> 1,
14
+ "maximum"=> 100
15
+ },
16
+ "additionalProperties"=> false
17
+ }
18
+ },
19
+ "additionalProperties"=> false
20
+ }
21
+ end
4
22
 
5
23
  context 'class methods' do
6
24
 
@@ -25,6 +43,17 @@ describe SchemaTools::Reader do
25
43
  schema[:properties][:lead_source].should_not be_empty
26
44
  end
27
45
 
46
+ it 'should read a schema from a Ruby Hash' do
47
+ schema = SchemaTools::Reader.read(:numbers, ruby_schema)
48
+
49
+ SchemaTools::Reader.registry[:numbers].should_not be_empty
50
+
51
+ schema[:properties][:numbers].should_not be_empty
52
+ end
53
+
54
+ it 'should enforce correct parameter usage' do
55
+ expect { SchemaTools::Reader.read(:contact, []) }.to raise_error ArgumentError
56
+ end
28
57
  end
29
58
 
30
59
  context 'instance methods' do
@@ -38,6 +67,12 @@ describe SchemaTools::Reader do
38
67
  reader.registry[:client].should_not be_empty
39
68
  end
40
69
 
70
+ it 'should read a single schema from Ruby Hash' do
71
+ schema = reader.read(:numbers, ruby_schema)
72
+ schema[:name].should == 'numbers'
73
+ schema[:properties].should_not be_empty
74
+ end
75
+
41
76
  it 'should populate instance registry' do
42
77
  reader.read(:client)
43
78
  reader.read(:address)
@@ -51,7 +86,9 @@ describe SchemaTools::Reader do
51
86
  SchemaTools::Reader.registry.should be_empty
52
87
  end
53
88
 
89
+ it 'should enforce correct parameter usage' do
90
+ expect { reader.read(:client, []) }.to raise_error ArgumentError
91
+ end
54
92
  end
55
-
56
93
  end
57
94
 
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.8
4
+ version: 0.1.0
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: 2013-02-05 00:00:00.000000000 Z
12
+ date: 2013-06-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -32,33 +32,33 @@ dependencies:
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
- - - ! '>='
35
+ - - ~>
36
36
  - !ruby/object:Gem::Version
37
- version: '0'
37
+ version: 3.2.13
38
38
  type: :runtime
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
- - - ! '>='
43
+ - - ~>
44
44
  - !ruby/object:Gem::Version
45
- version: '0'
45
+ version: 3.2.13
46
46
  - !ruby/object:Gem::Dependency
47
47
  name: activemodel
48
48
  requirement: !ruby/object:Gem::Requirement
49
49
  none: false
50
50
  requirements:
51
- - - ! '>='
51
+ - - ~>
52
52
  - !ruby/object:Gem::Version
53
- version: '0'
53
+ version: 3.2.13
54
54
  type: :runtime
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  none: false
58
58
  requirements:
59
- - - ! '>='
59
+ - - ~>
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: 3.2.13
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: rake
64
64
  requirement: !ruby/object:Gem::Requirement
@@ -159,7 +159,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
159
159
  version: '0'
160
160
  segments:
161
161
  - 0
162
- hash: 4078782522717715484
162
+ hash: -1701013660137054767
163
163
  required_rubygems_version: !ruby/object:Gem::Requirement
164
164
  none: false
165
165
  requirements:
@@ -168,7 +168,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
168
168
  version: '0'
169
169
  segments:
170
170
  - 0
171
- hash: 4078782522717715484
171
+ hash: -1701013660137054767
172
172
  requirements: []
173
173
  rubyforge_project:
174
174
  rubygems_version: 1.8.24