Class: Leftovers::ConfigLoader::StringEnumSchema

Inherits:
Schema
  • Object
show all
Defined in:
lib/leftovers/config_loader/string_enum_schema.rb

Class Method Summary collapse

Methods inherited from Schema

===, error

Class Method Details

.aliasesObject



14
15
16
# File 'lib/leftovers/config_loader/string_enum_schema.rb', line 14

def aliases
  @aliases ||= {}
end

.aliases_for(value) ⇒ Object



18
19
20
# File 'lib/leftovers/config_loader/string_enum_schema.rb', line 18

def aliases_for(value)
  aliases.select { |_k, v| v == value }.keys
end

.each_value(&block) ⇒ Object



26
27
28
# File 'lib/leftovers/config_loader/string_enum_schema.rb', line 26

def each_value(&block)
  @values.each(&block)
end

.to_ruby(node) ⇒ Object



30
31
32
# File 'lib/leftovers/config_loader/string_enum_schema.rb', line 30

def to_ruby(node)
  aliases[node.to_sym] || node.to_sym
end

.validate(node) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/leftovers/config_loader/string_enum_schema.rb', line 34

def validate(node)
  if node.string?
    node.error = error_message_with_suggestions(node) unless valid_value?(node.to_sym)
  else
    error(node, 'be a string')
  end

  super
end

.value(value, aliases: nil) ⇒ Object



7
8
9
10
11
12
# File 'lib/leftovers/config_loader/string_enum_schema.rb', line 7

def value(value, aliases: nil)
  values << value
  Array(aliases).each do |alias_name|
    self.aliases[alias_name] = value
  end
end

.valuesObject



22
23
24
# File 'lib/leftovers/config_loader/string_enum_schema.rb', line 22

def values
  @values ||= []
end