cwyckoff-babel_icious 0.0.4.1 → 0.0.4.2

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,7 +47,20 @@ 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
- When you want to translate the mappings, simply call:
50
+ 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
+
52
+ m.map(:from => "foo/bar", :to => "bar/foo").unless(:empty)
53
+
54
+ if the value at "foo/bar" is empty or nil, "foo/bar" will not be translated. Additionally, if your qualification is more complicated than a simple empty? or nil?, use the "when" method, which takes a block. The mapping:
55
+
56
+ m.map(:from => "foo/bar", :to => "bar/foo").when do |value|
57
+ value =~ /^M/
58
+ end
59
+
60
+ will only translate if the value at "foo/bar" begins with a capital "M".
61
+
62
+
63
+ Finally, when you want to translate the mappings, simply call:
51
64
 
52
65
  Babelicious::Mapper.translate(:foo, source)
53
66
 
data/lib/babel_icious.rb CHANGED
@@ -7,6 +7,8 @@ require "babel_icious/core_ext/enumerable"
7
7
  require "babel_icious/target_mapper"
8
8
  require "babel_icious/map_factory"
9
9
  require "babel_icious/path_translator"
10
- require "babel_icious/xml_map"
11
- require "babel_icious/hash_map"
10
+ require "babel_icious/mappers/base_map"
11
+ require "babel_icious/mappers/xml_map"
12
+ require "babel_icious/mappers/hash_map"
12
13
  require "babel_icious/mapper"
14
+ require "babel_icious/map_condition"
@@ -0,0 +1,37 @@
1
+ module Babelicious
2
+
3
+ class MapCondition
4
+
5
+ def initialize
6
+ @condition, @block = nil, nil
7
+ end
8
+
9
+ def is_satisfied_by(source_value)
10
+ unless @block
11
+ case @condition_key
12
+ when :unless
13
+ process_unless_condition(source_value)
14
+ when :when
15
+ eval "source_value.#{@condition.to_s}?"
16
+ end
17
+ else
18
+ @block.call(source_value)
19
+ end
20
+ end
21
+
22
+ def register(condition_key, condition=nil, &block)
23
+ @condition_key, @condition, @block = condition_key, condition, block
24
+ end
25
+
26
+ private
27
+
28
+ def process_unless_condition(source_value)
29
+ if(@condition == :nil)
30
+ !source_value.nil?
31
+ else
32
+ eval "!source_value.nil? && !source_value.#{@condition.to_s}?"
33
+ end
34
+ end
35
+
36
+ end
37
+ end
@@ -8,7 +8,7 @@ module Babelicious
8
8
  eval("Babelicious::#{direction[:from].to_s.capitalize}Map").new(PathTranslator.new(opts[:from]), opts)
9
9
  end
10
10
 
11
- def target(direction, opts={})
11
+ def target(direction, opts={}, &block)
12
12
  eval("Babelicious::#{direction[:to].to_s.capitalize}Map").new(PathTranslator.new(opts[:to]), opts)
13
13
  end
14
14
 
@@ -20,6 +20,7 @@ module Babelicious
20
20
 
21
21
  def map(opts={})
22
22
  current_target_mapper.register_mapping(opts)
23
+ self
23
24
  end
24
25
 
25
26
  def mappings
@@ -36,6 +37,14 @@ module Babelicious
36
37
  mappings[key].translate(source)
37
38
  end
38
39
 
40
+ def when(&block)
41
+ current_target_mapper.register_condition(:when, nil, &block)
42
+ end
43
+
44
+ def unless(condition)
45
+ current_target_mapper.register_condition(:unless, condition)
46
+ end
47
+
39
48
  private
40
49
 
41
50
  def current_target_mapper
@@ -0,0 +1,29 @@
1
+ module Babelicious
2
+
3
+ class BaseMap
4
+
5
+ def map_from(output, source_value)
6
+ if map_condition?
7
+ map_output(output, source_value) if map_condition.is_satisfied_by(source_value)
8
+ else
9
+ map_output(output, source_value)
10
+ end
11
+ end
12
+
13
+ def register_condition(condition_key, condition, &block)
14
+ map_condition.register(condition_key, condition, &block)
15
+ end
16
+
17
+ protected
18
+
19
+ def map_condition
20
+ @map_condition ||= MapCondition.new
21
+ end
22
+
23
+ def map_condition?
24
+ @map_condition
25
+ end
26
+
27
+ end
28
+
29
+ end
@@ -1,7 +1,6 @@
1
1
  module Babelicious
2
2
 
3
- class HashMap
4
-
3
+ class HashMap < BaseMap
5
4
  class << self
6
5
 
7
6
  def initial_target
@@ -18,18 +17,6 @@ module Babelicious
18
17
  @path_translator, @opts = path_translator, opts
19
18
  end
20
19
 
21
- def map_from(hash_output, source_value)
22
- catch :no_value do
23
- @path_translator.inject_with_index(hash_output) do |hsh, element, index|
24
- if(hsh[element])
25
- hsh[element]
26
- else
27
- hsh[element] = (index == @path_translator.last_index ? source_value : {})
28
- end
29
- end
30
- end
31
- end
32
-
33
20
  def value_from(source)
34
21
  hash = {}
35
22
  @path_translator.inject_with_index(hash) do |hsh, element, index|
@@ -42,7 +29,19 @@ module Babelicious
42
29
  end
43
30
  end
44
31
 
45
- end
32
+ private
46
33
 
34
+ def map_output(hash_output, source_value)
35
+ catch :no_value do
36
+ @path_translator.inject_with_index(hash_output) do |hsh, element, index|
37
+ if(hsh[element])
38
+ hsh[element]
39
+ else
40
+ hsh[element] = (index == @path_translator.last_index ? source_value : {})
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ end
47
47
  end
48
-
@@ -2,7 +2,7 @@ require 'xml'
2
2
 
3
3
  module Babelicious
4
4
 
5
- class XmlMap
5
+ class XmlMap < BaseMap
6
6
 
7
7
  class << self
8
8
 
@@ -27,7 +27,9 @@ module Babelicious
27
27
  end
28
28
  end
29
29
 
30
- def map_from(xml_output, source_value)
30
+ private
31
+
32
+ def map_output(xml_output, source_value)
31
33
  @index = @path_translator.last_index
32
34
 
33
35
  set_root(xml_output)
@@ -38,8 +40,6 @@ module Babelicious
38
40
  end
39
41
  end
40
42
 
41
- private
42
-
43
43
  def populate_nodes(xml_output)
44
44
  return if @index == 0
45
45
 
@@ -115,7 +115,7 @@ module Babelicious
115
115
 
116
116
  end
117
117
  end
118
-
118
+
119
119
  class ChildNodeMapper
120
120
 
121
121
  class << self
@@ -28,6 +28,10 @@ module Babelicious
28
28
  target
29
29
  end
30
30
 
31
+ def register_condition(condition_key, condition=nil, &block)
32
+ @mappings.last[1].register_condition(condition_key, condition, &block)
33
+ end
34
+
31
35
  def register_mapping(opts={})
32
36
  raise TargetMapperError, "Both :from and :to keys must be set (e.g., {:from => \"foo/bar\", :to => \"bar/foo\")" unless (opts[:from] && opts[:to])
33
37
  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.1
4
+ version: 0.0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Wyckoff
@@ -34,9 +34,11 @@ extra_rdoc_files:
34
34
  files:
35
35
  - init.rb
36
36
  - lib/babel_icious.rb
37
- - lib/babel_icious/hash_map.rb
37
+ - lib/babel_icious/map_condition.rb
38
+ - lib/babel_icious/mappers/base_map.rb
39
+ - lib/babel_icious/mappers/xml_map.rb
40
+ - lib/babel_icious/mappers/hash_map.rb
38
41
  - lib/babel_icious/map_factory.rb
39
- - lib/babel_icious/xml_map.rb
40
42
  - lib/babel_icious/mapper.rb
41
43
  - lib/babel_icious/path_translator.rb
42
44
  - lib/babel_icious/target_mapper.rb