Sha256: 6a0495abd09924ce46ab43f4136468cddc1bb782412cb2dae44b2b6ca167c82e

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

require 'super_mapper/version'

class SuperMapper

  ##
  # Define a new mapping schema from a source class, mapping getters to setters
  #
  # @param [Class] source_class the class that will act as the source for this mapping definition
  def define_mapping source_class, target_class, &block
    mapping_registry["#{source_class}-#{target_class}"] = block
  end

  ##
  # Maps a source object to a target object using previously registered mappings
  #
  # @param [Object] source the source object
  # @param [Object | Class] target the target object or class. If a class is given, it should have a no-args constructor since the new instance will be created calling +target.new+
  # @return [Object] the target object, or a new instance of the target class, filled with values coming from the source
  def map source, target
    target.is_a?(Class) ? map_object(source, target.new) : map_object(source, target)
  end

  private

  def mapping_registry
    @mapping_registry ||= {}
  end

  def map_object source, target
    mapping_proc = mapping_registry["#{source.class}-#{target.class}"]
    mapping_proc.call source, target
    target
  end

  class Error < StandardError;
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
super_mapper-0.1.0 lib/super_mapper.rb