Class: NodeDsl

Inherits:
Object
  • Object
show all
Includes:
DslExceptionHandling
Defined in:
lib/geoff/node_dsl.rb

Instance Method Summary collapse

Methods included from DslExceptionHandling

#eval_with_exceptions, #lines_around, #write_mode

Constructor Details

#initialize(options, &block) ⇒ NodeDsl

Returns a new instance of NodeDsl.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/geoff/node_dsl.rb', line 7

def initialize(options, &block)
  @node_name  = options[:node_name]  || 'root'
  @klass_name = options[:klass_name] || 'ROOT'
  @container  = options[:container]  || Container.new
  @rel_type   = options[:rel_type]
  @target     = options[:target]
  @properties      = {}
  @children_dsls = []

  @rendered = false

  eval_with_exceptions(&block) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &blk) ⇒ Object

e.g. sandwich “BLT”, type: :on_menu do

filling 'bacon'
filling 'lettuce'
filling 'tomato'

end



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/geoff/node_dsl.rb', line 70

def method_missing m, *args, &blk

  if args.empty?
    @properties[m]
  else
    return super unless @write_mode
    val = if args.first.is_a? Proc
            @target.instance_exec &args.first
          else
            args.first
          end
    @properties[m] = val
  end
end

Instance Method Details

#all_rule_to_geoffObject



43
44
45
# File 'lib/geoff/node_dsl.rb', line 43

def all_rule_to_geoff
  "(#{@klass_name})-[:all]->(#{node_name})"
end

#bObject



39
40
41
# File 'lib/geoff/node_dsl.rb', line 39

def b
  @container
end

#cloneObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/geoff/node_dsl.rb', line 89

def clone
  c = self.class.new(
    node_name:  @node_name,
    klass_name: @klass_name,
    container:  @container,
    rel_type:   @rel_type,
    target:     @target
  )

  # sort it out, instance_variable_set is probably not the best way to clone object
  c.instance_variable_set('@properties', @properties)

  children_dsls = @children_dsls.map &:clone
  children_dsls.each {|children_dsl| children_dsl.parent_node_dsl = c}

  c.instance_variable_set('@children_dsls', children_dsls)
  c
end

#geoff_linesObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/geoff/node_dsl.rb', line 26

def geoff_lines
  #we need this to prevent rendering same node which is rendered already
  return [] if @rendered
  @rendered = true

  lines = [self_to_geoff]
  lines << all_rule_to_geoff if use_neo4j_wrapper?

  lines += @children_dsls.map(&:geoff_lines)

  lines
end

#node_nameObject



108
109
110
# File 'lib/geoff/node_dsl.rb', line 108

def node_name
  [@node_name, object_id].compact.join '_'
end

#propertiesObject



56
57
58
59
60
61
62
# File 'lib/geoff/node_dsl.rb', line 56

def properties
  if use_neo4j_wrapper?
    { '_classname' => @klass_name }.merge @properties
  else
    @properties
  end
end

#self_to_geoffObject



47
48
49
# File 'lib/geoff/node_dsl.rb', line 47

def self_to_geoff
  "(#{node_name}) #{properties.to_json}"
end

#to_geoffObject



21
22
23
# File 'lib/geoff/node_dsl.rb', line 21

def to_geoff
  geoff_lines.join "\n"
end

#to_sObject



85
86
87
# File 'lib/geoff/node_dsl.rb', line 85

def to_s
  to_geoff
end

#use_neo4j_wrapper?Boolean

refactor out wrapper specific stuff later

Returns:

  • (Boolean)


52
53
54
# File 'lib/geoff/node_dsl.rb', line 52

def use_neo4j_wrapper?
  true
end