module Restool::Traversal::Converter

Public Class Methods

convert(request_response, response_representation, representations) click to toggle source
# File lib/restool/traversal/converter.rb, line 19
def self.convert(request_response, response_representation, representations)
  object = Restool::Traversal::Object.new

  object.class.__send__(:attr_accessor, :_raw)
  object.__send__("_raw=", request_response)

  if request_response.is_a?(Array)
    request_response.map do |element|
      map_response_to_representation(response_representation, element, object, representations)
    end
  else
    map_response_to_representation(response_representation, request_response, object, representations)
  end
end

Private Class Methods

map_complex_field(value, field, object, representations) click to toggle source
# File lib/restool/traversal/converter.rb, line 67
def self.map_complex_field(value, field, object, representations)
  operation_representation = representations[field.type.to_sym]

  new_value = if value.is_a?(Array)
                value.map { |element| convert(element, operation_representation, representations) }
              else
                convert(value, operation_representation, representations)
              end

  set_var(object, field, new_value)
end
map_primitive_field(value, field, object) click to toggle source
# File lib/restool/traversal/converter.rb, line 57
def self.map_primitive_field(value, field, object)
  new_value = if value.is_a?(Array)
                value.map { |element| parse_value(field.type, element) }
              else
                parse_value(field.type, value)
              end

  set_var(object, field, new_value)
end
map_response_to_representation(representation, request_response, object, representations) click to toggle source
# File lib/restool/traversal/converter.rb, line 36
def self.map_response_to_representation(representation, request_response, object, representations)
  representation.fields.each do |field|
    value = request_response[field.key.to_s] || request_response[field.key.to_sym]

    object.class.__send__(:attr_accessor, var_name(field))

    if value.nil?
      set_var(object, field, nil)
      next
    end

    if Restool::Traversal::TRAVERSAL_TYPES.include?(field.type.to_sym)
      map_primitive_field(value, field, object)
    else
      map_complex_field(value, field, object, representations)
    end
  end

  object
end
parse_value(type, value) click to toggle source
# File lib/restool/traversal/converter.rb, line 87
def self.parse_value(type, value)
  case type
  when Restool::Traversal::TRAVERSAL_TYPE_STRING
    value.to_s
  when Restool::Traversal::TRAVERSAL_TYPE_INTEGER
    Integer(value)
  when Restool::Traversal::TRAVERSAL_TYPE_DECIMAL
    BigDecimal.new(scalar)
  when Restool::Traversal::TRAVERSAL_TYPE_BOOLEAN
    value.downcase == 'true'
  end
end
set_var(object, field, new_value) click to toggle source
# File lib/restool/traversal/converter.rb, line 79
def self.set_var(object, field, new_value)
  object.__send__("#{var_name(field)}=", new_value)
end
var_name(field) click to toggle source
# File lib/restool/traversal/converter.rb, line 83
def self.var_name(field)
  field.metonym || field.key
end