Module: Qwack::BaseEnum

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

Overview

Base module for user defined enums

Instance Method Summary collapse

Methods included from Base

#mock!, #validate!

Instance Method Details

#allowed_valuesObject



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

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

#mock(input = nil) ⇒ Object



28
29
30
# File 'lib/qwack/base_enum.rb', line 28

def mock(input = nil)
  input || own_allowed_values&.first
end

#own_allowed_valuesObject



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

def own_allowed_values
  @own_allowed_values ||= []
end

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



21
22
23
24
25
26
# File 'lib/qwack/base_enum.rb', line 21

def validation_errors(input, path = 'root')
  return [{ path: path, name: name, desc: 'Is not an allowed value', item: input }] \
    unless allowed_values.include?(input)

  []
end