Class: Schemacop::V2::Collector

Inherits:
Object
  • Object
show all
Defined in:
lib/schemacop/v2/collector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Collector

Returns a new instance of Collector.



5
6
7
8
9
10
11
# 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

Instance Attribute Details

#exceptionsObject (readonly)

Returns the value of attribute exceptions.



3
4
5
# File 'lib/schemacop/v2/collector.rb', line 3

def exceptions
  @exceptions
end

Instance Method Details

#dataObject



13
14
15
16
17
# File 'lib/schemacop/v2/collector.rb', line 13

def data
  return nil unless valid?

  @current_datappoint_path.first
end

#error(error_msg) ⇒ Object



68
69
70
71
72
73
# File 'lib/schemacop/v2/collector.rb', line 68

def error(error_msg)
  @exceptions << {
    path:    @current_path.dup,
    message: error_msg
  }
end

#exception_messageObject



60
61
62
63
64
65
66
# 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_segmentSchemacop::Collector

Does not include the path segment next time Collector.path is called.

Returns:

  • (Schemacop::Collector)


79
80
81
82
# File 'lib/schemacop/v2/collector.rb', line 79

def ignore_next_segment
  @ignore_next_segment = true
  return self
end

#override_value(value) ⇒ Object



52
53
54
55
56
57
58
# 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) ⇒ Object

Construct the current path



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/schemacop/v2/collector.rb', line 19

def valid?
  @exceptions.empty?
end