class Inspector::Constraint::Violation::List

Attributes

children[R]
violations[R]

Public Class Methods

new(violations = [], children = {}) click to toggle source
# File lib/inspector/constraint/violation/list.rb, line 7
def initialize(violations = [], children = {})
  @violations = violations
  @children = children
end

Public Instance Methods

<<(violation) click to toggle source
# File lib/inspector/constraint/violation/list.rb, line 23
def <<(violation)
  unless violation.kind_of?(Inspector::Constraint::Violation)
    raise "#{violation.inspect} is not a Inspector::Constraint::Violation"
  end

  @violations << violation
end
Also aliased as: push
[](property_path) click to toggle source
# File lib/inspector/constraint/violation/list.rb, line 40
def [](property_path)
  child_path, _, property_path = property_path.to_s.split(/(\.|\[|\])/, 2)

  not_found       = "cannot locate violations for #{property_path}"
  violations_list = @children.fetch(child_path) { raise not_found }

  if property_path
    violations_list[property_path]
  else
    violations_list
  end
end
[]=(property_path, violations_list) click to toggle source
# File lib/inspector/constraint/violation/list.rb, line 32
def []=(property_path, violations_list)
  unless violations_list.kind_of?(Inspector::Constraint::Violation::List)
    raise "#{violations_list.inspect} is not a Inspector::Constraint::Violation::List"
  end

  @children[property_path.to_s] = violations_list
end
each(path = "") { |path, violation| ... } click to toggle source
# File lib/inspector/constraint/violation/list.rb, line 53
def each(path = "", &block)
  @violations.each do |violation|
    yield(path, violation)
  end

  @children.each do |sub_path, list|
    prefix = "." unless path.empty? || sub_path.start_with?("[")

    list.each("#{path}#{prefix}#{sub_path}", &block)
  end
end
empty?() click to toggle source
# File lib/inspector/constraint/violation/list.rb, line 12
def empty?
  @violations.empty? && @children.empty?
end
inspect() click to toggle source
# File lib/inspector/constraint/violation/list.rb, line 84
def inspect
  "#<violations %{violations} children=%{children}>" % {
    :violations    => @violations.inspect,
    :children      => @children.inspect
  }
end
length() click to toggle source
# File lib/inspector/constraint/violation/list.rb, line 16
def length
  length  = @violations.length
  length += @children.values.map(&:length).reduce(:+) unless @children.empty?

  length
end
push(violation)
Alias for: <<
to_s() click to toggle source
# File lib/inspector/constraint/violation/list.rb, line 65
def to_s
  string = ""

  unless @violations.empty?
    string += @violations.map(&:to_s).join("\n")
    string += "\n"
  end

  @children.each do |path, list|
    unless list.empty?
      string += "#{path}:\n"
      string += list.to_s.split("\n").map {|line| "  #{line}"}.join("\n")
      string += "\n"
    end
  end

  string
end