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.85.12 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.85.11 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.85.10 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.85.9 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.85.8 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.85.7 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.85.6 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.85.5 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.85.4 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.85.3 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.85.2 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.85.1 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.85.0 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.84.0 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.83.0 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.82.0 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.81.0 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.80.0 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.79.0 lib/cc/analyzer/issue_validations/location_format_validation.rb
codeclimate-0.78.1 lib/cc/analyzer/issue_validations/location_format_validation.rb