cwyckoff-babel_icious 0.0.4.5 → 0.0.4.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/babel_icious.rb +1 -0
- data/lib/babel_icious/core_ext/libxml_node.rb +28 -0
- data/lib/babel_icious/mapper.rb +7 -2
- data/lib/babel_icious/mappers/base_map.rb +4 -0
- data/lib/babel_icious/mappers/hash_map.rb +2 -33
- data/lib/babel_icious/mappers/xml_map.rb +5 -72
- data/lib/babel_icious/target_mapper.rb +4 -0
- metadata +2 -1
data/lib/babel_icious.rb
CHANGED
@@ -4,6 +4,7 @@ require 'xml'
|
|
4
4
|
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) unless $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__)))
|
5
5
|
|
6
6
|
require "babel_icious/core_ext/enumerable"
|
7
|
+
require "babel_icious/core_ext/libxml_node"
|
7
8
|
require "babel_icious/target_mapper"
|
8
9
|
require "babel_icious/map_factory"
|
9
10
|
require "babel_icious/path_translator"
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'xml/libxml'
|
2
|
+
|
3
|
+
module BabeliciousNodeHacks
|
4
|
+
|
5
|
+
def concatenate_children(glue)
|
6
|
+
res = self.children.inject('') {|a,b| a << "#{b.content}#{glue}"}
|
7
|
+
res.chop
|
8
|
+
end
|
9
|
+
|
10
|
+
def child_content(child)
|
11
|
+
child_arr(child).first.content
|
12
|
+
end
|
13
|
+
|
14
|
+
def child_name(child)
|
15
|
+
child_arr(child).first.name
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def child_arr(child)
|
21
|
+
child_arr = self.find(child)
|
22
|
+
raise "Cannot retrieve content or name for child node #{child}. Current node has more than one child." if child_arr.size > 1
|
23
|
+
child_arr
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
class XML::Node; include BabeliciousNodeHacks; end
|
data/lib/babel_icious/mapper.rb
CHANGED
@@ -14,6 +14,10 @@ module Babelicious
|
|
14
14
|
yield self
|
15
15
|
end
|
16
16
|
|
17
|
+
def customize(&block)
|
18
|
+
current_target_mapper.register_customized(&block)
|
19
|
+
end
|
20
|
+
|
17
21
|
def direction(dir={})
|
18
22
|
current_target_mapper.direction = @direction = dir
|
19
23
|
end
|
@@ -41,8 +45,9 @@ module Babelicious
|
|
41
45
|
current_target_mapper.register_condition(:when, nil, &block)
|
42
46
|
end
|
43
47
|
|
44
|
-
def unless(condition)
|
45
|
-
current_target_mapper.register_condition(:unless, condition)
|
48
|
+
def unless(condition=nil, &block)
|
49
|
+
current_target_mapper.register_condition(:unless, condition, &block)
|
50
|
+
self
|
46
51
|
end
|
47
52
|
|
48
53
|
private
|
@@ -48,43 +48,12 @@ module Babelicious
|
|
48
48
|
end
|
49
49
|
|
50
50
|
def map_source_value(source_value)
|
51
|
-
if(@
|
52
|
-
|
51
|
+
if(@customized_map)
|
52
|
+
@customized_map.call(source_value)
|
53
53
|
else
|
54
54
|
source_value
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
58
|
end
|
59
|
-
|
60
|
-
module HashMappingStrategies
|
61
|
-
|
62
|
-
class Concatenate
|
63
|
-
|
64
|
-
class << self
|
65
|
-
|
66
|
-
def map(source_value, concat)
|
67
|
-
if(source_value.kind_of?(Hash))
|
68
|
-
concat_values_from_hash(source_value).join(concat)
|
69
|
-
elsif(source_value.kind_of?(Array))
|
70
|
-
source_value.join(concat)
|
71
|
-
else
|
72
|
-
source_value
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
private
|
77
|
-
|
78
|
-
def concat_values_from_hash(source_value)
|
79
|
-
source_value.each do |key, value|
|
80
|
-
unless value.is_a?(Array)
|
81
|
-
return concat_values_from_hash(value)
|
82
|
-
else
|
83
|
-
return value
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
59
|
end
|
@@ -18,12 +18,15 @@ module Babelicious
|
|
18
18
|
|
19
19
|
def initialize(path_translator, opts={})
|
20
20
|
@path_translator, @opts = path_translator, opts
|
21
|
-
@xml_value_mapper = XmlValueMapper.new(@path_translator, @opts)
|
22
21
|
end
|
23
22
|
|
24
23
|
def value_from(source)
|
25
24
|
source.find("/#{@path_translator.full_path}").each do |node|
|
26
|
-
|
25
|
+
if(node.children.size > 1)
|
26
|
+
return node
|
27
|
+
else
|
28
|
+
return node.content
|
29
|
+
end
|
27
30
|
end
|
28
31
|
end
|
29
32
|
|
@@ -74,74 +77,4 @@ module Babelicious
|
|
74
77
|
end
|
75
78
|
end
|
76
79
|
|
77
|
-
|
78
|
-
class XmlValueMapper
|
79
|
-
|
80
|
-
def initialize(path_translator, opts={})
|
81
|
-
@path_translator, @opts = path_translator, opts
|
82
|
-
end
|
83
|
-
|
84
|
-
def map(node)
|
85
|
-
if(node.children.size > 1)
|
86
|
-
content = {}
|
87
|
-
map_child(node, content)
|
88
|
-
else
|
89
|
-
return node.content
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
private
|
94
|
-
|
95
|
-
def map_child(node, content)
|
96
|
-
node.each_element do |child|
|
97
|
-
if(content[child.name])
|
98
|
-
update_content_key(content, child)
|
99
|
-
else
|
100
|
-
create_content_key(content, child)
|
101
|
-
end
|
102
|
-
end
|
103
|
-
{node.name => content}
|
104
|
-
end
|
105
|
-
|
106
|
-
private
|
107
|
-
|
108
|
-
def content_value_is_array?(content, child)
|
109
|
-
content[child.name].is_a?(Array)
|
110
|
-
end
|
111
|
-
|
112
|
-
def create_content_key(content, child)
|
113
|
-
unless final_node?(child)
|
114
|
-
content[child.name] = {child.child.name => child.child.content}
|
115
|
-
else
|
116
|
-
set_value_in_array(content, child)
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
def final_node?(child)
|
121
|
-
!child.children? || child.child.name == "text"
|
122
|
-
end
|
123
|
-
|
124
|
-
def set_value_in_array(content, child)
|
125
|
-
content[child.name] = [] unless content_value_is_array?(content, child)
|
126
|
-
if(child.children?)
|
127
|
-
if (child.parent.find(child.name)).to_a.size == 1
|
128
|
-
content[child.name] = child.child.content
|
129
|
-
else
|
130
|
-
content[child.name] << child.child.content
|
131
|
-
end
|
132
|
-
else
|
133
|
-
content[child.name] = ""
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
def update_content_key(content, child)
|
138
|
-
unless final_node?(child)
|
139
|
-
content[child.name] = [content[child.name]] unless content_value_is_array?(content, child)
|
140
|
-
content[child.name] << {child.child.name => child.child.content}
|
141
|
-
else
|
142
|
-
set_value_in_array(content, child)
|
143
|
-
end
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
80
|
end
|
@@ -32,6 +32,10 @@ module Babelicious
|
|
32
32
|
@mappings.last[1].register_condition(condition_key, condition, &block)
|
33
33
|
end
|
34
34
|
|
35
|
+
def register_customized(&block)
|
36
|
+
@mappings.last[1].register_customized(&block)
|
37
|
+
end
|
38
|
+
|
35
39
|
def register_mapping(opts={})
|
36
40
|
raise TargetMapperError, "Both :from and :to keys must be set (e.g., {:from => \"foo/bar\", :to => \"bar/foo\")" unless (opts[:from] && opts[:to])
|
37
41
|
target = MapFactory.target(@direction, opts)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cwyckoff-babel_icious
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.4.
|
4
|
+
version: 0.0.4.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Wyckoff
|
@@ -42,6 +42,7 @@ files:
|
|
42
42
|
- lib/babel_icious/mapper.rb
|
43
43
|
- lib/babel_icious/path_translator.rb
|
44
44
|
- lib/babel_icious/target_mapper.rb
|
45
|
+
- lib/babel_icious/core_ext/libxml_node.rb
|
45
46
|
- lib/babel_icious/core_ext/enumerable.rb
|
46
47
|
- README.rdoc
|
47
48
|
- MIT-LICENSE
|