Class: ChildrenDsl

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DslExceptionHandling

#eval_with_exceptions, #lines_around, #write_mode

Constructor Details

#initialize(options, &block) ⇒ ChildrenDsl

Returns a new instance of ChildrenDsl.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/geoff/children_dsl.rb', line 13

def initialize options, &block
  @parent_node_dsl  = options[:parent_node_dsl]
  @parent_rel_type  = options[:type]
  @target           = options[:target]
  @outer_geoff      = options[:geoff]
  @direction        = options[:direction]
  @write_mode = false

  @node_dsls = {}
  @container = options[:container] || Container.new

  eval_with_exceptions(&block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

e.g. company “ACME” do

children "works_at" do
  employee 'Bob'
  employee 'Lenny'
  employee 'Tom'
end

end



117
118
119
120
121
122
123
124
125
# File 'lib/geoff/children_dsl.rb', line 117

def method_missing m, *args, &blk
  return super unless @write_mode
  relationship, options = parse_method_missing m, *args

  options.merge!(target: @target)
  NodeDsl.new(options, &blk).tap do |n|
    @node_dsls[n] = relationship
  end
end

Instance Attribute Details

#node_dslsObject (readonly)

Returns the value of attribute node_dsls.



10
11
12
# File 'lib/geoff/children_dsl.rb', line 10

def node_dsls
  @node_dsls
end

#parent_node_dsl=(value) ⇒ Object (writeonly)

Sets the attribute parent_node_dsl

Parameters:

  • value

    the value to set the attribute parent_node_dsl to.



11
12
13
# File 'lib/geoff/children_dsl.rb', line 11

def parent_node_dsl=(value)
  @parent_node_dsl = value
end

Instance Method Details

#add_node_dsl(node_dsl, rel_properties) ⇒ Object



76
77
78
79
80
# File 'lib/geoff/children_dsl.rb', line 76

def add_node_dsl node_dsl, rel_properties
  rel_properties ||= {}
  rel_properties[:type] ||= @parent_rel_type
  @node_dsls[node_dsl] = rel_properties
end

#bObject



71
72
73
74
# File 'lib/geoff/children_dsl.rb', line 71

def b
  @container.set_recipient_of_node_dsl self
  @container
end

#children(&block) ⇒ Object

Node dsls of children dsl defined inside other children dsl are merged into outer children dsl.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/geoff/children_dsl.rb', line 29

def children &block
  options = {
    parent_node_dsl: @parent_node_dsl,
    parent_rel_type: @type,
    target:          @target,
    outer_geoff:     @geoff,
    container:       @container
  }

  children_dsl = ChildrenDsl.new(options, &block) if block_given?
  @node_dsls.merge children_dsl.node_dsls

  children_dsl
end

#cloneObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/geoff/children_dsl.rb', line 82

def clone
  c = self.class.new(
    parent_node_dsl: @parent_node_dsl,
    parent_rel_type: @type,
    target:          @target,
    outer_geoff:     @geoff,
    container:       @container
  ) {}

  node_dsls = {}
  @node_dsls.each do |node_dsl, rel_properties|
    if rel_properties[:clone] == false
      node_dsls[node_dsl] = rel_properties
    else
      node_dsls[node_dsl.clone] = rel_properties.dup
    end
  end

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

  c
end

#geoff_linesObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/geoff/children_dsl.rb', line 48

def geoff_lines
  lines = []

  @node_dsls.each do |node_dsl, properties|
    lines += node_dsl.geoff_lines
    lines << rel_geoff(node_dsl, properties) unless top_level_node?
  end

  lines
end

#rel_geoff(node_dsl, properties) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/geoff/children_dsl.rb', line 59

def rel_geoff node_dsl, properties
  directions_ascii = ["<-", "-", "->"]
  direction_array = @direction == :incoming ? directions_ascii.first(2) : directions_ascii.last(2)
  properties = properties.dup
  type       = properties.delete :type
  properties.delete(:clone)

  "(#{parent_node_name})#{direction_array.first}[:#{type}]#{direction_array.last}(#{node_dsl.node_name})".tap do |r|
    r << " #{properties.to_json}" if properties.any?
  end
end

#to_geoffObject



44
45
46
# File 'lib/geoff/children_dsl.rb', line 44

def to_geoff
  "#{@outer_geoff}\n#{geoff_lines.join("\n")}".strip
end