Class: Edi::Amazon::EnumMapper

Inherits:
Object
  • Object
show all
Defined in:
app/services/edi/amazon/enum_mapper.rb

Instance Method Summary collapse

Constructor Details

#initialize(schema) ⇒ EnumMapper

Returns a new instance of EnumMapper.



2
3
4
# File 'app/services/edi/amazon/enum_mapper.rb', line 2

def initialize(schema)
  @schema = schema
end

Instance Method Details

#enum_editable?(field_path) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
# File 'app/services/edi/amazon/enum_mapper.rb', line 30

def enum_editable?(field_path)
  field = traverse_schema(field_path)
  return true unless field

  field['editable'].to_b
end

#enum_fieldsObject

Retrieve a list of all fields with enums



38
39
40
# File 'app/services/edi/amazon/enum_mapper.rb', line 38

def enum_fields
  collect_enum_fields(@schema['properties'])
end

#enum_mappings(field_path) ⇒ Object

Retrieve a hash of all enum names and values for a dot-separated field



18
19
20
21
22
23
24
25
26
27
28
# File 'app/services/edi/amazon/enum_mapper.rb', line 18

def enum_mappings(field_path)
  field = traverse_schema(field_path)
  return {} unless field

  enum_names = field['enumNames']
  enums = field['enum']

  return {} unless enum_names && enums

  enum_names.zip(enums).to_h
end

#enum_value(field_path, display_name) ⇒ Object

Retrieve the enum value for a given dot-separated field and display name
If the enum is editable, the display name can be used as is



8
9
10
11
12
13
14
15
# File 'app/services/edi/amazon/enum_mapper.rb', line 8

def enum_value(field_path, display_name)
  mappings = enum_mappings(field_path)
  return display_name unless mappings.present?

  v = mappings[display_name]
  v ||= display_name if enum_editable?(field_path)
  v
end