class Schemacop::V2::Collector

Attributes

exceptions[R]

Public Class Methods

new(data) click to toggle source
# File lib/schemacop/v2/collector.rb, line 5
def initialize(data)
  @exceptions = []
  @current_path = []
  @ignore_next_segment = false
  @current_datappoint_path = [data]
  @current_index = nil
end

Public Instance Methods

data() click to toggle source
# File lib/schemacop/v2/collector.rb, line 13
def data
  return nil unless valid?

  @current_datappoint_path.first
end
error(error_msg) click to toggle source
# File lib/schemacop/v2/collector.rb, line 68
def error(error_msg)
  @exceptions << {
    path:    @current_path.dup,
    message: error_msg
  }
end
exception_message() click to toggle source
# File lib/schemacop/v2/collector.rb, line 60
def exception_message
  # rubocop:disable Style/StringConcatenation
  return "Schemacop validation failed:\n" + @exceptions.map do |e|
    "- #{e[:path].join('')}: #{e[:message]}"
  end.join("\n")
  # rubocop:enable Style/StringConcatenation
end
ignore_next_segment() click to toggle source

Does not include the path segment next time {Schemacop::Collector.path} is called.

@return [Schemacop::Collector]

# File lib/schemacop/v2/collector.rb, line 79
def ignore_next_segment
  @ignore_next_segment = true
  return self
end
override_value(value) click to toggle source
# File lib/schemacop/v2/collector.rb, line 52
def override_value(value)
  if @current_datappoint_path.size > 1
    @current_datappoint_path[-2][@current_index] = value
  else
    @current_datappoint_path[0] = value
  end
end
path(segment, index, type) { || ... } click to toggle source

Construct the current path

# File lib/schemacop/v2/collector.rb, line 24
def path(segment, index, type)
  ignore_this_segment = false

  previous_index = @current_index

  if @ignore_next_segment
    ignore_this_segment = true
    @ignore_next_segment = false
  else
    unless @current_datappoint_path.last
      if type == :hash
        @current_datappoint_path[-1] = {}
      else
        @current_datappoint_path[-1] = []
      end
    end
    @current_path << segment unless ignore_this_segment
    @current_datappoint_path << @current_datappoint_path.last[index]
    @current_index = index
  end

  yield
ensure
  @current_index = previous_index
  @current_datappoint_path.pop unless ignore_this_segment
  @current_path.pop unless ignore_this_segment
end
valid?() click to toggle source
# File lib/schemacop/v2/collector.rb, line 19
def valid?
  @exceptions.empty?
end