class RuboCop::Schema::CopSchema

Constants

KNOWN_TYPES

Attributes

info[R]

@return CopInfo

json[R]

@return Hash

Public Class Methods

new(info) click to toggle source

@param [CopInfo] info

# File lib/rubocop/schema/cop_schema.rb, line 17
def initialize(info)
  @info = info.dup.freeze
  @json = template('cop_schema')
  generate
end

Public Instance Methods

as_json() click to toggle source
# File lib/rubocop/schema/cop_schema.rb, line 23
def as_json
  @json
end
Also aliased as: to_h
freeze() click to toggle source
Calls superclass method
# File lib/rubocop/schema/cop_schema.rb, line 29
def freeze
  @json.freeze
  super
end
to_h()
Alias for: as_json

Private Instance Methods

assign_attribute_description(prop, attr) click to toggle source

@param [Hash] prop @param [Attribute] attr

# File lib/rubocop/schema/cop_schema.rb, line 73
def assign_attribute_description(prop, attr)
  prop['description'] = format_default(attr.default) unless attr.default.nil?
end
assign_attribute_type(prop, attr) click to toggle source

@param [Hash] prop @param [Attribute] attr

# File lib/rubocop/schema/cop_schema.rb, line 63
def assign_attribute_type(prop, attr)
  if KNOWN_TYPES.key? attr.type&.downcase
    prop['type'] ||= KNOWN_TYPES[attr.type.downcase] unless prop.key? '$ref'
  elsif attr.type
    prop['enum'] = attr.type.split(/\s*,\s*/)
  end
end
assign_default_attributes() click to toggle source
# File lib/rubocop/schema/cop_schema.rb, line 56
def assign_default_attributes
  props['AutoCorrect']            = boolean if info.supports_autocorrect
  props['Enabled']['description'] = "Default: #{info.enabled_by_default}" if info.enabled_by_default
end
format_default(default) click to toggle source
# File lib/rubocop/schema/cop_schema.rb, line 77
def format_default(default)
  default = default.join(', ') if default.is_a? Array
  "Default: #{default}"
end
generate() click to toggle source
# File lib/rubocop/schema/cop_schema.rb, line 46
def generate
  json['description'] = info.description unless info.description.nil?
  assign_default_attributes
  info.attributes&.each do |attr|
    prop = props[attr.name] ||= {}
    assign_attribute_type prop, attr
    assign_attribute_description prop, attr
  end
end
props() click to toggle source
# File lib/rubocop/schema/cop_schema.rb, line 42
def props
  json['properties']
end