Class: Schemacop::Collector

Inherits:
Object
  • Object
show all
Defined in:
lib/schemacop/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/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/collector.rb', line 3

def exceptions
  @exceptions
end

Instance Method Details

#dataObject



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

def data
  return nil unless valid?
  @current_datappoint_path.first
end

#error(error_msg) ⇒ Object



65
66
67
68
69
70
# File 'lib/schemacop/collector.rb', line 65

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

#exception_messageObject



59
60
61
62
63
# File 'lib/schemacop/collector.rb', line 59

def exception_message
  return "Schemacop validation failed:\n" + @exceptions.map do |e|
    "- #{e[:path].join('')}: #{e[:message]}"
  end.join("\n")
end

#ignore_next_segmentSchemacop::Collector

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



76
77
78
79
# File 'lib/schemacop/collector.rb', line 76

def ignore_next_segment
  @ignore_next_segment = true
  return self
end

#override_value(value) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/schemacop/collector.rb', line 51

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



23
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
# File 'lib/schemacop/collector.rb', line 23

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)


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

def valid?
  @exceptions.empty?
end