Class: JsonSchemaBuilder::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/json_schema_builder/writer.rb

Overview

Create json schema files for each model in Rails.root/json-schema/modelName.json

Examples:

builder = JsonSchemaBuilder::Writer.new
builder.write

Instance Method Summary collapse

Instance Method Details

#base_hashHash{String=>Mixed}

Returns base json schema object hash.

Returns:

  • (Hash{String=>Mixed})

    base json schema object hash



47
48
49
50
51
52
53
54
55
# File 'lib/json_schema_builder/writer.rb', line 47

def base_hash
  hsh = {}
  hsh['type'] = 'object'
  hsh['title'] = ''
  hsh['description'] = 'object'
  hsh['properties'] = {}
  hsh['links'] = []
  hsh
end

#create_file_pathObject



65
66
67
# File 'lib/json_schema_builder/writer.rb', line 65

def create_file_path
  FileUtils.mkdir_p(out_path) unless File.exists?(out_path)
end

#model_pathObject



69
70
71
# File 'lib/json_schema_builder/writer.rb', line 69

def model_path
  @model_path ||= File.join( Dir.pwd, 'app/models', '**/*.rb')
end

#model_path=(path) ⇒ Object

Set the model path

Parameters:

  • path (String)

    or file or pattern like models/*/.rb



75
76
77
# File 'lib/json_schema_builder/writer.rb', line 75

def model_path=(path)
  @model_path = path
end

#modelsArray<ActiveRecord::Base>

Returns AR::Base descendant models.

Returns:

  • (Array<ActiveRecord::Base>)

    AR::Base descendant models



58
59
60
61
62
63
# File 'lib/json_schema_builder/writer.rb', line 58

def models
  # require so they are in module scope
  Dir.glob( model_path ).each { |file| require file }
  model_names = Module.constants.select { |c| (eval "#{c}").is_a?(Class) && (eval "#{c}") < ::ActiveRecord::Base }
  model_names.map{|i| "#{i}".constantize}
end

#models_as_hashObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/json_schema_builder/writer.rb', line 22

def models_as_hash
  out = []
  models.each do |model|
    obj = base_hash
    obj['title'] = model.name
    props = {}
    model.columns_hash.each do |name, col|
      prop = {}
      prop['description'] = 'the field description'
      prop['identity'] = true if col.primary
      set_readonly(name,prop)
      set_type(col.type, prop)
      set_format(col.type, prop)

      prop['default'] = col.default if col.default
      prop['maxlength'] = col.limit if col.type == :string && col.limit
      props["#{name}"] = prop
    end
    obj['properties'] = props
    out << obj
  end # models
  out
end

#out_pathObject

Path to write json files



80
81
82
# File 'lib/json_schema_builder/writer.rb', line 80

def out_path
  @out_path ||= File.join( Dir.pwd, 'json-schema')
end

#out_path=(path) ⇒ Object

Parameters:

  • path (String)

    to json schema files



85
86
87
# File 'lib/json_schema_builder/writer.rb', line 85

def out_path=(path)
  @out_path = path
end

#writeObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/json_schema_builder/writer.rb', line 11

def write
  res = []
  create_file_path
  models_as_hash.each do |model|
    file = File.join( out_path, "#{model['title'].downcase}.json")
    File.open( file, 'w+' ) {|f| f.write(JSON.pretty_generate(model)) }
    res << "#{file} created"
  end
  puts res.join("\n")
end