Module: Neomirror::Relationship::ClassMethods

Defined in:
lib/neomirror/relationship.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#neo_primary_keyObject



42
43
44
# File 'lib/neomirror/relationship.rb', line 42

def neo_primary_key
  @neo_primary_key ||= self.respond_to?(:primary_key) ? self.__send__(:primary_key) : :id
end

Instance Method Details

#mirror_neo_relationship(options, &block) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/neomirror/relationship.rb', line 27

def mirror_neo_relationship(options, &block)
  m = Hash[options.map{ |k, v| [k.to_sym, v] }]
  raise ArgumentError, "Mirror with such options already defined" if rel_mirror(m)
  raise ArgumentError, "Options :start_node and :end_node are mandatory" unless m[:start_node] && m[:end_node]
  m[:start_node] = m[:start_node].to_sym
  m[:end_node] = m[:end_node].to_sym
  class_name = self.name.gsub(/^.*::/, '').gsub(/([a-z\d])([A-Z])/, '\1_\2')
  m[:type] = (m[:type] ? m[:type] : class_name.upcase).to_sym
  m[:properties] = Neomirror::PropertyCollector.new(&block).properties if block_given?
  m[:if] = m[:if].to_proc if m[:if]
  m[:index_name] = "#{m[:start_node] == :self ? class_name.downcase : m[:start_node]}_#{m[:type]}_#{m[:end_node] == :self ? class_name.downcase : m[:end_node]}"
  ::Neomirror.neo.create_relationship_index(m[:index_name])
  rel_mirrors << m
end

#rel_mirror(p) ⇒ Object

Find declaration by partial options.



21
22
23
24
25
# File 'lib/neomirror/relationship.rb', line 21

def rel_mirror(p)
  return rel_mirrors.first unless p
  rel_mirrors.find { |m| (!p[:start_node] || p[:start_node] == m[:start_node]) &&
    (!p[:end_node] || p[:end_node] == m[:end_node]) && (!p[:type] || p[:type] == m[:type]) }
end

#rel_mirrorsObject



10
11
12
13
14
15
16
17
18
# File 'lib/neomirror/relationship.rb', line 10

def rel_mirrors
  @rel_mirrors ||= begin
    if a = self.ancestors.drop(1).find { |c| c.respond_to?(:rel_mirrors) && c.rel_mirrors.any? }
      a.rel_mirrors
    else
      []
    end
  end
end