Module: Qwack::BaseArray

Includes:
Base
Defined in:
lib/qwack/base_array.rb

Overview

Base module for user defined arrays

Instance Method Summary collapse

Methods included from Base

#mock!, #validate!

Instance Method Details

#allowed_typesObject



10
11
12
13
14
15
# File 'lib/qwack/base_array.rb', line 10

def allowed_types
  ancestors.reverse.each_with_object([]) do |ancestor, obj|
    obj.append(*ancestor.own_allowed_types) \
      if ancestor.respond_to?(:own_allowed_types)
  end
end

#mock(input = nil) ⇒ Object



33
34
35
# File 'lib/qwack/base_array.rb', line 33

def mock(input = nil)
  input || [own_allowed_types.first&.mock]
end

#own_allowed_typesObject



17
18
19
# File 'lib/qwack/base_array.rb', line 17

def own_allowed_types
  @own_allowed_types ||= []
end

#validation_errors(input, path = 'root') ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/qwack/base_array.rb', line 21

def validation_errors(input, path = 'root')
  return [{ path: path, name: name, desc: 'Is not an array', item: input }] \
    unless input.is_a?(::Array)

  all_allowed_types = allowed_types
  input.each_with_object([]).with_index do |(item, errors), index|
    all_allowed_types.each do |type|
      errors.append(*type.validation_errors(item, "#{path}.#{index}"))
    end
  end
end