cwyckoff-babel_icious 0.0.5.1 → 0.0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/babel_icious.rb CHANGED
@@ -5,6 +5,7 @@ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) unless $LOAD_PATH.i
5
5
 
6
6
  require "babel_icious/core_ext/enumerable"
7
7
  require "babel_icious/core_ext/libxml_node"
8
+ require "babel_icious/map_rule"
8
9
  require "babel_icious/target_mapper"
9
10
  require "babel_icious/map_factory"
10
11
  require "babel_icious/path_translator"
@@ -2,20 +2,23 @@ require 'xml/libxml'
2
2
 
3
3
  module BabeliciousNodeHacks
4
4
 
5
- def concatenate_children(glue)
5
+ def to_a
6
6
  self.children.reject { }
7
- res = self.children.inject('') do |a,b|
7
+ res = self.children.inject([]) do |a,b|
8
8
  unless b.content.strip.empty?
9
- a << "#{b.content.strip}#{glue.strip}"
9
+ a << b.content.strip
10
10
  else
11
11
  a
12
12
  end
13
13
  end
14
- res.chop
14
+ end
15
+
16
+ def concatenate_children(glue)
17
+ to_a.join(glue)
15
18
  end
16
19
 
17
20
  def child_content(child)
18
- child_arr(child).first.content
21
+ child_arr(child).first.content unless child_arr(child).empty?
19
22
  end
20
23
 
21
24
  def child_name(child)
@@ -2,6 +2,14 @@ module Babelicious
2
2
 
3
3
  class BaseMap
4
4
  attr_reader :opts, :path_translator
5
+
6
+ def initialize_copy(other)
7
+ @opts = {}
8
+ other.opts.each do |key, value|
9
+ @opts[key] = value.dup
10
+ end
11
+ @path_translator = other.path_translator.dup
12
+ end
5
13
 
6
14
  def map_from(output, source_value)
7
15
  if map_condition?
@@ -29,6 +29,9 @@ module Babelicious
29
29
  return node.content
30
30
  end
31
31
  end
32
+
33
+ rescue
34
+ raise "There was a problem extracting the value from your xml at mapping '#{@path_translator.full_path}'"
32
35
  end
33
36
 
34
37
  protected
@@ -7,6 +7,11 @@ module Babelicious
7
7
  def initialize(untranslated_path)
8
8
  set_path(untranslated_path)
9
9
  end
10
+
11
+ def initialize_copy(other)
12
+ @full_path = other.full_path.dup
13
+ @parsed_path = other.parsed_path.dup
14
+ end
10
15
 
11
16
  def [](index)
12
17
  @parsed_path[index]
@@ -41,6 +46,11 @@ module Babelicious
41
46
  untranslated_path.gsub(/^\//, "").split("/")
42
47
  end
43
48
 
49
+ def unshift(element)
50
+ @parsed_path.unshift(element)
51
+ @full_path = "#{element}/" << @full_path
52
+ end
53
+
44
54
  end
45
55
 
46
56
  end
@@ -18,36 +18,36 @@ module Babelicious
18
18
 
19
19
  def translate(source)
20
20
  target = nil
21
- @mappings.each do |source_element, target_element|
22
- target = target_element.class.initial_target if target.nil?
23
- filtered_source = source_element.class.filter_source(source) if filtered_source.nil?
21
+ @mappings.each do |rule|
22
+ target = rule.initial_target if target.nil?
23
+ filtered_source = rule.filtered_source(source) if filtered_source.nil?
24
24
 
25
- source_value = source_element.value_from(filtered_source)
26
- process_target(target, target_element, source_value)
25
+ source_value = rule.source.value_from(filtered_source)
26
+ rule.translate(target, source_value)
27
27
  end
28
28
  target
29
29
  end
30
30
 
31
31
  def register_condition(condition_key, condition=nil, &block)
32
- @mappings.last[1].register_condition(condition_key, condition, &block)
32
+ @mappings.last.target.register_condition(condition_key, condition, &block)
33
33
  end
34
34
 
35
35
  def register_customized(&block)
36
- @mappings.last[1].register_customized(&block)
36
+ @mappings.last.target.register_customized(&block)
37
37
  end
38
38
 
39
39
  def register_from(from_str)
40
40
  raise TargetMapperError, "Please specify a source mapping" if from_str.nil?
41
41
  source = MapFactory.source(@direction, {:from => from_str})
42
42
 
43
- @mappings << [source]
43
+ @mappings << MapRule.new(source)
44
44
  end
45
45
 
46
46
  def register_to(&block)
47
47
  raise TargetMapperError, "You must call the .from method before customizing the .to method (e.g., m.from(\"foo\").to {|value| ...}" unless @mappings.last
48
48
 
49
49
  target = MapFactory.target(@direction, {:to => '', :to_proc => block})
50
- @mappings.last << target
50
+ @mappings.last.target = target
51
51
  end
52
52
 
53
53
  def register_include(map_definition=nil, opts={})
@@ -55,43 +55,27 @@ module Babelicious
55
55
  raise TargetMapperError, "Mapping definition for #{map_definition} does not exist" unless (other_mapper = Mapper[map_definition.to_sym])
56
56
 
57
57
  other_mapper.mappings.each do |m|
58
- source = MapFactory.source(@direction, m[0].opts)
59
- target = MapFactory.target(@direction, m[1].opts.merge(:to => to_path(m, opts)))
58
+ source = m.source.dup
59
+ target = m.target.dup
60
+
61
+ if opts[:inside_of]
62
+ source.path_translator.unshift(opts[:inside_of])
63
+ target.path_translator.unshift(opts[:inside_of])
64
+ end
60
65
 
61
- @mappings << [source, target]
66
+ @mappings << MapRule.new(source, target)
62
67
  end
63
68
  end
64
69
 
65
70
  def register_mapping(opts={})
66
71
  raise TargetMapperError, "Both :from and :to keys must be set (e.g., {:from => \"foo/bar\", :to => \"bar/foo\")" unless (opts[:from] && opts[:to])
67
- target = MapFactory.target(@direction, opts)
68
- source = MapFactory.source(@direction, opts)
69
-
70
- @mappings << [source, target]
72
+
73
+ @mappings << MapRule.new(MapFactory.source(@direction, opts), MapFactory.target(@direction, opts))
71
74
  end
72
75
 
73
76
  def reset
74
77
  @mappings, @direction = [], nil
75
78
  end
76
-
77
- private
78
-
79
- def process_target(target, target_element, source_value)
80
- if(target_element.opts[:to_proc])
81
- target_element.path_translator.set_path(target_element.opts[:to_proc].call(source_value))
82
- target_element.map_from(target, source_value)
83
- else
84
- target_element.map_from(target, source_value)
85
- end
86
- end
87
-
88
- def to_path(mappings, opts)
89
- if opts[:inside_of]
90
- to_path = mappings[1].path_translator.append(opts[:inside_of])
91
- else
92
- to_path = mappings[1].path_translator.full_path
93
- end
94
- end
95
79
  end
96
80
  end
97
81
 
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.5.1
4
+ version: 0.0.6.0
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-06-18 00:00:00 -07:00
12
+ date: 2009-06-20 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency