Module: Prmd::Combine

Defined in:
lib/prmd/commands/combine.rb

Overview

Schema combine

Class Method Summary collapse

Class Method Details

.combine(paths, options = {}) ⇒ Prmd::Schema

Merges all found schema files in the given paths into a single Schema

Parameters:

  • paths (Array<String>)
  • options (Hash<Symbol, Object>) (defaults to: {})

Returns:



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/prmd/commands/combine.rb', line 107

def self.combine(paths, options = {})
  schemata = escape_hrefs(load_schemas(paths))
  base = Prmd::Template.load_json('combine_head.json')
  schema = base['$schema']
  meta = {}
  filename = options[:meta]
  meta = Prmd.load_schema_file(filename) if filename
  if meta.nil? || meta.empty?
    if filename
      warn "Meta file (#{filename}) is empty, please fill it next time."
    else
      warn "Meta is empty, please fill it next time."
    end
    meta ||= {}
  end
  combiner = Prmd::Combiner.new(meta: meta, base: base, schema: schema, options: options)
  combiner.combine(*schemata)
end

.crawl_map(paths, options = {}) ⇒ Array<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns list of filenames from paths.

Parameters:

  • paths (Array<String>)
  • options (Hash<Symbol, Object>) (defaults to: {})

Returns:

  • (Array<String>)

    list of filenames from paths



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/prmd/commands/combine.rb', line 25

def self.crawl_map(paths, options = {})
  files = [*paths].map do |path|
    if File.directory?(path)
      Dir.glob(File.join(path, '**', '*.{json,yml,yaml}'))
    else
      path
    end
  end
  files.flatten!
  files.delete(options[:meta])
  files
end

.escape_hrefs(data) ⇒ Array<SchemaHash>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Escape ‘#’ and ‘/’ in ‘href’ keys. They need to be escaped in JSON schema, but to make it easier to write JSON schema with Prmd, those two characters are escaped automatically when they appear between ‘()’. See github.com/interagent/prmd/issues/106.

Parameters:

Returns:



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/prmd/commands/combine.rb', line 80

def self.escape_hrefs(data)
  if data.is_a? Array
    data.map! {
      |x| escape_hrefs(x)
    }
  elsif data.is_a?(Hash) || data.is_a?(Prmd::SchemaHash)
    data.each { |k,v|
      if k == 'href'
        if v.is_a? String
          v = v.gsub(/\{\(.*?\)\}/) { |x|
            x.gsub('#', '%23').gsub('/', '%2F')
          }
        end
      else
        v = escape_hrefs(v)
      end
      data[k] = v
    }
  end
  data
end

.handle_faulty_load(given, expected) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parameters:

  • given (#size)
  • expected (#size)


13
14
15
16
17
18
19
# File 'lib/prmd/commands/combine.rb', line 13

def self.handle_faulty_load(given, expected)
  unless given.size == expected.size
    abort 'Somes files have failed to parse. ' \
          'If you wish to continue without them,' \
          'please enable faulty_load using --faulty-load'
  end
end

.load_files(files, options = {}) ⇒ Array<SchemaHash>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns schema hashes.

Parameters:

  • files (Array<String>)
  • options (Hash<Symbol, Object>) (defaults to: {})

Returns:



50
51
52
53
54
55
56
57
58
# File 'lib/prmd/commands/combine.rb', line 50

def self.load_files(files, options = {})
  files.each_with_object([]) do |filename, result|
    begin
      result << load_schema_hash(filename)
    rescue JSON::ParserError, Psych::SyntaxError => ex
      $stderr.puts "unable to parse #{filename} (#{ex.inspect})"
    end
  end
end

.load_schema_hash(filename) ⇒ SchemaHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • filename (String)

Returns:



41
42
43
44
# File 'lib/prmd/commands/combine.rb', line 41

def self.load_schema_hash(filename)
  data = Prmd.load_schema_file(filename)
  SchemaHash.new(data, filename: filename)
end

.load_schemas(paths, options = {}) ⇒ Array<SchemaHash>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns schema hashes.

Parameters:

  • paths (Array<String>)
  • options (Hash<Symbol, Object>) (defaults to: {})

Returns:



64
65
66
67
68
69
70
# File 'lib/prmd/commands/combine.rb', line 64

def self.load_schemas(paths, options = {})
  files = crawl_map(paths, options)
  # sort for stable loading across platforms
  schemata = load_files(files.sort, options)
  handle_faulty_load(schemata, files) unless options[:faulty_load]
  schemata
end