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



50
51
52
53
54
55
56
# File 'lib/schemacop/v3/result.rb', line 50

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

#messagesObject



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

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

#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

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  errors.empty?
end