Class: Schemacop::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/schemacop/v3/result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root = nil, original_data = nil) ⇒ Result

Returns a new instance of Result.



6
7
8
9
10
11
# File 'lib/schemacop/v3/result.rb', line 6

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

Instance Attribute Details

#current_pathObject (readonly)

Returns the value of attribute current_path.



3
4
5
# File 'lib/schemacop/v3/result.rb', line 3

def current_path
  @current_path
end

#errorsObject (readonly)

Returns the value of attribute errors.



4
5
6
# File 'lib/schemacop/v3/result.rb', line 4

def errors
  @errors
end

Instance Method Details

#dataObject



17
18
19
20
21
22
23
# File 'lib/schemacop/v3/result.rb', line 17

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

#error(message) ⇒ Object



25
26
27
28
# File 'lib/schemacop/v3/result.rb', line 25

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

#exception_messageObject



34
35
36
# File 'lib/schemacop/v3/result.rb', line 34

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

#in_path(segment) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/schemacop/v3/result.rb', line 56

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

#messages(pad: 0, itemize: false) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/schemacop/v3/result.rb', line 38

def messages(pad: 0, itemize: false)
  messages = []

  item_str = itemize ? '- ' : nil

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

  return messages
end

#messages_by_pathObject



30
31
32
# File 'lib/schemacop/v3/result.rb', line 30

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

#pad_lines(string, pad = 2) ⇒ Object



52
53
54
# File 'lib/schemacop/v3/result.rb', line 52

def pad_lines(string, pad = 2)
  string.split("\n").map { |line| "#{' ' * pad}#{line}" }.join("\n")
end

#valid?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/schemacop/v3/result.rb', line 13

def valid?
  errors.empty?
end