Converter 1.1.0.0 → 1.1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/Converter.rb +25 -12
  2. metadata +1 -1
data/lib/Converter.rb CHANGED
@@ -51,29 +51,36 @@ module Converter
51
51
 
52
52
  private
53
53
  def self.get_convertable_object source, target
54
- if source.class.included_modules.include? Converter
54
+ if source.class.included_modules.include?(Converter) && target.class.included_modules.include?(Converter)
55
+ if source.class == target.class
56
+ i = 7
57
+ else
58
+ raise ArgumentError.new "Unable to select from two Convertable objects"
59
+ end
60
+ elsif source.class.included_modules.include? Converter
55
61
  source
56
62
  elsif target.class.included_modules.include? Converter
57
63
  target
58
64
  else
59
- raise ArgumentError.new "One of the given classes should include Converter module"
65
+ raise ArgumentError.new "One of the given classes should include Converter module, classes: #{source.class}, #{target.class}"
60
66
  end
61
67
  end
62
68
 
63
69
  def self.convert_one_attribute source, target, conversion_metadata, is_source_convertable, hash
64
70
  source_attribute_name = is_source_convertable ? conversion_metadata.convertable_attribute_name : conversion_metadata.poro_attribute_name
65
71
  target_attribute_name = is_source_convertable ? conversion_metadata.poro_attribute_name : conversion_metadata.convertable_attribute_name
72
+ source_attribute_value = source.send(source_attribute_name)
66
73
  target_attribute_value = target.send(target_attribute_name)
67
74
  target_attribute_name = target_attribute_name.to_s.concat('=').to_sym
68
- convert_block = conversion_metadata.get_converter(target_attribute_value, is_source_convertable)
75
+ convert_block = conversion_metadata.get_converter(source_attribute_value, target_attribute_value, is_source_convertable)
69
76
 
70
77
  # Convert from one type to another (by default doesn't do anything)
71
78
  if convert_block.parameters.count == 1
72
- target_value = convert_block.call(source.send(source_attribute_name))
79
+ target_value = convert_block.call(source_attribute_value)
73
80
  elsif convert_block.parameters.count == 2
74
- target_value = convert_block.call(source.send(source_attribute_name), hash)
81
+ target_value = convert_block.call(source_attribute_value, hash)
75
82
  else
76
- target_value = convert_block.call(source.send(source_attribute_name), target_attribute_value, hash)
83
+ target_value = convert_block.call(source_attribute_value, target_attribute_value, hash)
77
84
  end
78
85
 
79
86
  target.send(target_attribute_name, target_value)
@@ -128,12 +135,18 @@ module Converter
128
135
  attr_accessor :copy_from_convertable_block
129
136
  attr_accessor :copy_from_poro_block
130
137
 
131
- def get_converter target_old_attribute_value, is_source_convertable
132
- if target_old_attribute_value && is_source_convertable
133
- copy_from_convertable_block
134
- elsif target_old_attribute_value && !is_source_convertable
135
- copy_from_poro_block
136
- elsif !target_old_attribute_value && is_source_convertable
138
+ def get_converter source_attribute_value, target_old_attribute_value, is_source_convertable
139
+ # Check if one of the attributes is Convertable
140
+ has_convertable_object = source_attribute_value.class.included_modules.include?(Converter) || target_old_attribute_value.class.included_modules.include?(Converter)
141
+
142
+ # Copy between inner attribute only if they are convertale and exists
143
+ if has_convertable_object && target_old_attribute_value
144
+ if is_source_convertable
145
+ copy_from_convertable_block
146
+ else
147
+ copy_from_poro_block
148
+ end
149
+ elsif is_source_convertable
137
150
  convert_from_convertable_block
138
151
  else
139
152
  convert_from_poro_block
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Converter
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0.0
4
+ version: 1.1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: