Module: Generator::KnownValues

Defined in:
lib/generator/support/known_values.rb

Overview

Amazon's enum lists are documentation, not a contract: they change without a version bump, one spec can spell a value two ways, and the API can return values the spec doesn't list. Fields stay String; the values are listed in the docs as known values.

Class Method Summary collapse

Class Method Details

.append(description, values) ⇒ Object

Appends the known values to a description, unless it already lists them all.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/generator/support/known_values.rb', line 27

def append(description, values)
  return description if values.empty?
  return description if description && values.all? { |value| description.include?(value.to_s) }

  sentence = "Known values: #{values.map { |value| "`#{value}`" }.join(", ")}."
  return sentence unless description

  description = description.rstrip
  description += "." unless description.end_with?(".", "!", "?")
  "#{description} #{sentence}"
end

.deref(schema, definitions) ⇒ Object



39
40
41
42
43
44
# File 'lib/generator/support/known_values.rb', line 39

def deref(schema, definitions)
  ref = schema["$ref"]
  return schema unless ref && definitions

  definitions[ref.split("/").last] || schema
end

.enum_values(schema, definitions = nil) ⇒ Object

Enum values a schema declares inline, through a $ref, or on its array items.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/generator/support/known_values.rb', line 12

def enum_values(schema, definitions = nil)
  return [] unless schema.is_a?(Hash)

  schema = deref(schema, definitions)
  return schema["enum"] if schema["enum"].is_a?(Array)

  items = schema["items"]
  items = items.first if items.is_a?(Array)
  return [] unless items.is_a?(Hash)

  items = deref(items, definitions)
  items["enum"].is_a?(Array) ? items["enum"] : []
end