Module: Yaoc::MappingToClass::InstanceMethods

Defined in:
lib/yaoc/mapping_to_class.rb

Instance Method Summary collapse

Instance Method Details

#call(pre_created_object = nil) ⇒ Object Also known as: to_object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/yaoc/mapping_to_class.rb', line 9

def call(pre_created_object = nil)
  source_converted_to_hash_or_array = to_hash_or_array
  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
    pre_created_object
  end
end

#create_target_from_class(args) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/yaoc/mapping_to_class.rb', line 27

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

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

#fill_target_object(attribute_hash, pre_created_object) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/yaoc/mapping_to_class.rb', line 37

def fill_target_object(attribute_hash, pre_created_object)
  fail '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

#source_methodObject



23
24
25
# File 'lib/yaoc/mapping_to_class.rb', line 23

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

#to_aObject

wenn included into struct’s Array(…) call’s to_a



47
48
49
# File 'lib/yaoc/mapping_to_class.rb', line 47

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