json_schema_tools 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -1,5 +1,8 @@
1
1
  rvm:
2
- - 1.9.2
3
2
  - 1.9.3
3
+ - 2.0.0
4
4
  gemfile:
5
- - Gemfile
5
+ - Gemfile
6
+ env:
7
+ - 'RAILS_VERSION=4'
8
+ - 'RAILS_VERSION=3.2.13'
data/CHANGELOG.md CHANGED
@@ -1,6 +1,14 @@
1
1
  # Changelog JSON Schema Tools
2
2
 
3
3
 
4
+ 2013-06
5
+
6
+ * *has_schema_attr :schema=>{schema as ruby hash}* allow pass a schema as hash
7
+ * *SchemaTools::Reader.read('name', {Schema Hash}|'String Path' )* init a schema from hash additionally to passing a string as schema path
8
+ * rails 4 (ActiveModel) compatibility
9
+ * Testing with different ActiveModel Versions
10
+
11
+
4
12
  2013-02
5
13
 
6
14
  * add validations for classes generated by KlassFactory
data/Gemfile CHANGED
@@ -1,2 +1,8 @@
1
1
  source :rubygems
2
- gemspec
2
+ gemspec
3
+
4
+ # allow testing with rails 4
5
+ version = ENV['RAILS_VERSION'] || '>3'
6
+
7
+ gem 'activesupport', version
8
+ gem 'activemodel', version
data/README.md CHANGED
@@ -21,34 +21,48 @@ Before the fun begins, with any of the tools, one or multiple JSON schema files
21
21
  must be available(read into a hash). So first provide a base path where the
22
22
  schema.json files are located.
23
23
 
24
- SchemaTools.schema_path = '/path/to/schema-json-files'
24
+ ```ruby
25
+ SchemaTools.schema_path = '/path/to/schema-json-files'
26
+ ```
25
27
 
26
28
  Read a single schema:
27
29
 
28
- schema = SchemaTools::Reader.read :client
29
-
30
+ ```ruby
31
+ schema = SchemaTools::Reader.read :client
32
+ ```
33
+
30
34
  Read a schema from an existing Ruby hash:
31
35
 
32
- schema = SchemaTools::Reader.read :client, { ... }
36
+ ```ruby
37
+ schema = SchemaTools::Reader.read :client, { ... }
38
+ ```
33
39
 
34
40
  Read multiple schemas, all *.json files in schema path
35
41
 
36
- schemata = SchemaTools::Reader.read_all
42
+ ```ruby
43
+ schemata = SchemaTools::Reader.read_all
44
+ ```
37
45
 
38
46
  Schemata are cached in registry
39
47
 
40
- SchemaTools::Reader.registry[:client]
48
+ ```ruby
49
+ SchemaTools::Reader.registry[:client]
50
+ ```
41
51
 
42
52
  Read files from a custom path?
43
53
 
44
- schema = SchemaTools::Reader.read :client, 'my/path/to/json-files'
45
- schemata = SchemaTools::Reader.read_all 'my/path/to/json-files'
54
+ ```ruby
55
+ schema = SchemaTools::Reader.read :client, 'my/path/to/json-files'
56
+ schemata = SchemaTools::Reader.read_all 'my/path/to/json-files'
57
+ ```
46
58
 
47
59
  Don't like the global path and registry? Go local:
48
60
 
49
- reader = SchemaTools::Reader.new
50
- reader.read :client, 'from/path'
51
- reader.registry
61
+ ```ruby
62
+ reader = SchemaTools::Reader.new
63
+ reader.read :client, 'from/path'
64
+ reader.registry
65
+ ```
52
66
 
53
67
  ## Object to Schema JSON
54
68
 
@@ -62,28 +76,36 @@ Following uses client.json schema(same as peter.class name) inside the global
62
76
  schema_path and adds properties to the clients_hash simply calling
63
77
  client.send('property-name'):
64
78
 
65
- peter = Client.new name: 'Peter'
66
- client_hash = SchemaTools::Hash.from_schema(peter)
67
- #=> "client"=>{"id"=>12, "name"=> "Peter", "email"=>"",..} # whatever else you have as properties
68
- # to_json is up to you .. or your rails controller
79
+ ```ruby
80
+ peter = Client.new name: 'Peter'
81
+ client_hash = SchemaTools::Hash.from_schema(peter)
82
+ #=> "client"=>{"id"=>12, "name"=> "Peter", "email"=>"",..} # whatever else you have as properties
83
+ # to_json is up to you .. or your rails controller
84
+ ```
69
85
 
70
86
  ### Customise Schema Hash
71
87
 
72
88
  Only use some fields e.g. to save bandwidth
73
89
 
74
- client_hash = SchemaTools::Hash.from_schema(peter, fields:['id', 'name'])
75
- #=> "client"=>{"id"=>12, "name"=> "Peter"}
90
+ ```ruby
91
+ client_hash = SchemaTools::Hash.from_schema(peter, fields:['id', 'name'])
92
+ #=> "client"=>{"id"=>12, "name"=> "Peter"}
93
+ ```
76
94
 
77
95
  Use a custom schema name e.g. to represent a client as contact. Assumes you also
78
96
  have a schema named contact.json
79
97
 
80
- client_hash = SchemaTools::Hash.from_schema(peter, class_name: 'contact')
81
- #=> "contact"=>{"id"=>12, "name"=> "Peter"}
98
+ ```ruby
99
+ client_hash = SchemaTools::Hash.from_schema(peter, class_name: 'contact')
100
+ #=> "contact"=>{"id"=>12, "name"=> "Peter"}
101
+ ```
82
102
 
83
103
  Use a custom schema path
84
104
 
85
- client_hash = SchemaTools::Hash.from_schema(peter, path: 'path-to/json-files/')
86
- #=> "client"=>{"id"=>12, "name"=> "Peter"}
105
+ ```ruby
106
+ client_hash = SchemaTools::Hash.from_schema(peter, path: 'path-to/json-files/')
107
+ #=> "client"=>{"id"=>12, "name"=> "Peter"}
108
+ ```
87
109
 
88
110
  ## Parameter cleaning
89
111
 
@@ -92,11 +114,13 @@ check incoming params.
92
114
 
93
115
  For example in a client controller
94
116
 
95
- def create
96
- SchemaTools::Cleaner.clean_params!(:client, params[:client])
97
- # params[:client] now only has keys defined as writable in client.json schema
98
- #..create and save client
99
- end
117
+ ```ruby
118
+ def create
119
+ SchemaTools::Cleaner.clean_params!(:client, params[:client])
120
+ # params[:client] now only has keys defined as writable in client.json schema
121
+ #..create and save client
122
+ end
123
+ ```
100
124
 
101
125
  ## Object attributes from Schema
102
126
 
@@ -104,15 +128,17 @@ Add methods, defined in schema properties, to an existing class.
104
128
  Very usefull if you are building a API client and don't want to manually add
105
129
  methods to you local classes .. like people NOT using JSON schema
106
130
 
107
- class Contact
108
- include SchemaTools::Modules::Attributes
109
- has_schema_attrs :client
110
- end
131
+ ```ruby
132
+ class Contact
133
+ include SchemaTools::Modules::Attributes
134
+ has_schema_attrs :client
135
+ end
111
136
 
112
- contact = Client.new
113
- contact.last_name = 'Rambo'
114
- # raw access
115
- contact.schema_attrs
137
+ contact = Client.new
138
+ contact.last_name = 'Rambo'
139
+ # raw access
140
+ contact.schema_attrs
141
+ ```
116
142
 
117
143
  ## Classes from Schema - KlassFactory
118
144
 
@@ -122,36 +148,56 @@ The classes are named after each schema's [name] (in global path).
122
148
  So lets assume you have a 'client.json' schema with a name attribute in it, for
123
149
  the following examples:
124
150
 
125
- SchemaTools::KlassFactory.build
126
- client = Client.new first_name: 'Heinz'
127
- client.name = 'Schultz'
128
- client.valid?
129
- client.errors.full_messages
130
-
151
+ ```ruby
152
+ SchemaTools::KlassFactory.build
153
+ client = Client.new first_name: 'Heinz'
154
+ client.name = 'Schultz'
155
+ client.valid?
156
+ client.errors.full_messages
157
+ ```
131
158
 
132
159
  Rather like a namespace? Good idea, but don't forget the class or module must
133
160
  be defined.
134
161
 
135
- SchemaTools::KlassFactory.build namespace: SalesKing
136
- client = SalesKing::Client.new
162
+ ```ruby
163
+ SchemaTools::KlassFactory.build namespace: SalesKing
164
+ client = SalesKing::Client.new
165
+ ```
137
166
 
138
167
  Add a custom schema reader most likely usefull in conjunction with a custom path
139
168
 
140
- reader = SchemaTools::Reader.new
141
- SchemaTools::KlassFactory.build reader: reader, path: HappyPdf::Schema.path
169
+ ```ruby
170
+ reader = SchemaTools::Reader.new
171
+ SchemaTools::KlassFactory.build reader: reader, path: HappyPdf::Schema.path
172
+ ```
142
173
 
143
174
  ## Real world examples
144
175
 
145
176
  * [HappyPdf json schema](https://github.com/happyPDF/happypdf_json_schema) .. api gem will follow
146
177
  * [DocTag ruby gem](https://github.com/docTag/doctag_rb) and [DocTag json-schema](https://github.com/docTag/doctag_json_schema)
147
178
  * [SalesKing json schema](https://github.com/salesking/sk_api_schema)
179
+ * .. Your UseCase here
148
180
 
149
181
  ## Test
150
182
 
151
- Only runs on Ruby 1.9
183
+ Only runs on Ruby 1.9 and by default uses most recent ActiveModel version (>3).
152
184
 
153
185
  bundle install
154
- bundle exec rake spec
186
+ rake spec
187
+
188
+ Testing with different ActiveModel / ActiveSupport Versions:
189
+
190
+ RAILS_VERSION=3.1 bundle install
191
+ rake spec
192
+ # or if already installed
193
+ RAILS_VERSION=4 rake spec
194
+
195
+ The RAILS_VERSION switch sets the version of the gems in the Gemfile and is only
196
+ usefull in test env.
197
+
198
+
199
+ # Credits
155
200
 
201
+ * [Andy Nicholson](https://github.com/anicholson)
156
202
 
157
203
  Copyright 2012-1013, Georg Leciejewski, MIT License
@@ -17,9 +17,6 @@ Gem::Specification.new do |s|
17
17
  s.require_paths = ["lib"]
18
18
 
19
19
  s.add_dependency 'json'
20
- s.add_dependency 'activesupport', '~> 3.2.13'
21
- s.add_dependency 'activemodel', '~> 3.2.13'
22
-
23
20
  s.add_development_dependency 'rake'
24
21
  s.add_development_dependency 'simplecov'
25
22
  s.add_development_dependency 'rspec'
@@ -17,8 +17,9 @@ module SchemaTools
17
17
  # @options opts [String] :path schema path
18
18
  # @options opts [SchemaTools::Reader] :reader instance, instead of global reader/registry
19
19
  def has_schema_attrs(schema, opts={})
20
- reader = opts[:reader] || SchemaTools::Reader
21
- schema = reader.read(schema, opts[:path])
20
+ reader = opts[:reader] || SchemaTools::Reader
21
+ schema_location = opts[:path] || opts[:schema]
22
+ schema = reader.read(schema, schema_location)
22
23
  # make getter / setter
23
24
  schema[:properties].each do |key, val|
24
25
  # getter
@@ -39,4 +40,4 @@ module SchemaTools
39
40
 
40
41
  end
41
42
  end
42
- end
43
+ end
@@ -1,8 +1,12 @@
1
1
  require 'active_support/concern'
2
- require 'active_model/validations'
3
- require 'active_model/naming'
4
- require 'active_model/translation'
5
- require 'active_model/conversion'
2
+ require 'active_model'
3
+ # comment single files bcs. AM requires lazy and in rails 4 names have changed!
4
+ # left them in for reference
5
+ #require 'active_model/validations'
6
+ #require 'active_model/naming'
7
+ #require 'active_model/translation'
8
+ #require 'active_model/conversion'
9
+
6
10
  module SchemaTools
7
11
  module Modules
8
12
  # Add schema properties to a class by including this module and defining from
@@ -1,3 +1,3 @@
1
1
  module SchemaTools
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
@@ -50,11 +50,11 @@ describe SchemaTools::KlassFactory do
50
50
  #to big
51
51
  client.cash_discount = 101
52
52
  client.valid?
53
- client.errors.full_messages[0].should include('less_than_or_equal_to')
53
+ client.errors.full_messages[0].should include('less than or equal to')
54
54
  # to small
55
55
  client.cash_discount = -1
56
56
  client.valid?
57
- client.errors.full_messages[0].should include('greater_than_or_equal_to')
57
+ client.errors.full_messages[0].should include('greater than or equal to')
58
58
  end
59
59
 
60
60
  it 'should raise with invalid params' do
@@ -5,21 +5,42 @@ class Contact
5
5
  has_schema_attrs :client
6
6
  end
7
7
 
8
+ class Numbers
9
+ include SchemaTools::Modules::Attributes
10
+ has_schema_attrs :numbers, :schema => schema_as_ruby_object
11
+ end
12
+
8
13
  describe SchemaTools::Modules::Attributes do
9
14
 
10
15
  context 'included' do
11
- let(:contact){Contact.new}
16
+ subject { Contact.new }
17
+
18
+ it 'should add getter methods' do
19
+ subject.should respond_to(:last_name)
20
+ end
21
+
22
+ it 'should add setter methods' do
23
+ subject.should respond_to('first_name=')
24
+ end
25
+
26
+ it 'should not add setter for readonly properties' do
27
+ subject.should_not respond_to('id=')
28
+ end
29
+ end
30
+
31
+ context 'attributes from dynamic schema' do
32
+ subject { Numbers.new }
12
33
 
13
34
  it 'should add getter methods' do
14
- contact.respond_to?(:last_name).should be_true
35
+ subject.should respond_to(:numbers)
15
36
  end
16
37
 
17
38
  it 'should add setter methods' do
18
- contact.respond_to?('first_name=').should be_true
39
+ subject.should respond_to('numbers=')
19
40
  end
20
41
 
21
42
  it 'should not add setter for readonly properties' do
22
- contact.respond_to?('id=').should be_false
43
+ subject.should_not respond_to('id=')
23
44
  end
24
45
  end
25
46
  end
@@ -1,24 +1,6 @@
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
22
4
 
23
5
  context 'class methods' do
24
6
 
@@ -44,7 +26,7 @@ describe SchemaTools::Reader do
44
26
  end
45
27
 
46
28
  it 'should read a schema from a Ruby Hash' do
47
- schema = SchemaTools::Reader.read(:numbers, ruby_schema)
29
+ schema = SchemaTools::Reader.read(:numbers, schema_as_ruby_object)
48
30
 
49
31
  SchemaTools::Reader.registry[:numbers].should_not be_empty
50
32
 
@@ -68,7 +50,7 @@ describe SchemaTools::Reader do
68
50
  end
69
51
 
70
52
  it 'should read a single schema from Ruby Hash' do
71
- schema = reader.read(:numbers, ruby_schema)
53
+ schema = reader.read(:numbers, schema_as_ruby_object)
72
54
  schema[:name].should == 'numbers'
73
55
  schema[:properties].should_not be_empty
74
56
  end
data/spec/spec_helper.rb CHANGED
@@ -11,9 +11,12 @@ end
11
11
  $:.unshift(File.dirname(__FILE__))
12
12
  $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
13
13
  require 'json_schema_tools'
14
+ require 'test_helpers'
14
15
 
15
16
  RSpec.configure do |config|
16
17
  end
17
18
 
18
19
  # set global json schema path for examples
19
20
  SchemaTools.schema_path = File.expand_path('../fixtures', __FILE__)
21
+
22
+ puts "Testing with ActiveModel Version: #{ActiveModel.version rescue ActiveModel::VERSION::STRING}"
@@ -0,0 +1,22 @@
1
+ def schema_as_ruby_object
2
+ {
3
+ "type" => "object",
4
+ "name" => "numbers",
5
+ "properties" => {
6
+ "numbers" => {
7
+ "type" => "array",
8
+ "items" => {
9
+ "type" => "number",
10
+ "minimum" => 1,
11
+ "maximum" => 100
12
+ },
13
+ "id" => {
14
+ "type" => "number",
15
+ "readonly" => true
16
+ },
17
+ "additionalProperties" => false
18
+ }
19
+ },
20
+ "additionalProperties" => false
21
+ }
22
+ end
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.1.0
4
+ version: 0.1.1
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-06-26 00:00:00.000000000 Z
12
+ date: 2013-07-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -27,38 +27,6 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
- - !ruby/object:Gem::Dependency
31
- name: activesupport
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - ~>
36
- - !ruby/object:Gem::Version
37
- version: 3.2.13
38
- type: :runtime
39
- prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ~>
44
- - !ruby/object:Gem::Version
45
- version: 3.2.13
46
- - !ruby/object:Gem::Dependency
47
- name: activemodel
48
- requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - ~>
52
- - !ruby/object:Gem::Version
53
- version: 3.2.13
54
- type: :runtime
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ~>
60
- - !ruby/object:Gem::Version
61
- version: 3.2.13
62
30
  - !ruby/object:Gem::Dependency
63
31
  name: rake
64
32
  requirement: !ruby/object:Gem::Requirement
@@ -145,6 +113,7 @@ files:
145
113
  - spec/schema_tools/modules/attributes_spec.rb
146
114
  - spec/schema_tools/reader_spec.rb
147
115
  - spec/spec_helper.rb
116
+ - spec/test_helpers.rb
148
117
  homepage: https://github.com/salesking/json_schema_tools
149
118
  licenses: []
150
119
  post_install_message:
@@ -159,7 +128,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
159
128
  version: '0'
160
129
  segments:
161
130
  - 0
162
- hash: -1701013660137054767
131
+ hash: 2544977169694822683
163
132
  required_rubygems_version: !ruby/object:Gem::Requirement
164
133
  none: false
165
134
  requirements:
@@ -168,7 +137,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
168
137
  version: '0'
169
138
  segments:
170
139
  - 0
171
- hash: -1701013660137054767
140
+ hash: 2544977169694822683
172
141
  requirements: []
173
142
  rubyforge_project:
174
143
  rubygems_version: 1.8.24