Converter 1.0.0 → 1.0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Converter.rb +89 -0
- metadata +6 -7
- data/lib/Converter.rb +0 -50
data/Converter.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
# This module helps converting between to instances
|
2
|
+
# of different classes
|
3
|
+
module Converter
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(ClassConverter)
|
6
|
+
end
|
7
|
+
|
8
|
+
# Create new Target_Type instance according to Conversion definition
|
9
|
+
# @param [source] The instance to copy from
|
10
|
+
# @param [target_type] to target type to create instance of
|
11
|
+
# @return instance of target_type with data from source
|
12
|
+
def self.convert(source, target_type)
|
13
|
+
# Create new target instance
|
14
|
+
target = target_type.new
|
15
|
+
|
16
|
+
# Gets source Conversion definition
|
17
|
+
conversion_properties = source.class.class_eval { @attribute_converter}
|
18
|
+
|
19
|
+
# update each accessor on the target according to the conversion definition
|
20
|
+
source.instance_variables.each do |var|
|
21
|
+
var_name = var.to_s.delete('@').to_sym
|
22
|
+
|
23
|
+
# Get the conversion definition for the current accessor if exists
|
24
|
+
if convert_property = conversion_properties[var_name]
|
25
|
+
target_property_name = convert_property.target_property_name.to_s.concat('=').to_sym
|
26
|
+
|
27
|
+
# Convert from one type to another (by default doesn't do anything)
|
28
|
+
target_value = convert_property.convert_block.call(source.send(var_name))
|
29
|
+
target.send(target_property_name, target_value)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
target
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.convertBack(target, source_type)
|
37
|
+
# Create new target instance
|
38
|
+
source = source_type.new
|
39
|
+
|
40
|
+
# Gets source Conversion definition
|
41
|
+
conversion_properties = source.class.class_eval { @attribute_converter }
|
42
|
+
|
43
|
+
# update each accessor on the target according to the conversion definition
|
44
|
+
source.methods.grep(/[a-zA-Z0-9]=$/).each do |var_name|
|
45
|
+
var_name_trimmed = var_name.to_s.delete('=').to_sym
|
46
|
+
|
47
|
+
# Get the conversion definition for the current accessor if exists
|
48
|
+
if convert_property = conversion_properties[var_name_trimmed]
|
49
|
+
target_property_name = convert_property.target_property_name
|
50
|
+
|
51
|
+
# Convert from one type to another (by default doesn't do anything)
|
52
|
+
source_value = convert_property.convert_back_block.call(target.send(target_property_name))
|
53
|
+
source.send(var_name, source_value)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
source
|
58
|
+
end
|
59
|
+
|
60
|
+
# This module add class extension of attr_converter
|
61
|
+
module ClassConverter
|
62
|
+
def attr_converter(symbol, attr_another_name = nil, convert_bock = nil, convert_back_block = nil)
|
63
|
+
# Set default values for nil arguments
|
64
|
+
attr_another_name ||= symbol
|
65
|
+
convert_bock ||= lambda { |v| v }
|
66
|
+
convert_back_block ||= lambda { |v| v }
|
67
|
+
@attribute_converter ||= {}
|
68
|
+
|
69
|
+
# Create new ConversionMetadata
|
70
|
+
@attribute_converter[symbol] =ConversionMetadata.new(symbol, attr_another_name, convert_bock, convert_back_block)
|
71
|
+
attr_accessor symbol
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
# Represent conversion data of one property to another
|
76
|
+
class ConversionMetadata
|
77
|
+
def initialize(source_prop_name, target_prop_name, convert, convert_back)
|
78
|
+
@source_property_name = source_prop_name
|
79
|
+
@target_property_name = target_prop_name
|
80
|
+
@convert_block = convert
|
81
|
+
@convert_back_block = convert_back
|
82
|
+
end
|
83
|
+
|
84
|
+
attr_accessor :source_property_name
|
85
|
+
attr_accessor :target_property_name
|
86
|
+
attr_accessor :convert_block
|
87
|
+
attr_accessor :convert_back_block
|
88
|
+
end
|
89
|
+
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.0
|
4
|
+
version: 1.0.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,17 +9,16 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-07-19 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
|
-
description:
|
15
|
-
object
|
14
|
+
description: Add attr_converter attribute creator that aps attributes to another object
|
16
15
|
email: shukyc19@gmail.com
|
17
16
|
executables: []
|
18
17
|
extensions: []
|
19
18
|
extra_rdoc_files: []
|
20
19
|
files:
|
21
|
-
-
|
22
|
-
homepage: http://
|
20
|
+
- Converter.rb
|
21
|
+
homepage: http://rubygem.org/gems/Converter
|
23
22
|
licenses: []
|
24
23
|
post_install_message:
|
25
24
|
rdoc_options: []
|
@@ -42,5 +41,5 @@ rubyforge_project:
|
|
42
41
|
rubygems_version: 1.8.23
|
43
42
|
signing_key:
|
44
43
|
specification_version: 3
|
45
|
-
summary: Convert between to object by mapping
|
44
|
+
summary: Convert between to object by mapping theirs attributes
|
46
45
|
test_files: []
|
data/lib/Converter.rb
DELETED
@@ -1,50 +0,0 @@
|
|
1
|
-
module Converter
|
2
|
-
def self.included(base)
|
3
|
-
base.extend(ClassConverter)
|
4
|
-
end
|
5
|
-
|
6
|
-
def self.convert(source, targetType)
|
7
|
-
target = targetType.new
|
8
|
-
source.instance_variables.each do |var|
|
9
|
-
varName = var.to_s.delete('@').to_sym
|
10
|
-
converter = source.class.class_eval { @attribute_converter }
|
11
|
-
if converter.has_key? varName
|
12
|
-
targetPropertyName = converter[varName][0] || varName
|
13
|
-
targetPropertyName = targetPropertyName.to_s.concat('=').to_sym
|
14
|
-
targetValue = converter[varName][1] ?
|
15
|
-
converter[varName][1].call(source.send(varName)) :
|
16
|
-
source.send(varName)
|
17
|
-
|
18
|
-
target.send(targetPropertyName, targetValue)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
target
|
23
|
-
end
|
24
|
-
|
25
|
-
def self.ConvertBack(target, sourceType)
|
26
|
-
source = sourceType.new
|
27
|
-
source.methods.grep(/[a-zA-Z0-9]=$/).each do |varName|
|
28
|
-
varNameTrimmed = varName.to_s.delete('=').to_sym
|
29
|
-
converter = source.class.class_eval { @attribute_converter }
|
30
|
-
if converter.has_key? varNameTrimmed
|
31
|
-
targetPropertyName = converter[varNameTrimmed][0] || varNameTrimmed
|
32
|
-
sourceValue = converter[varNameTrimmed][2] ?
|
33
|
-
converter[varNameTrimmed][2].call(target.send(targetPropertyName)) :
|
34
|
-
target.send(targetPropertyName)
|
35
|
-
source.send(varName, sourceValue)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
source
|
40
|
-
end
|
41
|
-
|
42
|
-
module ClassConverter
|
43
|
-
def attr_converter(symbol, attr_another_name = nil, converterBlock = nil, converterBackBlock = nil)
|
44
|
-
@attribute_converter ||= {}
|
45
|
-
@attribute_converter[symbol] = [attr_another_name, converterBlock, converterBackBlock]
|
46
|
-
class_eval( "def #{symbol}() @#{symbol}; end" )
|
47
|
-
class_eval( "def #{symbol}=(val) @#{symbol} = val; end" )
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|