Converter 1.1.0.1 → 1.1.1.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 +31 -3
  2. metadata +4 -4
@@ -5,6 +5,10 @@ module Converter
5
5
  base.extend(ClassConverter)
6
6
  end
7
7
 
8
+ def clone
9
+ convert_to self.class
10
+ end
11
+
8
12
  def convert_to target_class
9
13
  Converter.convert self, target_class
10
14
  end
@@ -27,10 +31,14 @@ module Converter
27
31
  # Create new target instance
28
32
  hash[source] = target
29
33
 
30
- # Gets source Conversion definition
31
- # Check which one includes Converter module
34
+ # Check which of the objects includes Converter module
35
+ # and gets it's Conversion definition
32
36
  convertable_object = get_convertable_object source, target
33
37
  conversion_metadatas = convertable_object.class.class_eval { @attribute_converter}
38
+
39
+ if source.class == target.class
40
+ conversion_metadatas = build_conversion_metadata_to_clone conversion_metadatas
41
+ end
34
42
 
35
43
  # update each accessor on the target according to the attr_converters
36
44
  conversion_metadatas.values.each do |conversion_metadata|
@@ -48,12 +56,32 @@ module Converter
48
56
  def self.convert(source, target_type, hash = {})
49
57
  copy source, target_type.new, hash
50
58
  end
59
+
60
+ # Create new source.class instance according to the Conversion definition of source
61
+ # source should include Converter module
62
+ # @param [Any Class] source instance to copy from
63
+ # @return instance of source.class with data converted from source
64
+ def self.clone source
65
+ convert source, source.class
66
+ end
51
67
 
52
68
  private
69
+
70
+ def self.build_conversion_metadata_to_clone conversion_metadatas
71
+ default_converter = lambda { |source| source }
72
+ new_conversion_metadata = {}
73
+ conversion_metadatas.keys.each do |key|
74
+ metadata = conversion_metadatas[key]
75
+ new_conversion_metadata[key] = ConversionMetadata.new(metadata.convertable_attribute_name, metadata.convertable_attribute_name, default_converter, default_converter)
76
+ end
77
+
78
+ new_conversion_metadata
79
+ end
80
+
53
81
  def self.get_convertable_object source, target
54
82
  if source.class.included_modules.include?(Converter) && target.class.included_modules.include?(Converter)
55
83
  if source.class == target.class
56
- i = 7
84
+ source
57
85
  else
58
86
  raise ArgumentError.new "Unable to select from two Convertable objects"
59
87
  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.1.0.1
4
+ version: 1.1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,10 +9,10 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-20 00:00:00.000000000 Z
12
+ date: 2012-07-21 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Add attr_converter to maps attributes to another object, and this allows
15
- you to copy or convert between them automatically
15
+ you to copy, convert and clone between them automatically
16
16
  email: shukyc19@gmail.com
17
17
  executables: []
18
18
  extensions: []
@@ -42,6 +42,6 @@ rubyforge_project:
42
42
  rubygems_version: 1.8.23
43
43
  signing_key:
44
44
  specification_version: 3
45
- summary: Convert or Copy between two objects by mapping theirs attributes
45
+ summary: Convert, Copy and Clone between two objects by mapping theirs attributes
46
46
  test_files: []
47
47
  has_rdoc: