json_schema_tools 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +32 -0
- data/json_schema_tools.gemspec +4 -4
- data/lib/schema_tools/klass_factory.rb +35 -0
- data/lib/schema_tools/modules/attributes.rb +3 -1
- data/lib/schema_tools/version.rb +2 -2
- data/lib/schema_tools.rb +1 -0
- data/spec/schema_tools/klass_factory_spec.rb +56 -0
- metadata +11 -9
data/README.md
CHANGED
@@ -111,6 +111,38 @@ methods to you local classes .. like people NOT using JSON schema
|
|
111
111
|
# raw access
|
112
112
|
contact.schema_attrs
|
113
113
|
|
114
|
+
## Objects from Schema - KlassFactory
|
115
|
+
|
116
|
+
Use the KlassFactory to directly create classes, with all attributes from a
|
117
|
+
schema. The classes are named after each schema[name] found in from global path.
|
118
|
+
So lets assume you have a 'client.json' schema with a name attribute in it, for
|
119
|
+
the following examples:
|
120
|
+
|
121
|
+
SchemaTools::KlassFactory.build
|
122
|
+
client = Client.new
|
123
|
+
client.name = 'Mändy'
|
124
|
+
|
125
|
+
|
126
|
+
Rather like a namespace? Good idea, but the class or module must be defined.
|
127
|
+
|
128
|
+
module SalesKing; end
|
129
|
+
SchemaTools::KlassFactory.build namespace: SalesKing
|
130
|
+
client = SalesKing::Client.new
|
131
|
+
|
132
|
+
Add a custom schema reader
|
133
|
+
|
134
|
+
reader = SchemaTools::Reader.new
|
135
|
+
reader.path = HappyPdf::Schema.path
|
136
|
+
SchemaTools::KlassFactory.build reader: reader
|
137
|
+
|
138
|
+
|
139
|
+
|
140
|
+
## Real world examples
|
141
|
+
|
142
|
+
* [HappyPdf json schema](https://github.com/happyPDF/happypdf_json_schema) .. api gem will follow
|
143
|
+
* [DocTag ruby gem](https://github.com/docTag/doctag_rb) and [DocTag json-schema](https://github.com/docTag/doctag_json_schema)
|
144
|
+
* [SalesKing json schema](https://github.com/salesking/sk_api_schema)
|
145
|
+
|
114
146
|
## Test
|
115
147
|
|
116
148
|
Only runs on Ruby 1.9
|
data/json_schema_tools.gemspec
CHANGED
@@ -4,12 +4,12 @@ require 'schema_tools/version'
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
6
6
|
s.name = 'json_schema_tools'
|
7
|
-
s.version =
|
7
|
+
s.version = SchemaTools::VERSION
|
8
8
|
s.authors = ['Georg Leciejewski']
|
9
9
|
s.email = ['gl@salesking.eu']
|
10
|
-
s.homepage = '
|
11
|
-
s.summary = %q{JSON Schema API tools
|
12
|
-
s.description = %q{Want to create a JSON Schema powered API? This toolset provides methods to read schemata, render objects as defined in schema, clean parameters according to schema, ...}
|
10
|
+
s.homepage = 'https://github.com/salesking/json_schema_tools'
|
11
|
+
s.summary = %q{JSON Schema API tools for server and client side}
|
12
|
+
s.description = %q{Want to create or read a JSON Schema powered API? This toolset provides methods to read schemata, render objects as defined in schema, clean parameters according to schema, ...}
|
13
13
|
|
14
14
|
s.files = `git ls-files`.split("\n")
|
15
15
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'active_support/core_ext/string/inflections'
|
3
|
+
module SchemaTools
|
4
|
+
class KlassFactory
|
5
|
+
|
6
|
+
class << self
|
7
|
+
|
8
|
+
# Build classes from schema inside the given namespace. Uses all classes
|
9
|
+
# found in schema path
|
10
|
+
#
|
11
|
+
# @param [String|Symbol|Module] namespace of the new classes e.g. MyCustomNamespace::MySchemaClass
|
12
|
+
# @param [Hash] opts
|
13
|
+
# @options opts [SchemaTools::Reader] :reader to use instead of global one
|
14
|
+
def build(opts={})
|
15
|
+
reader = opts[:reader] || SchemaTools::Reader
|
16
|
+
schemata = reader.read_all( opts[:path] || SchemaTools.schema_path )
|
17
|
+
namespace = opts[:namespace] || Object
|
18
|
+
if namespace.is_a?(String) || namespace.is_a?(Symbol)
|
19
|
+
namespace = "#{namespace}".constantize
|
20
|
+
end
|
21
|
+
|
22
|
+
schemata.each do |schema|
|
23
|
+
klass_name = schema['name'].classify
|
24
|
+
next if namespace.const_defined?(klass_name, false)
|
25
|
+
klass = namespace.const_set(klass_name, Class.new)
|
26
|
+
klass.class_eval do
|
27
|
+
include SchemaTools::Modules::Attributes
|
28
|
+
has_schema_attrs schema['name'], reader: reader
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -15,8 +15,10 @@ module SchemaTools
|
|
15
15
|
# @param [Symbol|String] schema name
|
16
16
|
# @param [Hash<Symbol|String>] opts
|
17
17
|
# @options opts [String] :path schema path
|
18
|
+
# @options opts [SchemaTools::Reader] :reader instance, instead of global reader/registry
|
18
19
|
def has_schema_attrs(schema, opts={})
|
19
|
-
|
20
|
+
reader = opts[:reader] || SchemaTools::Reader
|
21
|
+
schema = reader.read(schema, opts[:path])
|
20
22
|
# make getter / setter
|
21
23
|
schema[:properties].each do |key, val|
|
22
24
|
# getter
|
data/lib/schema_tools/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module
|
2
|
-
VERSION = '0.0.
|
1
|
+
module SchemaTools
|
2
|
+
VERSION = '0.0.7'
|
3
3
|
end
|
data/lib/schema_tools.rb
CHANGED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class TestNamespaceKlass
|
4
|
+
# for schema classes
|
5
|
+
end
|
6
|
+
|
7
|
+
module TestNamespaceModule
|
8
|
+
|
9
|
+
end
|
10
|
+
|
11
|
+
describe SchemaTools::KlassFactory do
|
12
|
+
|
13
|
+
after :each do
|
14
|
+
SchemaTools::Reader.registry_reset
|
15
|
+
|
16
|
+
# try cleaning objs
|
17
|
+
Object.send :remove_const, 'Client' if Object.const_defined? 'Client'
|
18
|
+
TestNamespaceKlass.send :remove_const, 'Client' if TestNamespaceKlass.const_defined? 'Client'
|
19
|
+
TestNamespaceModule.send :remove_const, 'Client' if TestNamespaceModule.const_defined? 'Client'
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'class building' do
|
23
|
+
it 'should build from class name' do
|
24
|
+
SchemaTools::KlassFactory.build
|
25
|
+
expect { Client.new }.to_not raise_error
|
26
|
+
expect { Lead.new }.to_not raise_error
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
context 'class building with namespace' do
|
32
|
+
|
33
|
+
after :each do
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'should build from class name' do
|
37
|
+
SchemaTools::KlassFactory.build(namespace: TestNamespaceKlass)
|
38
|
+
expect { TestNamespaceKlass::Client.new }.to_not raise_error
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should build from class as string' do
|
42
|
+
SchemaTools::KlassFactory.build(namespace: 'TestNamespaceKlass')
|
43
|
+
expect { TestNamespaceKlass::Client.new }.to_not raise_error
|
44
|
+
end
|
45
|
+
it 'should build from module name' do
|
46
|
+
SchemaTools::KlassFactory.build(namespace: TestNamespaceModule)
|
47
|
+
expect { TestNamespaceModule::Client.new }.to_not raise_error
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should build from module name as string' do
|
51
|
+
SchemaTools::KlassFactory.build(namespace: 'TestNamespaceModule')
|
52
|
+
expect { TestNamespaceModule::Client.new }.to_not raise_error
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
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.0.
|
4
|
+
version: 0.0.7
|
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:
|
12
|
+
date: 2013-01-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: json
|
@@ -91,9 +91,9 @@ dependencies:
|
|
91
91
|
- - ! '>='
|
92
92
|
- !ruby/object:Gem::Version
|
93
93
|
version: '0'
|
94
|
-
description: Want to create a JSON Schema powered API? This toolset provides
|
95
|
-
to read schemata, render objects as defined in schema, clean parameters
|
96
|
-
to schema, ...
|
94
|
+
description: Want to create or read a JSON Schema powered API? This toolset provides
|
95
|
+
methods to read schemata, render objects as defined in schema, clean parameters
|
96
|
+
according to schema, ...
|
97
97
|
email:
|
98
98
|
- gl@salesking.eu
|
99
99
|
executables: []
|
@@ -111,6 +111,7 @@ files:
|
|
111
111
|
- lib/schema_tools.rb
|
112
112
|
- lib/schema_tools/cleaner.rb
|
113
113
|
- lib/schema_tools/hash.rb
|
114
|
+
- lib/schema_tools/klass_factory.rb
|
114
115
|
- lib/schema_tools/modules/attributes.rb
|
115
116
|
- lib/schema_tools/modules/hash.rb
|
116
117
|
- lib/schema_tools/modules/read.rb
|
@@ -123,10 +124,11 @@ files:
|
|
123
124
|
- spec/fixtures/page.json
|
124
125
|
- spec/schema_tools/cleaner_spec.rb
|
125
126
|
- spec/schema_tools/hash_spec.rb
|
127
|
+
- spec/schema_tools/klass_factory_spec.rb
|
126
128
|
- spec/schema_tools/modules/attributes_spec.rb
|
127
129
|
- spec/schema_tools/reader_spec.rb
|
128
130
|
- spec/spec_helper.rb
|
129
|
-
homepage:
|
131
|
+
homepage: https://github.com/salesking/json_schema_tools
|
130
132
|
licenses: []
|
131
133
|
post_install_message:
|
132
134
|
rdoc_options: []
|
@@ -140,7 +142,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
140
142
|
version: '0'
|
141
143
|
segments:
|
142
144
|
- 0
|
143
|
-
hash:
|
145
|
+
hash: -3983792296207531443
|
144
146
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
145
147
|
none: false
|
146
148
|
requirements:
|
@@ -149,11 +151,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
149
151
|
version: '0'
|
150
152
|
segments:
|
151
153
|
- 0
|
152
|
-
hash:
|
154
|
+
hash: -3983792296207531443
|
153
155
|
requirements: []
|
154
156
|
rubyforge_project:
|
155
157
|
rubygems_version: 1.8.24
|
156
158
|
signing_key:
|
157
159
|
specification_version: 3
|
158
|
-
summary: JSON Schema API tools
|
160
|
+
summary: JSON Schema API tools for server and client side
|
159
161
|
test_files: []
|