json_schema_builder 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +1 -1
- data/bin/schema +3 -3
- data/json_schema_builder.gemspec +9 -9
- data/lib/json_schema_builder.rb +1 -5
- data/lib/schema_builder/railtie.rb +7 -0
- data/lib/schema_builder/tasks.rb +16 -0
- data/lib/schema_builder/version.rb +3 -0
- data/lib/{json_schema_builder → schema_builder}/writer.rb +32 -17
- data/lib/schema_builder.rb +6 -0
- data/spec/{json_schema_builder → schema_builder}/writer_spec.rb +3 -3
- metadata +15 -10
- data/lib/json_schema_builder/version.rb +0 -3
data/README.rdoc
CHANGED
data/bin/schema
CHANGED
@@ -6,12 +6,12 @@ require 'json_schema_builder'
|
|
6
6
|
require 'gli'
|
7
7
|
include GLI
|
8
8
|
|
9
|
-
version
|
9
|
+
version SchemaBuilder::VERSION
|
10
10
|
|
11
|
-
desc "
|
11
|
+
desc "Create schema files in json-schema/*.json"
|
12
12
|
command :build do |c|
|
13
13
|
c.action do |global_options, options, args|
|
14
|
-
builder =
|
14
|
+
builder = SchemaBuilder::Writer.new
|
15
15
|
builder.write
|
16
16
|
end
|
17
17
|
end
|
data/json_schema_builder.gemspec
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
$:.push File.expand_path(
|
3
|
-
require
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
require 'schema_builder/version'
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
6
|
-
s.name =
|
7
|
-
s.version =
|
8
|
-
s.authors = [
|
9
|
-
s.email = [
|
10
|
-
s.homepage =
|
11
|
-
s.summary = %q{
|
12
|
-
s.description = %q{
|
6
|
+
s.name = 'json_schema_builder'
|
7
|
+
s.version = SchemaBuilder::VERSION
|
8
|
+
s.authors = ['Georg Leciejewski']
|
9
|
+
s.email = ['gl@salesking.eu']
|
10
|
+
s.homepage = 'http://www.salesking.eu/dev'
|
11
|
+
s.summary = %q{A good API needs JSON Schema description! Get it right and build JSON Schemata for your ActiveRecord models}
|
12
|
+
s.description = %q{The first step to a clean and simple API is a unified API description in form of a shared JSON 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")
|
data/lib/json_schema_builder.rb
CHANGED
@@ -0,0 +1,16 @@
|
|
1
|
+
namespace :schema do
|
2
|
+
rails_env = ENV['RAILS_ENV'] || 'development'
|
3
|
+
desc "create schema files in json-schema/*.json"
|
4
|
+
task :build do |t,args|
|
5
|
+
if defined?(Rails) && Rails.respond_to?(:application)
|
6
|
+
# Rails 3
|
7
|
+
Rails.application.initialize!
|
8
|
+
elsif defined?(Rails::Initializer)
|
9
|
+
# Rails 2.3 .. untested
|
10
|
+
$rails_rake_task = false
|
11
|
+
Rails::Initializer.run :load_application_classes
|
12
|
+
end
|
13
|
+
builder = SchemaBuilder::Writer.new
|
14
|
+
builder.write
|
15
|
+
end
|
16
|
+
end
|
@@ -1,22 +1,29 @@
|
|
1
1
|
require 'active_record'
|
2
|
-
module
|
2
|
+
module SchemaBuilder
|
3
3
|
|
4
4
|
# Create json schema files for each model in
|
5
5
|
# Rails.root/json-schema/modelName.json
|
6
6
|
# @example
|
7
|
-
# builder =
|
7
|
+
# builder = SchemaBuilder::Writer.new
|
8
8
|
# builder.write
|
9
9
|
class Writer
|
10
10
|
|
11
11
|
def write
|
12
|
-
|
13
|
-
|
12
|
+
out = {new: [], old: [] }
|
13
|
+
create_out_path
|
14
14
|
models_as_hash.each do |model|
|
15
15
|
file = File.join( out_path, "#{model['title'].downcase}.json")
|
16
|
-
File.
|
17
|
-
|
16
|
+
if File.exist? file
|
17
|
+
out[:old] << file
|
18
|
+
else
|
19
|
+
File.open( file, 'w+' ) {|f| f.write(JSON.pretty_generate(model)) }
|
20
|
+
out[:new] << "#{file} created"
|
21
|
+
end
|
18
22
|
end
|
19
|
-
puts
|
23
|
+
puts "== Existing Files ==\n" unless out[:old].empty?
|
24
|
+
puts out[:old].join("\n")
|
25
|
+
puts "== New Files ==\n"
|
26
|
+
puts out[:new].join("\n")
|
20
27
|
end
|
21
28
|
|
22
29
|
def models_as_hash
|
@@ -54,20 +61,19 @@ module JsonSchemaBuilder
|
|
54
61
|
hsh
|
55
62
|
end
|
56
63
|
|
57
|
-
# @return [Array<
|
64
|
+
# @return [Array<Class>] classes(models) descending from ActiveRecord::Base
|
58
65
|
def models
|
59
|
-
# require so they are in module scope
|
60
66
|
Dir.glob( model_path ).each { |file| require file }
|
61
67
|
model_names = Module.constants.select { |c| (eval "#{c}").is_a?(Class) && (eval "#{c}") < ::ActiveRecord::Base }
|
62
68
|
model_names.map{|i| "#{i}".constantize}
|
63
69
|
end
|
64
70
|
|
65
|
-
def
|
71
|
+
def create_out_path
|
66
72
|
FileUtils.mkdir_p(out_path) unless File.exists?(out_path)
|
67
73
|
end
|
68
74
|
|
69
75
|
def model_path
|
70
|
-
@model_path ||= File.join(
|
76
|
+
@model_path ||= File.join( base_path, 'app/models', '**/*.rb')
|
71
77
|
end
|
72
78
|
|
73
79
|
# Set the model path
|
@@ -78,7 +84,7 @@ module JsonSchemaBuilder
|
|
78
84
|
|
79
85
|
# Path to write json files
|
80
86
|
def out_path
|
81
|
-
@out_path ||= File.join(
|
87
|
+
@out_path ||= File.join( base_path, 'json-schema')
|
82
88
|
end
|
83
89
|
|
84
90
|
# @param [String] path to json schema files
|
@@ -86,6 +92,15 @@ module JsonSchemaBuilder
|
|
86
92
|
@out_path = path
|
87
93
|
end
|
88
94
|
|
95
|
+
# Base path to application/framework e.g. Rails.root.
|
96
|
+
# Default to current working dir
|
97
|
+
def base_path
|
98
|
+
@base_path ||= Dir.pwd
|
99
|
+
end
|
100
|
+
def base_path=(path)
|
101
|
+
@base_path = path
|
102
|
+
end
|
103
|
+
|
89
104
|
private
|
90
105
|
|
91
106
|
# Set the type of the field property
|
@@ -112,11 +127,11 @@ module JsonSchemaBuilder
|
|
112
127
|
# @param [Symbol] col_type derived from ActiveRecord model
|
113
128
|
# @param [Hash{String=>String}] hsh with field properties
|
114
129
|
def set_format(col_type, hsh)
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
130
|
+
if col_type == :datetime
|
131
|
+
hsh['format'] = 'date-time'
|
132
|
+
elsif col_type == :date
|
133
|
+
hsh['format']= 'date'
|
134
|
+
end
|
120
135
|
end
|
121
136
|
|
122
137
|
# Set a field to read-only
|
@@ -1,10 +1,10 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe SchemaBuilder::Writer do
|
4
4
|
|
5
5
|
context 'in general' do
|
6
6
|
before :each do
|
7
|
-
@writer =
|
7
|
+
@writer = SchemaBuilder::Writer.new
|
8
8
|
end
|
9
9
|
it 'should get model_path' do
|
10
10
|
@writer.model_path.should == "#{Dir.pwd}/app/models/**/*.rb"
|
@@ -23,7 +23,7 @@ describe JsonSchemaBuilder::Writer do
|
|
23
23
|
|
24
24
|
context 'file writing' do
|
25
25
|
before :each do
|
26
|
-
@writer =
|
26
|
+
@writer = SchemaBuilder::Writer.new
|
27
27
|
@writer.out_path = test_out_path
|
28
28
|
@writer.model_path = File.join( File.expand_path( __FILE__), '../fixtures/*.rb')
|
29
29
|
@writer.write
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_schema_builder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
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: 2012-06-
|
12
|
+
date: 2012-06-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: json
|
@@ -139,7 +139,8 @@ dependencies:
|
|
139
139
|
- - ! '>='
|
140
140
|
- !ruby/object:Gem::Version
|
141
141
|
version: '0'
|
142
|
-
description:
|
142
|
+
description: The first step to a clean and simple API is a unified API description
|
143
|
+
in form of a shared JSON Schema
|
143
144
|
email:
|
144
145
|
- gl@salesking.eu
|
145
146
|
executables:
|
@@ -156,13 +157,16 @@ files:
|
|
156
157
|
- bin/schema
|
157
158
|
- json_schema_builder.gemspec
|
158
159
|
- lib/json_schema_builder.rb
|
159
|
-
- lib/
|
160
|
-
- lib/
|
160
|
+
- lib/schema_builder.rb
|
161
|
+
- lib/schema_builder/railtie.rb
|
162
|
+
- lib/schema_builder/tasks.rb
|
163
|
+
- lib/schema_builder/version.rb
|
164
|
+
- lib/schema_builder/writer.rb
|
161
165
|
- spec/fixtures/ar_schema.rb
|
162
166
|
- spec/fixtures/user.rb
|
163
|
-
- spec/
|
167
|
+
- spec/schema_builder/writer_spec.rb
|
164
168
|
- spec/spec_helper.rb
|
165
|
-
homepage: http://www.salesking.eu
|
169
|
+
homepage: http://www.salesking.eu/dev
|
166
170
|
licenses: []
|
167
171
|
post_install_message:
|
168
172
|
rdoc_options: []
|
@@ -176,7 +180,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
176
180
|
version: '0'
|
177
181
|
segments:
|
178
182
|
- 0
|
179
|
-
hash: -
|
183
|
+
hash: -3445288760737026502
|
180
184
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
181
185
|
none: false
|
182
186
|
requirements:
|
@@ -185,11 +189,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
185
189
|
version: '0'
|
186
190
|
segments:
|
187
191
|
- 0
|
188
|
-
hash: -
|
192
|
+
hash: -3445288760737026502
|
189
193
|
requirements: []
|
190
194
|
rubyforge_project:
|
191
195
|
rubygems_version: 1.8.24
|
192
196
|
signing_key:
|
193
197
|
specification_version: 3
|
194
|
-
summary:
|
198
|
+
summary: A good API needs JSON Schema description! Get it right and build JSON Schemata
|
199
|
+
for your ActiveRecord models
|
195
200
|
test_files: []
|