Permalink
Browse files

Add a Dex encoder

  • Loading branch information...
1 parent 729ce63 commit d067e894be9c7b8a642cac2d0f9c4092dcf093f4 @pangloss committed Sep 21, 2012
Showing with 80 additions and 2 deletions.
  1. +1 −0 lib/pacer-dex.rb
  2. +77 −0 lib/pacer-dex/encoder.rb
  3. +1 −1 lib/pacer-dex/graph.rb
  4. +1 −1 lib/pacer-dex/version.rb
View
@@ -9,6 +9,7 @@
require Pacer::Dex::JAR
require 'pacer-dex/graph'
+ require 'pacer-dex/encoder'
else
puts "Dex has been disabled."
puts
View
@@ -0,0 +1,77 @@
+require 'java'
+require 'yaml'
+
+module Pacer::Dex
+ # This encoder was originally part of pacer-neo4j. It uses native data where
+ # Neo4j could and for everything else it uses (slow (but easy))
+ # human-readable YAML encoding.
+ class Encoder
+ def self.encode_property(value)
+ case value
+ when nil
+ nil
+ when String
+ value = value.strip
+ value = nil if value == ''
+ value
+ when Fixnum
+ value.to_java :int
+ when Float
+ value.to_java :double
+ when Bignum
+ value.to_yaml
+ when true, false
+ value.to_java :boolean
+ when Array
+ if value.length == 0
+ value_type = Fixnum
+ else
+ value_type = value.first.class
+ value_type = TrueClass if value_type == FalseClass
+ value.each do |v|
+ if value_type != v.class or (value == true or value == false and value_type == TrueClass)
+ value_type = nil
+ break
+ end
+ end
+ end
+ case value_type
+ when Fixnum
+ value.to_java :int
+ when Float
+ value.to_java :double
+ when TrueClass
+ value.to_java :boolean
+ when String
+ value.to_java :string
+ else
+ value.to_yaml
+ end
+ else
+ value.to_yaml
+ end
+ end
+
+ if 'x'.to_yaml[0, 5] == '%YAML'
+ def self.decode_property(value)
+ if value.is_a? String and value[0, 5] == '%YAML'
+ YAML.load(value)
+ elsif value.is_a? ArrayJavaProxy
+ value.to_a
+ else
+ value
+ end
+ end
+ else
+ def self.decode_property(value)
+ if value.is_a? String and value[0, 3] == '---'
+ YAML.load(value)
+ elsif value.is_a? ArrayJavaProxy
+ value.to_a
+ else
+ value
+ end
+ end
+ end
+ end
+end
View
@@ -17,7 +17,7 @@ def dex(path)
g.blueprints_graph.shutdown
Pacer.open_graphs[path] = nil
end
- PacerGraph.new(Pacer::YamlEncoder, open, shutdown)
+ PacerGraph.new(Pacer::Dex::Encoder, open, shutdown)
end
end
end
View
@@ -4,7 +4,7 @@ module Dex
if not defined? ENABLED
ENABLED = ENV_JAVA['java.runtime.version'] =~ /^1\.6\./
end
- VERSION = "2.0.1"
+ VERSION = "2.0.2"
JAR = "pacer-dex-#{ VERSION }-standalone.jar"
JAR_PATH = "lib/#{ JAR }"
BLUEPRINTS_VERSION = "2.1.0"

0 comments on commit d067e89

Please sign in to comment.