Converter 1.0.1.0 → 1.0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/Converter.rb +52 -13
- metadata +3 -3
data/lib/Converter.rb
CHANGED
@@ -6,17 +6,22 @@ module Converter
|
|
6
6
|
end
|
7
7
|
|
8
8
|
# Create new Target_Type instance according to Conversion definition
|
9
|
-
# @param [
|
10
|
-
# @param [
|
9
|
+
# @param [Any Class] source instance to copy from
|
10
|
+
# @param [class] target_type the result type
|
11
11
|
# @return instance of target_type with data from source
|
12
|
-
def self.convert(source, target_type)
|
12
|
+
def self.convert(source, target_type, hash = {})
|
13
13
|
# Create new target instance
|
14
|
-
|
14
|
+
if(converted_target = hash[source])
|
15
|
+
return converted_target
|
16
|
+
end
|
17
|
+
|
18
|
+
# Create new target instance
|
19
|
+
hash[source] = target = target_type.new
|
15
20
|
|
16
21
|
# Gets source Conversion definition
|
17
22
|
conversion_properties = source.class.class_eval { @attribute_converter}
|
18
23
|
|
19
|
-
# update each accessor on the target according to the
|
24
|
+
# update each accessor on the target according to the attr_converters
|
20
25
|
source.instance_variables.each do |var|
|
21
26
|
var_name = var.to_s.delete('@').to_sym
|
22
27
|
|
@@ -25,7 +30,12 @@ module Converter
|
|
25
30
|
target_property_name = convert_property.target_property_name.to_s.concat('=').to_sym
|
26
31
|
|
27
32
|
# Convert from one type to another (by default doesn't do anything)
|
28
|
-
|
33
|
+
if convert_property.convert_block.parameters.count == 1
|
34
|
+
target_value = convert_property.convert_block.call(source.send(var_name))
|
35
|
+
else
|
36
|
+
target_value = convert_property.convert_block.call(source.send(var_name), hash)
|
37
|
+
end
|
38
|
+
|
29
39
|
target.send(target_property_name, target_value)
|
30
40
|
end
|
31
41
|
end
|
@@ -33,9 +43,18 @@ module Converter
|
|
33
43
|
target
|
34
44
|
end
|
35
45
|
|
36
|
-
|
46
|
+
# Create new source_type instance according to attr_converters
|
47
|
+
# @param [Any Class] target instance to copy from
|
48
|
+
# @param [class] source_type the result type
|
49
|
+
# @return instance of source_type with data from source
|
50
|
+
def self.convert_back(target, source_type, hash = {})
|
37
51
|
# Create new target instance
|
38
|
-
|
52
|
+
if(converted_source = hash[target])
|
53
|
+
return converted_source
|
54
|
+
end
|
55
|
+
|
56
|
+
# Create new source instance
|
57
|
+
hash[target] = source = source_type.new
|
39
58
|
|
40
59
|
# Gets source Conversion definition
|
41
60
|
conversion_properties = source.class.class_eval { @attribute_converter }
|
@@ -49,7 +68,12 @@ module Converter
|
|
49
68
|
target_property_name = convert_property.target_property_name
|
50
69
|
|
51
70
|
# Convert from one type to another (by default doesn't do anything)
|
52
|
-
|
71
|
+
if convert_property.convert_back_block.parameters.count == 1
|
72
|
+
source_value = convert_property.convert_back_block.call(target.send(target_property_name))
|
73
|
+
else
|
74
|
+
source_value = convert_property.convert_back_block.call(target.send(target_property_name), hash)
|
75
|
+
end
|
76
|
+
|
53
77
|
source.send(var_name, source_value)
|
54
78
|
end
|
55
79
|
end
|
@@ -59,15 +83,30 @@ module Converter
|
|
59
83
|
|
60
84
|
# This module add class extension of attr_converter
|
61
85
|
module ClassConverter
|
62
|
-
|
86
|
+
# Create an attr_accessor and map this attribute as convertable ,
|
87
|
+
# this means that this attribute will be converted when calling to Convert/
|
88
|
+
# @param [symbol] symbol attribute name
|
89
|
+
# @param [symbol] another_name the name of the converted attribute(on the target)
|
90
|
+
# @param [block] convert_block block that convert the source data type to the target data type
|
91
|
+
# @param [block] convert_back_block block that convert the target data type to the source data type
|
92
|
+
def attr_converter(symbol, attr_another_name = nil, convert_block = nil, convert_back_block = nil)
|
63
93
|
# Set default values for nil arguments
|
64
94
|
attr_another_name ||= symbol
|
65
|
-
convert_bock ||= lambda { |v| v }
|
66
|
-
convert_back_block ||= lambda { |v| v }
|
67
95
|
@attribute_converter ||= {}
|
96
|
+
|
97
|
+
if convert_block.class == Symbol
|
98
|
+
source_type = convert_block.to_s
|
99
|
+
target_type = convert_back_block.to_s
|
100
|
+
convert_block = lambda { |s,h| Converter.convert(s, eval(target_type), h)}
|
101
|
+
convert_back_block = lambda { |t,h| Converter.convert_back(t, eval(source_type), h)}
|
102
|
+
else
|
103
|
+
convert_block ||= lambda { |source, hash| source }
|
104
|
+
convert_back_block ||= lambda { |target, hash| target }
|
105
|
+
end
|
106
|
+
|
68
107
|
|
69
108
|
# Create new ConversionMetadata
|
70
|
-
@attribute_converter[symbol] =ConversionMetadata.new(symbol, attr_another_name,
|
109
|
+
@attribute_converter[symbol] =ConversionMetadata.new(symbol, attr_another_name, convert_block, convert_back_block)
|
71
110
|
attr_accessor symbol
|
72
111
|
end
|
73
112
|
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.
|
4
|
+
version: 1.0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,9 +9,9 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-20 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
|
-
description: Add attr_converter
|
14
|
+
description: Add attr_converter to maps attributes to another object
|
15
15
|
email: shukyc19@gmail.com
|
16
16
|
executables: []
|
17
17
|
extensions: []
|