Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added functionality to tools:

* translate specific map rule
* compare two separate mappings
  • Loading branch information...
commit 392b1c2ecd8b54d150227689fd51cd0e5b271a07 1 parent 2a3e9b0
@cwyckoff authored
Showing with 120 additions and 29 deletions.
  1. +54 −29 Rakefile
  2. +66 −0 lib/ptolemy/tools.rb
View
83 Rakefile
@@ -1,44 +1,69 @@
require 'rubygems'
-require 'ruby-debug'
-require 'babel_icious'
-require File.expand_path(File.dirname(__FILE__) + "/../market/lib/event_api/mappings/events.rb")
+require 'ptolemy'
-# task :default => [:]
+Ptolemy::Mapper.map_directory = File.dirname(__FILE__) + "/tmp"
namespace :mappings do
-
- desc "iterate through all babel_icious definitions and spit out mapping rules"
- task :all do
- Babelicious::Mapper.definitions.each do |key, definition|
- puts "Map definition: '#{key.to_s}'\n\n"
- len = definition.rules.map { |r| r.source_path.length }.max
- definition.rules.each do |rule|
- puts "\t%-#{len}s => %s" % [rule.source_path, rule.target_path]
- end
- puts "\n\n"
- end
- end
+ def format_mapping(length, source, target)
+ "\t%-#{length}s => %s" % [source, target]
+ end
+
+ desc "compare the source rules of a specific map definition with the target rules of another"
+ task :compare do
+ s_key = ARGV[1]
+ t_key = ARGV[2]
+ Ptolemy::Mapper.load_maps
+ Ptolemy::Tools.compare(s_key, t_key) do |length, source, target|
+ puts format_mapping(length, source, target)
+ end
+ end
+
desc "iterate through a specific map definition and spit out mapping rules"
- task :definition, :def_key do |t, def_key|
- key = def_key[:def_key]
- definition = Babelicious::Mapper.definitions[key.to_sym]
- puts "Map definition: '#{key.to_s}'\n\n"
- len = definition.rules.map { |r| r.source_path.length }.max
- definition.rules.each do |rule|
- puts "\t%-#{len}s => %s" % [rule.source_path, rule.target_path]
- end
+ task :definition do
+ key = ARGV[1].to_sym
+ puts "Map definition for: '#{key.to_s}'\n\n"
+ Ptolemy::Mapper.load_maps
+ Ptolemy::Tools.list_definition(key) do |length, source, target|
+ puts format_mapping(length, source, target)
+ end
puts "\n\n"
end
desc "list all map definition keys"
task :keys do
- puts "Babelicious map definition keys:\n\n"
- Babelicious::Mapper.definitions.each do |key, definition|
- puts "#{key}\n"
- end
+ Ptolemy::Mapper.load_maps
+ Ptolemy::Tools.keys
end
-end
+ desc "iterate through a specific map definition and spit out source rules"
+ task :sources do
+ key = ARGV[1].to_sym
+ puts "Map definition sources: '#{key.to_s}'\n\n"
+ Ptolemy::Mapper.load_maps
+ Ptolemy::Tools.list_sources(key) do |source|
+ puts source
+ end
+ end
+ desc "iterate through a specific map definition and spit out target rules"
+ task :targets do
+ key = ARGV[1].to_sym
+ puts "Map definition targets: '#{key.to_s}'\n\n"
+ Ptolemy::Mapper.load_maps
+ Ptolemy::Tools.list_targets(key) do |target|
+ puts target
+ end
+ end
+ desc "translate a single mapping rule for a specific definition"
+ task :translate_rule do
+ definition = ARGV[1].to_sym
+ source_path = ARGV[2].to_sym
+ source = {:code => "LMP"}
+ puts "Map translation of rule '#{rule}' for definition '#{definition}'\n\n"
+ Ptolemy::Mapper.load_maps
+ puts Ptolemy::Tools.translate(definition, source_path, source)
+ end
+
+end
View
66 lib/ptolemy/tools.rb
@@ -0,0 +1,66 @@
+module Ptolemy
+
+ class ToolsError < Exception; end
+
+ class Tools
+
+ class << self
+ attr_accessor :mapping_path
+
+ def compare(source_key, target_key)
+ puts "Comparing source map definition: '#{source_key.to_s}' with target map definition: '#{target_key.to_s}'\n\n"
+ source_paths = definition(source_key).rules.map { |r| r.source_path }
+ target_paths = definition(target_key).rules.map { |r| r.target_path }
+
+ max_length = source_paths.map { |path| path.length }.max
+ source_paths.each_with_index do |s_path, index|
+ yield max_length, s_path, target_paths[index]
+ end
+ end
+
+ def keys
+ Ptolemy::Mapper.definitions.each do |key, definition|
+ puts "#{key}\n"
+ end
+ end
+
+ def list_definition(key)
+ length = max_source_path_length(definition(key))
+ definition(key).rules.each do |rule|
+ yield length, rule.source_path, rule.target_path
+ end
+ end
+
+ def list_sources(key)
+ definition(key).rules.each do |rule|
+ yield rule.source_path
+ end
+ end
+
+ def list_targets(key)
+ definition(key).rules.each do |rule|
+ yield rule.target_path
+ end
+ end
+
+ def translate(definition, source_path, source)
+ rule = definition(definition).rules.detect {|rule| rule.source_path == source_path.to_s}
+ source_value = rule.source_value(rule.filtered_source(source))
+ target = rule.initial_target
+ rule.translate(target, source_value)
+ target
+ end
+
+ private
+
+ def definition(key)
+ @definition ||= Ptolemy::Mapper.definitions[key.to_sym]
+ end
+
+ def max_source_path_length(definition)
+ definition.rules.map { |r| r.source_path.length }.max
+ end
+
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.