require "json" require "geoff/version" require 'geoff/node_dsl' require 'geoff/children_dsl' require 'geoff-importer' def java_present? require 'java' true rescue LoadError false end if java_present? require 'geoff/indexer' else $stderr.puts "In order to use the Geoff::Importer class and Geoff.import you must be running under jruby" end class Geoff::Error < StandardError; end class Geoff::MissingNodeDefinition < Geoff::Error ; end class Geoff::ContainerLabelMissing < Geoff::Error ; end class Geoff::DslSyntaxError < Geoff::Error; end class Geoff class << self def import *args @options = args[1] Geoff.log @options db = options.delete(:db) || Neo4j.db.graph node_map = build_node_map db Importer.import args[0], db, node_map Indexer.rebuild_indices(Neo4j.db.graph) unless options[:skip_index] true end def build_node_map db return {} if db.is_a?(String) ref_node = db.reference_node ref_node.rels(:outgoing).inject({}) do |h, r| h[r.rel_type.to_s] = r.end_node h end.merge("ROOT" => ref_node) end def log(message) $stdout.puts message unless silent? end def silent? options.has_key?(:silent) ? options[:silent] : false end def options @options || {} end end attr_reader :children_dsl, :container def initialize *classes_and_builders, &block options = classes_and_builders.last.is_a?(Hash) ? classes_and_builders.pop : {} grouped = classes_and_builders.group_by &:class classes = grouped[Class] || {} builders = grouped[Geoff] || {} validate grouped @container = Container.new builders.each do |builder| @container.merge builder.container end options.merge!({ parent_node_dsl: nil, type: nil, container: @container }) @children_dsl = ChildrenDsl.new(options, &block) if block_given? end def to_geoff return @geoff if @geoff geoff = "#{add_classes}\n" geoff += @children_dsl.to_geoff if @children_dsl @geoff = geoff.chomp end def <=> other to_geoff == other.to_geoff ? 0 : -1 end def to_s to_geoff end def inspect "" end private def add_classes @classes.map{|c| root_to_class_as_geoff c }.join("\n") end def root_to_class_as_geoff c "(ROOT)-[:#{c}]->(#{c})" end def validate grouped_classes_and_builders validate_classes grouped_classes_and_builders[Class] unknown_argument_types = grouped_classes_and_builders.keys - [Class, Geoff] if unknown_argument_types.any? formatted = unknown_argument_types.map(&:to_s).join(', ') raise ArgumentError, "Unknown argument types #{formatted}" end end def validate_classes classes @classes = classes || [] @classes.each do |c| m = "Class #{c} should include Neo4j::NodeMixin" raise ArgumentError, m unless c.is_a?(Class) and c.included_modules.include? Neo4j::NodeMixin end end end def Geoff *class_list, options, &block Geoff.new(*class_list, options, &block) end