Module: RuboCop::Schema::Helpers

Included in:
AsciiDoc::Base, AsciiDoc::Stringifier, CachedHTTPClient, CopSchema, Diff, Generator, Repo
Defined in:
lib/rubocop/schema/helpers.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.templatesObject



9
10
11
# File 'lib/rubocop/schema/helpers.rb', line 9

def self.templates
  @templates ||= {}
end

Instance Method Details

#booleanObject



40
41
42
# File 'lib/rubocop/schema/helpers.rb', line 40

def boolean
  { 'type' => 'boolean' }
end

#deep_dup(obj) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rubocop/schema/helpers.rb', line 13

def deep_dup(obj)
  case obj
  when String
    obj.dup
  when Hash
    obj.transform_values &method(:deep_dup)
  when Array
    obj.map &method(:deep_dup)
  else
    obj
  end
end

#deep_merge(old, new, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rubocop/schema/helpers.rb', line 26

def deep_merge(old, new, &block)
  return old if old.class != new.class

  case old
  when Hash
    old.merge(new.map { |k, v| [k, old.key?(k) ? deep_merge(old[k], v, &block) : v] }.to_h)
      .tap { |merged| yield merged if block_given? }
  when Array
    old | new
  else
    old
  end
end

#http_get(url) ⇒ Object



54
55
56
57
58
59
# File 'lib/rubocop/schema/helpers.rb', line 54

def http_get(url)
  url = URI(url)
  res = Net::HTTP.get_response(url)
  res.body = '' unless res.is_a? Net::HTTPOK
  res.body.force_encoding Encoding::UTF_8
end

#strip_html(str) ⇒ Object

Used for stripping HTML from Asciidoctor output, where raw output is not available, or not appropriate to use.



50
51
52
# File 'lib/rubocop/schema/helpers.rb', line 50

def strip_html(str)
  CGI.unescapeHTML str.gsub(/<.*?>/, '').gsub(/\s+/, ' ')
end

#template(name) ⇒ Object



44
45
46
# File 'lib/rubocop/schema/helpers.rb', line 44

def template(name)
  deep_dup(Helpers.templates[name] ||= YAML.load_file(ROOT.join('assets', 'templates', "#{name}.yml")).freeze)
end