Sha256: 484f2ec045bb15d742ddfa2086edffb675a3f65ebd06eb5b4bd3c19f1e1f78dc

Contents?: true

Size: 987 Bytes

Versions: 1

Compression:

Stored size: 987 Bytes

Contents

require 'geoff/children_dsl'

class Neo4jWrapperDsl
  def initialize *classes, &block
    options = classes.last.is_a?(Hash) ? classes.pop : {}

    validate classes

    options.merge!({
      parent_node_name: 'ROOT',
      type:       nil,
      container: Container.new
    })

    @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

  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 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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
geoff-0.0.3.beta lib/geoff/neo4j_wrapper_dsl.rb