require "json" require "geoff/version" require 'geoff/node_dsl' require 'geoff/importer' require 'geoff/children_dsl' 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 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 geoff = "#{add_classes}\n" geoff += @children_dsl.to_geoff if @children_dsl 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