cwyckoff-babel_icious 0.0.6.3 → 0.0.6.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,15 @@
1
1
  require 'xml/libxml'
2
2
 
3
- def new_node(name)
4
- XML::Node.new(name)
3
+ def new_node(name, val=nil)
4
+ node = XML::Node.new(name)
5
+
6
+ if(val)
7
+ node << val
8
+ else
9
+ yield node if block_given?
10
+ end
11
+
12
+ node
5
13
  end
6
14
 
7
15
  module BabeliciousNodeHacks
@@ -3,10 +3,10 @@ class MapDefinitionError < Exception; end
3
3
  module Babelicious
4
4
 
5
5
  class MapDefinition
6
- attr_reader :mappings, :direction
6
+ attr_reader :rules, :direction
7
7
 
8
8
  def initialize
9
- @mappings = []
9
+ @rules = []
10
10
  end
11
11
 
12
12
  def direction=(dir)
@@ -18,43 +18,43 @@ module Babelicious
18
18
 
19
19
  def translate(source)
20
20
  target = nil
21
- @mappings.each do |rule|
21
+ @rules.each do |rule|
22
22
  target = rule.initial_target if target.nil?
23
23
  filtered_source = rule.filtered_source(source) if filtered_source.nil?
24
24
 
25
- source_value = rule.source.value_from(filtered_source)
25
+ source_value = rule.source_value(filtered_source)
26
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.target.register_condition(condition_key, condition, &block)
32
+ @rules.last.target.register_condition(condition_key, condition, &block)
33
33
  end
34
34
 
35
35
  def register_customized(&block)
36
- @mappings.last.target.register_customized(&block)
36
+ @rules.last.target.register_customized(&block)
37
37
  end
38
38
 
39
39
  def register_from(from_str)
40
40
  raise MapDefinitionError, "Please specify a source mapping" if from_str.nil?
41
41
  source = MapFactory.source(@direction, {:from => from_str})
42
42
 
43
- @mappings << MapRule.new(source)
43
+ @rules << MapRule.new(source)
44
44
  end
45
45
 
46
46
  def register_to(&block)
47
- raise MapDefinitionError, "You must call the .from method before customizing the .to method (e.g., m.from(\"foo\").to {|value| ...}" unless @mappings.last
47
+ raise MapDefinitionError, "You must call the .from method before customizing the .to method (e.g., m.from(\"foo\").to {|value| ...}" unless @rules.last
48
48
 
49
49
  target = MapFactory.target(@direction, {:to => '', :to_proc => block})
50
- @mappings.last.target = target
50
+ @rules.last.target = target
51
51
  end
52
52
 
53
53
  def register_include(map_definition=nil, opts={})
54
54
  raise MapDefinitionError, "A mapping definition key is required (e.g., m.include(:another_map))" if map_definition.nil?
55
55
  raise MapDefinitionError, "Mapping definition for #{map_definition} does not exist" unless (other_mapper = Mapper[map_definition.to_sym])
56
56
 
57
- other_mapper.mappings.each do |m|
57
+ other_mapper.rules.each do |m|
58
58
  source = m.source.dup
59
59
  target = m.target.dup
60
60
 
@@ -63,18 +63,18 @@ module Babelicious
63
63
  target.path_translator.unshift(opts[:inside_of])
64
64
  end
65
65
 
66
- @mappings << MapRule.new(source, target)
66
+ @rules << MapRule.new(source, target)
67
67
  end
68
68
  end
69
69
 
70
- def register_mapping(opts={})
70
+ def register_rule(opts={})
71
71
  raise MapDefinitionError, "Both :from and :to keys must be set (e.g., {:from => \"foo/bar\", :to => \"bar/foo\")" unless (opts[:from] && opts[:to])
72
72
 
73
- @mappings << MapRule.new(MapFactory.source(@direction, opts), MapFactory.target(@direction, opts))
73
+ @rules << MapRule.new(MapFactory.source(@direction, opts), MapFactory.target(@direction, opts))
74
74
  end
75
75
 
76
76
  def reset
77
- @mappings, @direction = [], nil
77
+ @rules, @direction = [], nil
78
78
  end
79
79
  end
80
80
  end
@@ -15,6 +15,18 @@ module Babelicious
15
15
  @target.class.initial_target
16
16
  end
17
17
 
18
+ def source_path
19
+ @source.path_translator.full_path
20
+ end
21
+
22
+ def source_value(src)
23
+ @source.value_from(src)
24
+ end
25
+
26
+ def target_path
27
+ @target.path_translator.full_path
28
+ end
29
+
18
30
  def translate(target_data, source_value)
19
31
  if(@target.opts[:to_proc])
20
32
  @target.path_translator.set_path(@target.opts[:to_proc].call(source_value))
@@ -8,7 +8,7 @@ module Babelicious
8
8
  attr_reader :direction, :current_map_definition_key
9
9
 
10
10
  def [](key)
11
- mappings[key.to_sym]
11
+ definitions[key.to_sym]
12
12
  end
13
13
 
14
14
  def config(key)
@@ -37,16 +37,16 @@ module Babelicious
37
37
  end
38
38
 
39
39
  def map(opts={})
40
- current_map_definition.register_mapping(opts)
40
+ current_map_definition.register_rule(opts)
41
41
  self
42
42
  end
43
43
 
44
- def mappings
45
- @mapped_targets ||= {}
44
+ def definitions
45
+ @map_definitions ||= {}
46
46
  end
47
47
 
48
48
  def reset
49
- @mapped_targets, @direction = nil, {}
49
+ @map_definitions, @direction = nil, {}
50
50
  end
51
51
 
52
52
  def to(&block)
@@ -55,9 +55,9 @@ module Babelicious
55
55
  end
56
56
 
57
57
  def translate(key=nil, source=nil)
58
- raise MapperError, "No target mapper exists for key #{key}" unless mappings.has_key?(key)
58
+ raise MapperError, "No target mapper exists for key #{key}" unless definitions.has_key?(key)
59
59
 
60
- mappings[key].translate(source)
60
+ definitions[key].translate(source)
61
61
  end
62
62
 
63
63
  def when(&block)
@@ -72,11 +72,11 @@ module Babelicious
72
72
  private
73
73
 
74
74
  def current_map_definition
75
- mappings[@current_map_definition_key] ||= MapDefinition.new
75
+ definitions[@current_map_definition_key] ||= MapDefinition.new
76
76
  end
77
77
 
78
78
  def mapping_already_exists?(key)
79
- mappings.keys.include?(key)
79
+ definitions.keys.include?(key)
80
80
  end
81
81
 
82
82
  end
@@ -26,7 +26,7 @@ module Babelicious
26
26
  def register_customized(&block)
27
27
  @customized_map = block
28
28
  end
29
-
29
+
30
30
  protected
31
31
 
32
32
  def map_condition
@@ -22,17 +22,18 @@ module Babelicious
22
22
  def value_from(source)
23
23
  hash = {}
24
24
  element = ""
25
+ return source if (@path_translator.full_path == "" || @path_translator.full_path == "/")
25
26
  @path_translator.inject_with_index(hash) do |hsh, element, index|
26
- return hsh[element.to_sym] if (index == @path_translator.last_index && index != 0)
27
+ return source_element(hsh, element) if (index == @path_translator.last_index && index != 0)
27
28
  if hsh.empty?
28
29
  source_element(source, element)
29
30
  else
30
- hsh[element.to_sym]
31
+ source_element(hsh, element)
31
32
  end
32
33
  end
33
34
 
34
- rescue
35
- raise "There was a problem extracting the value from your hash. It seems to be missing element '#{element}'"
35
+ rescue Exception => e
36
+ raise "There was a problem extracting the value from your hash at map definition source path '#{@path_translator.full_path}'."
36
37
  end
37
38
 
38
39
  protected
@@ -52,7 +53,7 @@ module Babelicious
52
53
  private
53
54
 
54
55
  def source_element(source, element)
55
- source[element.to_sym] || source[element.to_s]
56
+ source[element.to_sym] || source[element.to_s] || ''
56
57
  end
57
58
 
58
59
  def map_source_value(source_value)
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.6.3
4
+ version: 0.0.6.6
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-23 00:00:00 -07:00
12
+ date: 2009-07-25 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -49,6 +49,7 @@ files:
49
49
  - MIT-LICENSE
50
50
  has_rdoc: true
51
51
  homepage: http://github.com/cwyckoff/babel_icious
52
+ licenses:
52
53
  post_install_message:
53
54
  rdoc_options:
54
55
  - --line-numbers
@@ -74,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
75
  requirements: []
75
76
 
76
77
  rubyforge_project:
77
- rubygems_version: 1.2.0
78
+ rubygems_version: 1.3.5
78
79
  signing_key:
79
80
  specification_version: 2
80
81
  summary: Babel_icious dynamic and scalable mapping tool