Converter 1.0.3.0 → 1.1.0.0

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 +41 -9
  2. metadata +4 -3
data/lib/Converter.rb CHANGED
@@ -9,19 +9,23 @@ module Converter
9
9
  Converter.convert self, target_class
10
10
  end
11
11
 
12
- # Create new Target_Type instance according to the Conversion definition
13
- # one of the classes (source.class or target_type) should include Converter module
12
+ def copy_to target
13
+ Converter.copy self, target
14
+ end
15
+
16
+ # Update the given target object according to the Conversion definition
17
+ # one of the classes (source.class or target.class) should include Converter module
14
18
  # @param [Any Class] source instance to copy from
15
- # @param [class] target_type the result type
16
- # @return instance of target_type with data converted from source
17
- def self.convert(source, target_type, hash = {})
19
+ # @param [Any Class] target the result type
20
+ # @return target with new values
21
+ def self.copy source, target, hash = {}
18
22
  # Check if the object has already been converted (Circular pointing)
19
23
  if(converted_object = hash[source])
20
24
  return converted_object
21
25
  end
22
26
 
23
27
  # Create new target instance
24
- hash[source] = target = target_type.new
28
+ hash[source] = target
25
29
 
26
30
  # Gets source Conversion definition
27
31
  # Check which one includes Converter module
@@ -36,6 +40,15 @@ module Converter
36
40
  target
37
41
  end
38
42
 
43
+ # Create new Target_Type instance according to the Conversion definition
44
+ # one of the classes (source.class or target_type) should include Converter module
45
+ # @param [Any Class] source instance to copy from
46
+ # @param [class] target_type the result type
47
+ # @return instance of target_type with data converted from source
48
+ def self.convert(source, target_type, hash = {})
49
+ copy source, target_type.new, hash
50
+ end
51
+
39
52
  private
40
53
  def self.get_convertable_object source, target
41
54
  if source.class.included_modules.include? Converter
@@ -49,15 +62,18 @@ module Converter
49
62
 
50
63
  def self.convert_one_attribute source, target, conversion_metadata, is_source_convertable, hash
51
64
  source_attribute_name = is_source_convertable ? conversion_metadata.convertable_attribute_name : conversion_metadata.poro_attribute_name
52
- convert_block = is_source_convertable ? conversion_metadata.convert_from_convertable_block : conversion_metadata.convert_from_poro_block
53
65
  target_attribute_name = is_source_convertable ? conversion_metadata.poro_attribute_name : conversion_metadata.convertable_attribute_name
66
+ target_attribute_value = target.send(target_attribute_name)
54
67
  target_attribute_name = target_attribute_name.to_s.concat('=').to_sym
68
+ convert_block = conversion_metadata.get_converter(target_attribute_value, is_source_convertable)
55
69
 
56
70
  # Convert from one type to another (by default doesn't do anything)
57
71
  if convert_block.parameters.count == 1
58
72
  target_value = convert_block.call(source.send(source_attribute_name))
59
- else
73
+ elsif convert_block.parameters.count == 2
60
74
  target_value = convert_block.call(source.send(source_attribute_name), hash)
75
+ else
76
+ target_value = convert_block.call(source.send(source_attribute_name), target_attribute_value, hash)
61
77
  end
62
78
 
63
79
  target.send(target_attribute_name, target_value)
@@ -77,7 +93,7 @@ module Converter
77
93
  # Set default values for nil arguments
78
94
  poro_attribute_name ||= convertable_attribute_name
79
95
  @attribute_converter ||= {}
80
-
96
+
81
97
  if convert_block.class == Symbol
82
98
  convertable_type = convert_block.to_s
83
99
  poro_type = convert_back_block.to_s
@@ -101,11 +117,27 @@ module Converter
101
117
  @poro_attribute_name = poro_attribute_name
102
118
  @convert_from_convertable_block = convert_from_convertable_block
103
119
  @convert_from_poro_block = convert_from_poro_block
120
+ @copy_from_convertable_block = lambda { |source, target, hash| Converter.copy(source, target, hash) }
121
+ @copy_from_poro_block = lambda { |source, target, hash| Converter.copy(source, target , hash) }
104
122
  end
105
123
 
106
124
  attr_accessor :convertable_attribute_name
107
125
  attr_accessor :poro_attribute_name
108
126
  attr_accessor :convert_from_convertable_block
109
127
  attr_accessor :convert_from_poro_block
128
+ attr_accessor :copy_from_convertable_block
129
+ attr_accessor :copy_from_poro_block
130
+
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
137
+ convert_from_convertable_block
138
+ else
139
+ convert_from_poro_block
140
+ end
141
+ end
110
142
  end
111
143
  end
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.0.3.0
4
+ version: 1.1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,8 @@ bindir: bin
11
11
  cert_chain: []
12
12
  date: 2012-07-20 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: Add attr_converter to maps attributes to another object
14
+ description: Add attr_converter to maps attributes to another object, and this allows
15
+ you to copy or convert between them automatically
15
16
  email: shukyc19@gmail.com
16
17
  executables: []
17
18
  extensions: []
@@ -41,6 +42,6 @@ rubyforge_project:
41
42
  rubygems_version: 1.8.23
42
43
  signing_key:
43
44
  specification_version: 3
44
- summary: Convert between to object by mapping theirs attributes
45
+ summary: Convert or Copy between two objects by mapping theirs attributes
45
46
  test_files: []
46
47
  has_rdoc: