Sha256: 361ef2e28321ae58ac72f375d039716a09135a44148305feb1150a9c28b57ea2

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 KB

Contents

module Yaoc
  module MappingToClass

    def self.included(other)
      other.send(:include, MappingBase)
      other.send(:include, InstanceMethods)
    end

    module InstanceMethods
      def call(pre_created_object=nil)
        source_converted_to_hash_or_array = super()
        unless source_converted_to_hash_or_array.nil?
          if pre_created_object.nil?
            create_target_from_class(source_converted_to_hash_or_array)
          else
            fill_target_object(source_converted_to_hash_or_array, pre_created_object)
          end
        else
          nil
        end
      end

      def source_method
        self.target_source.respond_to?(:call) ? :call : :new
      end

      def create_target_from_class(args)
        array_based_constructor = args.is_a? Array

        if array_based_constructor
          self.target_source.send(source_method, *args)
        else
          self.target_source.send(source_method, args)
        end
      end

      def fill_target_object(attribute_hash, pre_created_object)
        raise "UnexpectedStrategy" unless attribute_hash.respond_to? :each_pair

        attribute_hash.each_pair do |key, value|
          pre_created_object.send("#{key}=", value)
        end

        pre_created_object
      end

      def to_a # wenn included into struct's Array(...) call's to_a
        [self]
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
yaoc-0.0.9 lib/yaoc/mapping_to_class.rb
yaoc-0.0.8 lib/yaoc/mapping_to_class.rb
yaoc-0.0.7 lib/yaoc/mapping_to_class.rb