module RuboCop::Schema::Helpers

Public Class Methods

templates() click to toggle source
# File lib/rubocop/schema/helpers.rb, line 9
def self.templates
  @templates ||= {}
end

Public Instance Methods

boolean() click to toggle source
# File lib/rubocop/schema/helpers.rb, line 40
def boolean
  { 'type' => 'boolean' }
end
deep_dup(obj) click to toggle source
# 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) { |merged| ... } click to toggle source
# 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) click to toggle source
# 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) click to toggle source

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

# File lib/rubocop/schema/helpers.rb, line 50
def strip_html(str)
  CGI.unescapeHTML str.gsub(/<.*?>/, '').gsub(/\s+/, ' ')
end
template(name) click to toggle source
# 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