cwyckoff-babel_icious 0.0.4.2 → 0.0.4.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.
data/README.rdoc CHANGED
@@ -47,6 +47,8 @@ the slash indicates that "bar" is a nested hash within "foo". The target xml th
47
47
  with the value of "bar" from the hash above placed in the nested <foo> tags in the xml.
48
48
 
49
49
 
50
+ === Conditions
51
+
50
52
  You can also qualify your mappings with the methods "unless" and "when". For example, if you do not want to translate mappings that lack a value, simply add an "unless" method:
51
53
 
52
54
  m.map(:from => "foo/bar", :to => "bar/foo").unless(:empty)
@@ -60,6 +62,8 @@ if the value at "foo/bar" is empty or nil, "foo/bar" will not be translated. Ad
60
62
  will only translate if the value at "foo/bar" begins with a capital "M".
61
63
 
62
64
 
65
+ === Translation
66
+
63
67
  Finally, when you want to translate the mappings, simply call:
64
68
 
65
69
  Babelicious::Mapper.translate(:foo, source)
@@ -54,8 +54,8 @@ module Babelicious
54
54
  def mapping_already_exists?(key)
55
55
  mappings.keys.include?(key)
56
56
  end
57
+
57
58
  end
58
-
59
59
  end
60
60
 
61
61
  end
@@ -20,9 +20,9 @@ module Babelicious
20
20
  def value_from(source)
21
21
  hash = {}
22
22
  @path_translator.inject_with_index(hash) do |hsh, element, index|
23
- return hsh[element.to_sym] if (index == @path_translator.last_index)
23
+ return hsh[element.to_sym] if (index == @path_translator.last_index && index != 0)
24
24
  if hsh.empty?
25
- source[element.to_sym]
25
+ source_element(source, element)
26
26
  else
27
27
  hsh[element.to_sym]
28
28
  end
@@ -30,6 +30,10 @@ module Babelicious
30
30
  end
31
31
 
32
32
  private
33
+
34
+ def source_element(source, element)
35
+ source[element.to_sym] || source[element.to_s]
36
+ end
33
37
 
34
38
  def map_output(hash_output, source_value)
35
39
  catch :no_value do
@@ -37,11 +41,50 @@ module Babelicious
37
41
  if(hsh[element])
38
42
  hsh[element]
39
43
  else
40
- hsh[element] = (index == @path_translator.last_index ? source_value : {})
44
+ hsh[element] = (index == @path_translator.last_index ? map_source_value(source_value) : {})
41
45
  end
42
46
  end
43
47
  end
44
48
  end
45
49
 
50
+ def map_source_value(source_value)
51
+ if(@opts[:concatenate])
52
+ HashMappingStrategies::Concatenate.map(source_value, @opts[:concatenate])
53
+ else
54
+ source_value
55
+ end
56
+ end
57
+
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
46
89
  end
47
90
  end
@@ -18,7 +18,7 @@ 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(@opts)
21
+ @xml_value_mapper = XmlValueMapper.new(@path_translator, @opts)
22
22
  end
23
23
 
24
24
  def value_from(source)
@@ -76,15 +76,15 @@ module Babelicious
76
76
 
77
77
 
78
78
  class XmlValueMapper
79
-
80
- def initialize(opts={})
81
- @opts = opts
79
+
80
+ def initialize(path_translator, opts={})
81
+ @path_translator, @opts = path_translator, opts
82
82
  end
83
83
 
84
84
  def map(node)
85
85
  if(node.children.size > 1)
86
86
  content = {}
87
- return map_with_strategies_for_children(node, content)
87
+ map_child(node, content)
88
88
  else
89
89
  return node.content
90
90
  end
@@ -92,78 +92,56 @@ module Babelicious
92
92
 
93
93
  private
94
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)
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
100
102
  end
103
+ {node.name => content}
101
104
  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
-
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)
116
117
  end
117
118
  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)
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
154
130
  content[child.name] << child.child.content
155
131
  end
132
+ else
133
+ content[child.name] = ""
134
+ end
135
+ end
156
136
 
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
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)
165
143
  end
166
144
  end
167
-
168
145
  end
146
+
169
147
  end
@@ -21,6 +21,10 @@ module Babelicious
21
21
  @parsed_path.size - 1
22
22
  end
23
23
 
24
+ def last
25
+ @parsed_path.last
26
+ end
27
+
24
28
  def size
25
29
  @parsed_path.size
26
30
  end
@@ -43,7 +43,6 @@ module Babelicious
43
43
  def reset
44
44
  @mappings, @direction = [], nil
45
45
  end
46
-
47
46
  end
48
47
  end
49
48
 
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.2
4
+ version: 0.0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Wyckoff