cwyckoff-babel_icious 0.0.3 → 0.0.4
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.
@@ -13,11 +13,11 @@ module Babelicious
|
|
13
13
|
end
|
14
14
|
|
15
15
|
end
|
16
|
-
|
17
|
-
def initialize(path_translator)
|
18
|
-
@path_translator = path_translator
|
19
|
-
end
|
20
16
|
|
17
|
+
def initialize(path_translator, opts={})
|
18
|
+
@path_translator, @opts = path_translator, opts
|
19
|
+
end
|
20
|
+
|
21
21
|
def map_from(hash_output, source_value)
|
22
22
|
catch :no_value do
|
23
23
|
@path_translator.inject_with_index(hash_output) do |hsh, element, index|
|
@@ -5,11 +5,11 @@ module Babelicious
|
|
5
5
|
class << self
|
6
6
|
|
7
7
|
def source(direction, opts={})
|
8
|
-
eval("Babelicious::#{direction[:from].to_s.capitalize}Map").new(PathTranslator.new(opts[:from]))
|
8
|
+
eval("Babelicious::#{direction[:from].to_s.capitalize}Map").new(PathTranslator.new(opts[:from]), opts)
|
9
9
|
end
|
10
10
|
|
11
11
|
def target(direction, opts={})
|
12
|
-
eval("Babelicious::#{direction[:to].to_s.capitalize}Map").new(PathTranslator.new(opts[:to]))
|
12
|
+
eval("Babelicious::#{direction[:to].to_s.capitalize}Map").new(PathTranslator.new(opts[:to]), opts)
|
13
13
|
end
|
14
14
|
|
15
15
|
end
|
@@ -3,10 +3,11 @@ class TargetMapperError < Exception; end
|
|
3
3
|
module Babelicious
|
4
4
|
|
5
5
|
class TargetMapper
|
6
|
-
attr_reader :mappings, :direction
|
6
|
+
attr_reader :mappings, :direction, :target
|
7
7
|
|
8
8
|
def initialize
|
9
9
|
@mappings = []
|
10
|
+
@target = nil
|
10
11
|
end
|
11
12
|
|
12
13
|
def direction=(dir)
|
@@ -17,25 +18,26 @@ module Babelicious
|
|
17
18
|
end
|
18
19
|
|
19
20
|
def translate(source)
|
20
|
-
target = nil
|
21
21
|
@mappings.each do |source_element, target_element|
|
22
|
-
target = target_element.class.initial_target if target.nil?
|
23
22
|
filtered_source = source_element.class.filter_source(source) if filtered_source.nil?
|
24
23
|
|
25
24
|
source_value = source_element.value_from(filtered_source)
|
26
|
-
target_element.map_from(target, source_value)
|
25
|
+
target_element.map_from(@target, source_value)
|
27
26
|
end
|
28
|
-
target
|
27
|
+
@target
|
29
28
|
end
|
30
29
|
|
31
30
|
def register_mapping(opts={})
|
32
31
|
raise TargetMapperError, "Both :from and :to keys must be set (e.g., {:from => \"foo/bar\", :to => \"bar/foo\")" unless (opts[:from] && opts[:to])
|
32
|
+
target = MapFactory.target(@direction, opts)
|
33
|
+
source = MapFactory.source(@direction, opts)
|
34
|
+
@target = target.class.initial_target if @target.nil?
|
33
35
|
|
34
|
-
@mappings << [
|
36
|
+
@mappings << [source, target]
|
35
37
|
end
|
36
38
|
|
37
39
|
def reset
|
38
|
-
@mappings, @direction = [], nil
|
40
|
+
@mappings, @direction, @target = [], nil, nil
|
39
41
|
end
|
40
42
|
|
41
43
|
end
|
data/lib/babel_icious/xml_map.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'xml'
|
2
2
|
|
3
3
|
module Babelicious
|
4
|
-
|
4
|
+
|
5
5
|
class XmlMap
|
6
6
|
|
7
7
|
class << self
|
@@ -16,13 +16,14 @@ module Babelicious
|
|
16
16
|
|
17
17
|
end
|
18
18
|
|
19
|
-
def initialize(path_translator)
|
20
|
-
@path_translator = path_translator
|
19
|
+
def initialize(path_translator, opts={})
|
20
|
+
@path_translator, @opts = path_translator, opts
|
21
|
+
@xml_value_mapper = XmlValueMapper.new(@opts)
|
21
22
|
end
|
22
23
|
|
23
24
|
def value_from(source)
|
24
25
|
source.find("/#{@path_translator.full_path}").each do |node|
|
25
|
-
return node
|
26
|
+
return @xml_value_mapper.map(node)
|
26
27
|
end
|
27
28
|
end
|
28
29
|
|
@@ -60,6 +61,7 @@ module Babelicious
|
|
60
61
|
if xml_output.root.nil?
|
61
62
|
xml_output.root = XML::Node.new(@path_translator[0])
|
62
63
|
end
|
64
|
+
|
63
65
|
end
|
64
66
|
|
65
67
|
def update_node?(xml_output, source_value)
|
@@ -72,4 +74,96 @@ module Babelicious
|
|
72
74
|
end
|
73
75
|
end
|
74
76
|
|
77
|
+
|
78
|
+
class XmlValueMapper
|
79
|
+
|
80
|
+
def initialize(opts={})
|
81
|
+
@opts = opts
|
82
|
+
end
|
83
|
+
|
84
|
+
def map(node)
|
85
|
+
if(node.children.size > 1)
|
86
|
+
content = {}
|
87
|
+
return map_with_strategies_for_children(node, content)
|
88
|
+
else
|
89
|
+
return node.content
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
private
|
94
|
+
|
95
|
+
def map_with_strategies_for_children(node, content)
|
96
|
+
if @opts[:concatenate]
|
97
|
+
XmlMappingStrategies::Concatenate.map(node, @opts[:concatenate])
|
98
|
+
else
|
99
|
+
XmlMappingStrategies::ChildNodeMapper.map(node, content)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
|
105
|
+
module XmlMappingStrategies
|
106
|
+
|
107
|
+
class Concatenate
|
108
|
+
|
109
|
+
class << self
|
110
|
+
|
111
|
+
def map(node, concat_value)
|
112
|
+
concatenated_children = node.children.inject('') {|a,b| a << "#{b.content}#{concat_value}"}
|
113
|
+
{node.name => concatenated_children.chop}
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
class ChildNodeMapper
|
120
|
+
|
121
|
+
class << self
|
122
|
+
|
123
|
+
def map(node, content)
|
124
|
+
node.children.each do |child|
|
125
|
+
if(content[child.name])
|
126
|
+
update_content_key(content, child)
|
127
|
+
else
|
128
|
+
create_content_key(content, child)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
{node.name => content}
|
132
|
+
end
|
133
|
+
|
134
|
+
private
|
135
|
+
|
136
|
+
def content_value_is_array?(content, child)
|
137
|
+
content[child.name].is_a?(Array)
|
138
|
+
end
|
139
|
+
|
140
|
+
def create_content_key(content, child)
|
141
|
+
unless grandchild_is_final_node(child)
|
142
|
+
content[child.name] = {child.child.name => child.child.content}
|
143
|
+
else
|
144
|
+
set_grandchild_value_in_array(content, child)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
def grandchild_is_final_node(child)
|
149
|
+
child.child.name == "text"
|
150
|
+
end
|
151
|
+
|
152
|
+
def set_grandchild_value_in_array(content, child)
|
153
|
+
content[child.name] = [] unless content_value_is_array?(content, child)
|
154
|
+
content[child.name] << child.child.content
|
155
|
+
end
|
156
|
+
|
157
|
+
def update_content_key(content, child)
|
158
|
+
unless grandchild_is_final_node(child)
|
159
|
+
content[child.name] = [content[child.name]] unless content_value_is_array?(content, child)
|
160
|
+
content[child.name] << {child.child.name => child.child.content}
|
161
|
+
else
|
162
|
+
set_grandchild_value_in_array(content, child)
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
end
|
75
169
|
end
|
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
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Wyckoff
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-04-
|
12
|
+
date: 2009-04-24 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|