Sha256: 4682b903023bef85a650eef0c783d047810d21e8c6209d5d428a8be3fb819ed3

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

module Schemacop
  class Collector
    attr_reader :current_path
    attr_reader :exceptions

    def initialize
      @exceptions = []
      @current_path = []
      @ignore_next_segment = false
    end

    def valid?
      @exceptions.empty?
    end

    # Construct the current path
    def path(segment)
      ignore_this_segment = false
      if @ignore_next_segment
        ignore_this_segment = true
        @ignore_next_segment = false
      end

      @current_path << segment unless ignore_this_segment

      yield
    ensure
      @current_path.pop unless ignore_this_segment
    end

    def exception_message
      return "Schemacop validation failed:\n" + @exceptions.map do |e|
        "- #{e[:path].join('')}: #{e[:message]}"
      end.join("\n")
    end

    def error(error_msg)
      @exceptions << {
        path: current_path.dup,
        message: error_msg
      }
    end

    # Does not include the path segment next time {Schemacop::Collector.path} is
    # called.
    #
    # @return [Schemacop::Collector]
    def ignore_next_segment
      @ignore_next_segment = true
      return self
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
schemacop-2.3.2 lib/schemacop/collector.rb
schemacop-2.3.1 lib/schemacop/collector.rb
schemacop-2.3.0 lib/schemacop/collector.rb