Sha256: 390c534e4926808aaf14e9375a712f6f50de3c7563e7dd0b5b7e37e6c165f673

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

require 'super_mapper/version'

class SuperMapper
  def initialize
    yield self if block_given?
  end

  ##
  # 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
    return if source.nil?

    raise ArgumentError, 'target cannot be nil' if target.nil?
    
    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.3.0 lib/super_mapper.rb