Sha256: faa8f2f9f9a77e0e2caa747f2b692c164fa3136e0a3c2e1fe32c0fe166e22922

Contents?: true

Size: 1.06 KB

Versions: 26

Compression:

Stored size: 1.06 KB

Contents

module Schemacop
  class Result
    attr_reader :current_path
    attr_reader :errors

    def initialize(root = nil, original_data = nil)
      @current_path = []
      @errors = {}
      @root = root
      @original_data = original_data
    end

    def valid?
      errors.empty?
    end

    def data
      if errors.any?
        return nil
      else
        return @data ||= @root.cast(@original_data)
      end
    end

    def error(message)
      @errors[current_path] ||= []
      @errors[current_path] << message
    end

    def messages_by_path
      @errors.transform_keys { |k| "/#{k.join('/')}" }
    end

    def exception_message
      messages.join("\n")
    end

    def messages
      messages = []

      @errors.each do |path, path_messages|
        messages += path_messages.map do |path_message|
          "/#{path.join('/')}: #{path_message}"
        end
      end

      return messages
    end

    def in_path(segment)
      prev_path = @current_path
      @current_path += [segment]
      yield
    ensure
      @current_path = prev_path
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
schemacop-3.0.19 lib/schemacop/v3/result.rb
schemacop-3.0.18 lib/schemacop/v3/result.rb
schemacop-3.0.17 lib/schemacop/v3/result.rb
schemacop-3.0.16 lib/schemacop/v3/result.rb
schemacop-3.0.15 lib/schemacop/v3/result.rb
schemacop-3.0.14 lib/schemacop/v3/result.rb
schemacop-3.0.13 lib/schemacop/v3/result.rb
schemacop-3.0.12 lib/schemacop/v3/result.rb
schemacop-3.0.11 lib/schemacop/v3/result.rb
schemacop-3.0.10 lib/schemacop/v3/result.rb
schemacop-3.0.9 lib/schemacop/v3/result.rb
schemacop-3.0.8 lib/schemacop/v3/result.rb
schemacop-3.0.7 lib/schemacop/v3/result.rb
schemacop-3.0.6 lib/schemacop/v3/result.rb
schemacop-3.0.5 lib/schemacop/v3/result.rb
schemacop-3.0.4 lib/schemacop/v3/result.rb
schemacop-3.0.3 lib/schemacop/v3/result.rb
schemacop-3.0.2 lib/schemacop/v3/result.rb
schemacop-3.0.1 lib/schemacop/v3/result.rb
schemacop-3.0.0 lib/schemacop/v3/result.rb