Sha256: 3c4a91402d837774b6011a800ead516d6ec90a8ea08a0af51ebd3c3f40e5e432

Contents?: true

Size: 1.8 KB

Versions: 2

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

module DryObjectMapper
  class Mapper
    @@definitions = {}

    def self.definitions
      @@definitions
    end

    def self.call(model_object, struct_dto, options = {})
      if @@definitions[struct_dto]
        schema_definition = @@definitions[struct_dto]
      else
        schema_definition = DryStructParser::StructSchemaParser.new.call(struct_dto).keys
        @@definitions[struct_dto] = schema_definition
      end

      if model_object.respond_to?(:each)
        model_object = model_object.to_a
        result = model_object.map do |it|
          struct_dto.new(get_model_hash_from_definition(it, schema_definition, options))
        end
      else
        result = struct_dto.new(get_model_hash_from_definition(model_object, schema_definition, options))
      end

      result
    end

    def self.get_model_hash_from_definition(model_object, schema_definition, options)
      result = {}
      schema_definition.each do |field_name, definition|
        if options && options.key?(field_name.to_sym)
          result[field_name] = options[field_name.to_sym]
        elsif definition[:type] == "hash"
          if model_object.send(field_name).nil?
            result[field_name] = nil
          else
            result[field_name] =
              get_model_hash_from_definition(model_object.send(field_name), definition[:keys], options&.dig(field_name))
          end
        elsif definition[:type] == "array" && !definition[:keys]&.empty?
          result[field_name] = []
          model_object.send(field_name).each do |object|
            result[field_name] << get_model_hash_from_definition(object, definition[:keys], options&.dig(field_name))
          end
        else
          result[field_name] = model_object.send(field_name)
        end
      end

      result
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dry_object_mapper-0.1.3 lib/dry_object_mapper/mapper.rb
dry_object_mapper-0.1.2 lib/dry_object_mapper/mapper.rb