Sha256: 2212f258fb950b1c8511b95b088024432410db8926b9b8e49b94e134e596e2c9

Contents?: true

Size: 1.87 KB

Versions: 105

Compression:

Stored size: 1.87 KB

Contents

module CC
  module Analyzer
    module IssueValidations
      class LocationFormatValidation < Validation
        class Validator
          def initialize(location)
            @location = location
            check_validity
          end

          def valid?
            error.nil?
          end

          def message
            if error
              "Location is not formatted correctly: #{error}"
            end
          end

          private

          attr_accessor :error

          attr_reader :location

          def check_validity
            if location["lines"]
              self.error = "location.lines is not valid: #{JSON.dump(location["lines"])}" unless valid_lines?(location["lines"])
            elsif location["positions"]
              self.error = "location.positions is not valid: #{JSON.dump(location["positions"])}" unless valid_positions?(location["positions"])
            else
              self.error = "location.lines or location.positions must be present"
            end
          end

          def valid_positions?(positions)
            positions.is_a?(Hash) &&
              valid_position?(positions["begin"]) &&
              valid_position?(positions["end"])
          end

          def valid_position?(position)
            position &&
              (
                [position["line"], position["column"]].all? { |value| value.is_a?(Integer) } ||
                position["offset"].is_a?(Integer)
              )
          end

          def valid_lines?(lines)
            lines.is_a?(Hash) && [lines["begin"], lines["end"]].all? { |value| value.is_a?(Integer) }
          end
        end

        def valid?
          validation.valid?
        end

        def message
          validation.message
        end

        private

        def validation
          @validation ||= Validator.new(object.fetch("location", {}))
        end
      end
    end
  end
end

Version data entries

105 entries across 105 versions & 2 rubygems

Version Path
codeclimate-0.78.0 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.77.0 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.76.0 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.75.0 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.74.0 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.73.0 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.72.0 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.71.2 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.71.1 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.71.0 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.70.6 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.70.5 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.70.4 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.70.3 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.70.2 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.70.1 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.70.0 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.69.0 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.68.0 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.67.0 lib/cc/analyzer/issue_validations/location_format_validation.rb