Class: Inspector::Constraint::Violation::List

Inherits:
Object
  • Object
show all
Defined in:
lib/inspector/constraint/violation/list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(violations = [], children = {}) ⇒ List

Returns a new instance of List.



7
8
9
10
# File 'lib/inspector/constraint/violation/list.rb', line 7

def initialize(violations = [], children = {})
  @violations = violations
  @children = children
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



5
6
7
# File 'lib/inspector/constraint/violation/list.rb', line 5

def children
  @children
end

#violationsObject (readonly)

Returns the value of attribute violations.



5
6
7
# File 'lib/inspector/constraint/violation/list.rb', line 5

def violations
  @violations
end

Instance Method Details

#<<(violation) ⇒ Object Also known as: push



23
24
25
26
27
28
29
# 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

#[](property_path) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# 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) ⇒ Object



32
33
34
35
36
37
38
# 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 = "", &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# 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?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/inspector/constraint/violation/list.rb', line 12

def empty?
  @violations.empty? && @children.empty?
end

#inspectObject



84
85
86
87
88
89
# File 'lib/inspector/constraint/violation/list.rb', line 84

def inspect
  "#<violations %{violations} children=%{children}>" % {
    :violations    => @violations.inspect,
    :children      => @children.inspect
  }
end

#lengthObject



16
17
18
19
20
21
# 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

#to_sObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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