cwyckoff-babel_icious 0.0.6.0 → 0.0.6.2
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,8 @@
|
|
1
|
-
class
|
1
|
+
class MapDefinitionError < Exception; end
|
2
2
|
|
3
3
|
module Babelicious
|
4
4
|
|
5
|
-
class
|
5
|
+
class MapDefinition
|
6
6
|
attr_reader :mappings, :direction
|
7
7
|
|
8
8
|
def initialize
|
@@ -10,8 +10,8 @@ module Babelicious
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def direction=(dir)
|
13
|
-
raise
|
14
|
-
raise
|
13
|
+
raise MapDefinitionError, "Direction must be a hash" unless dir.is_a?(Hash)
|
14
|
+
raise MapDefinitionError, "Both :from and :to keys must be set (e.g., {:from => :xml, :to => :hash}" unless (dir[:from] && dir[:to])
|
15
15
|
|
16
16
|
@direction = dir
|
17
17
|
end
|
@@ -37,22 +37,22 @@ module Babelicious
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def register_from(from_str)
|
40
|
-
raise
|
40
|
+
raise MapDefinitionError, "Please specify a source mapping" if from_str.nil?
|
41
41
|
source = MapFactory.source(@direction, {:from => from_str})
|
42
42
|
|
43
43
|
@mappings << MapRule.new(source)
|
44
44
|
end
|
45
45
|
|
46
46
|
def register_to(&block)
|
47
|
-
raise
|
47
|
+
raise MapDefinitionError, "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
50
|
@mappings.last.target = target
|
51
51
|
end
|
52
52
|
|
53
53
|
def register_include(map_definition=nil, opts={})
|
54
|
-
raise
|
55
|
-
raise
|
54
|
+
raise MapDefinitionError, "A mapping definition key is required (e.g., m.include(:another_map))" if map_definition.nil?
|
55
|
+
raise MapDefinitionError, "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
58
|
source = m.source.dup
|
@@ -68,7 +68,7 @@ module Babelicious
|
|
68
68
|
end
|
69
69
|
|
70
70
|
def register_mapping(opts={})
|
71
|
-
raise
|
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
73
|
@mappings << MapRule.new(MapFactory.source(@direction, opts), MapFactory.target(@direction, opts))
|
74
74
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Babelicious
|
2
|
+
|
3
|
+
class MapRule
|
4
|
+
attr_accessor :source, :target
|
5
|
+
|
6
|
+
def initialize(source=nil, target=nil)
|
7
|
+
@source, @target = source, target
|
8
|
+
end
|
9
|
+
|
10
|
+
def filtered_source(source)
|
11
|
+
@source.class.filter_source(source)
|
12
|
+
end
|
13
|
+
|
14
|
+
def initial_target
|
15
|
+
@target.class.initial_target
|
16
|
+
end
|
17
|
+
|
18
|
+
def translate(target_data, source_value)
|
19
|
+
if(@target.opts[:to_proc])
|
20
|
+
@target.path_translator.set_path(@target.opts[:to_proc].call(source_value))
|
21
|
+
@target.map_from(target_data, source_value)
|
22
|
+
else
|
23
|
+
@target.map_from(target_data, source_value)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
data/lib/babel_icious/mapper.rb
CHANGED
@@ -5,7 +5,7 @@ module Babelicious
|
|
5
5
|
class Mapper
|
6
6
|
|
7
7
|
class << self
|
8
|
-
attr_reader :direction, :
|
8
|
+
attr_reader :direction, :current_map_definition_key
|
9
9
|
|
10
10
|
def [](key)
|
11
11
|
mappings[key.to_sym]
|
@@ -14,30 +14,30 @@ module Babelicious
|
|
14
14
|
def config(key)
|
15
15
|
raise MapperError, "A mapping for the key #{key} currently exists. Are you sure you want to merge the mapping you are about to do with the existing mapping?" if mapping_already_exists?(key)
|
16
16
|
|
17
|
-
@
|
17
|
+
@current_map_definition_key = key
|
18
18
|
yield self
|
19
19
|
end
|
20
20
|
|
21
21
|
def customize(&block)
|
22
|
-
|
22
|
+
current_map_definition.register_customized(&block)
|
23
23
|
end
|
24
24
|
|
25
25
|
def direction(dir={})
|
26
|
-
|
26
|
+
current_map_definition.direction = @direction = dir
|
27
27
|
end
|
28
28
|
|
29
29
|
def from(from_str=nil)
|
30
|
-
|
30
|
+
current_map_definition.register_from(from_str)
|
31
31
|
self
|
32
32
|
end
|
33
33
|
|
34
34
|
def include(key, opts={})
|
35
|
-
|
35
|
+
current_map_definition.register_include(key, opts)
|
36
36
|
self
|
37
37
|
end
|
38
38
|
|
39
39
|
def map(opts={})
|
40
|
-
|
40
|
+
current_map_definition.register_mapping(opts)
|
41
41
|
self
|
42
42
|
end
|
43
43
|
|
@@ -50,7 +50,7 @@ module Babelicious
|
|
50
50
|
end
|
51
51
|
|
52
52
|
def to(&block)
|
53
|
-
|
53
|
+
current_map_definition.register_to(&block)
|
54
54
|
self
|
55
55
|
end
|
56
56
|
|
@@ -61,18 +61,18 @@ module Babelicious
|
|
61
61
|
end
|
62
62
|
|
63
63
|
def when(&block)
|
64
|
-
|
64
|
+
current_map_definition.register_condition(:when, nil, &block)
|
65
65
|
end
|
66
66
|
|
67
67
|
def unless(condition=nil, &block)
|
68
|
-
|
68
|
+
current_map_definition.register_condition(:unless, condition, &block)
|
69
69
|
self
|
70
70
|
end
|
71
71
|
|
72
72
|
private
|
73
73
|
|
74
|
-
def
|
75
|
-
mappings[@
|
74
|
+
def current_map_definition
|
75
|
+
mappings[@current_map_definition_key] ||= MapDefinition.new
|
76
76
|
end
|
77
77
|
|
78
78
|
def mapping_already_exists?(key)
|
data/lib/babel_icious.rb
CHANGED
@@ -6,7 +6,7 @@ $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) unless $LOAD_PATH.i
|
|
6
6
|
require "babel_icious/core_ext/enumerable"
|
7
7
|
require "babel_icious/core_ext/libxml_node"
|
8
8
|
require "babel_icious/map_rule"
|
9
|
-
require "babel_icious/
|
9
|
+
require "babel_icious/map_definition"
|
10
10
|
require "babel_icious/map_factory"
|
11
11
|
require "babel_icious/path_translator"
|
12
12
|
require "babel_icious/mappers/base_map"
|
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.
|
4
|
+
version: 0.0.6.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Wyckoff
|
@@ -41,7 +41,8 @@ files:
|
|
41
41
|
- lib/babel_icious/map_factory.rb
|
42
42
|
- lib/babel_icious/mapper.rb
|
43
43
|
- lib/babel_icious/path_translator.rb
|
44
|
-
- lib/babel_icious/
|
44
|
+
- lib/babel_icious/map_rule.rb
|
45
|
+
- lib/babel_icious/map_definition.rb
|
45
46
|
- lib/babel_icious/core_ext/libxml_node.rb
|
46
47
|
- lib/babel_icious/core_ext/enumerable.rb
|
47
48
|
- README.rdoc
|