class LessonlyApi::Utils::CustomFieldConverter

Constants

CONVERSIONS

Public Class Methods

new(data_type) click to toggle source
# File lib/lessonly_api/utils/custom_field_converter.rb, line 14
def initialize(data_type)
  @data_type = data_type
end

Public Instance Methods

convert(value) click to toggle source
# File lib/lessonly_api/utils/custom_field_converter.rb, line 18
def convert(value)
  if @data_type.is_a?(Array)
    convert_array(value)
  elsif CONVERSIONS.key?(@data_type)
    CONVERSIONS[@data_type].call(value)
  else
    wrap(value, @data_type)
  end
end

Private Instance Methods

convert_array(array) click to toggle source
# File lib/lessonly_api/utils/custom_field_converter.rb, line 35
def convert_array(array)
  return [] if array.blank?

  array.map { |item| wrap(item, @data_type.first) }
end
wrap(value, data_type) click to toggle source
# File lib/lessonly_api/utils/custom_field_converter.rb, line 30
def wrap(value, data_type)
  data_type_class = "LessonlyApi::ResourceType::#{data_type.to_s.camelize}".constantize
  value.is_a?(LessonlyApi::ResourceType::Base) ? value : data_type_class.new(value)
end