json_schema_builder 0.0.4 → 0.0.5

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.
@@ -1,16 +1,17 @@
1
1
  namespace :schema do
2
- rails_env = ENV['RAILS_ENV'] || 'development'
3
- desc "create schema files in json-schema/*.json"
2
+
3
+ desc "create JSON schema files in json-schema/*.json"
4
4
  task :build do |t,args|
5
5
  if defined?(Rails) && Rails.respond_to?(:application)
6
- # Rails 3
7
6
  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
7
+ elseif defined?(Rails::Initializer)
8
+ # Rails 2.3
9
+ puts "== Hiccup ==\n"
10
+ puts "Sorry Rails < 3 is not supported\n You can still write a schema by hand .. it's not that difficult.\n Btw. you should move to Rails 3 anyway!"
11
+ next
12
12
  end
13
13
  builder = SchemaBuilder::Writer.new
14
+ builder.routes = Rails.application.routes.routes.routes
14
15
  builder.write
15
16
  end
16
17
  end
@@ -1,3 +1,3 @@
1
1
  module SchemaBuilder
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -7,9 +7,12 @@ module SchemaBuilder
7
7
  # builder = SchemaBuilder::Writer.new
8
8
  # builder.write
9
9
  class Writer
10
+ # [Array<Journey::Routes>] from Rails.application.routes.routes.routes
11
+ attr_accessor :routes
10
12
 
13
+ # Create schema files
11
14
  def write
12
- out = {new: [], old: [] }
15
+ out = {:new => [], :old => [] }
13
16
  create_out_path
14
17
  models_as_hash.each do |model|
15
18
  file = File.join( out_path, "#{model['title'].downcase}.json")
@@ -17,19 +20,22 @@ module SchemaBuilder
17
20
  out[:old] << file
18
21
  else
19
22
  File.open( file, 'w+' ) {|f| f.write(JSON.pretty_generate(model)) }
20
- out[:new] << "#{file} created"
23
+ out[:new] << file
21
24
  end
22
25
  end
23
- puts "== Existing Files ==\n" unless out[:old].empty?
24
- puts out[:old].join("\n")
25
- puts "== New Files ==\n"
26
+ unless out[:old].empty?
27
+ puts "== Existing Files ==\n"
28
+ puts "Please rename them before they can be re-generated\n"
29
+ puts out[:old].join("\n")
30
+ end
31
+ puts "== Created Files ==\n" unless out[:new].empty?
26
32
  puts out[:new].join("\n")
27
33
  end
28
34
 
29
35
  def models_as_hash
30
36
  out = []
31
37
  models.each do |model|
32
- obj = base_hash
38
+ obj = schema_template
33
39
  obj['title'] = model.name
34
40
  props = {}
35
41
  model.columns_hash.each do |name, col|
@@ -39,19 +45,61 @@ module SchemaBuilder
39
45
  set_readonly(name,prop)
40
46
  set_type(col.type, prop)
41
47
  set_format(col.type, prop)
42
-
43
48
  prop['default'] = col.default if col.default
44
49
  prop['maxlength'] = col.limit if col.type == :string && col.limit
45
50
  props["#{name}"] = prop
46
51
  end
47
52
  obj['properties'] = props
48
53
  out << obj
54
+ #add links
55
+ if links = links_as_hash[model.name.tableize]
56
+ obj['links'] = links
57
+ end
49
58
  end # models
50
59
  out
51
60
  end
52
61
 
62
+ # Collect links from rails routes
63
+ # TODO detect nesting /pdts/:pdt_id/pages/:id(.:format)
64
+ #
65
+ # @return [Hash{String=>Array<Hash{ String=>String }> } ]
66
+ # { 'articles' => [
67
+ # { 'rel' => 'create'
68
+ # 'method' => POST
69
+ # 'href' => 'articles/'
70
+ # },
71
+ # {more articles actions}
72
+ # ],
73
+ # 'users' => []
74
+ # }
75
+ def links_as_hash
76
+ @links ||= begin
77
+ skip_contrl = ['passwords', 'sessions', 'users', 'admin']
78
+ skip_actions = ['edit', 'new']
79
+ out = {}
80
+ routes.collect do |route| #Journey::Route object
81
+ reqs = route.requirements
82
+ next if reqs.empty? ||
83
+ skip_contrl.detect{|c| reqs[:controller][c] } ||
84
+ skip_actions.detect{|a| reqs[:action][a]}
85
+
86
+ # setup links ary
87
+ out[ reqs[:controller] ] = [] unless out[reqs[:controller]]
88
+ # add actions as hash
89
+ unless out[ reqs[:controller] ].detect{ |i| i[:rel] == reqs[:action] }
90
+ link = {:rel => reqs[:action],
91
+ :method=> route.verb.source.gsub(/[$^]/, ''),
92
+ :href => route.path.spec.to_s.gsub(/\(\.:format\)/, '').gsub(/:id/, '{id}')
93
+ }
94
+ out[reqs[:controller]] << link
95
+ end
96
+ end if routes
97
+ out
98
+ end
99
+ end
100
+
53
101
  #@return [Hash{String=>Mixed}] base json schema object hash
54
- def base_hash
102
+ def schema_template
55
103
  hsh = {}
56
104
  hsh['type'] = 'object'
57
105
  hsh['title'] = ''
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
4
+ version: 0.0.5
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-30 00:00:00.000000000 Z
12
+ date: 2012-07-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -180,7 +180,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
180
180
  version: '0'
181
181
  segments:
182
182
  - 0
183
- hash: -3445288760737026502
183
+ hash: 2701833476145455556
184
184
  required_rubygems_version: !ruby/object:Gem::Requirement
185
185
  none: false
186
186
  requirements:
@@ -189,7 +189,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
189
189
  version: '0'
190
190
  segments:
191
191
  - 0
192
- hash: -3445288760737026502
192
+ hash: 2701833476145455556
193
193
  requirements: []
194
194
  rubyforge_project:
195
195
  rubygems_version: 1.8.24