Class: Neo4j::ActiveNode::HasN::Association

Inherits:
Object
  • Object
show all
Includes:
Dependent::AssociationMethods, AssociationCypherMethods, Shared::RelTypeConverters
Defined in:
lib/neo4j/active_node/has_n/association.rb,
lib/neo4j/active_node/has_n/association/rel_factory.rb,
lib/neo4j/active_node/has_n/association/rel_wrapper.rb

Defined Under Namespace

Classes: RelFactory, RelWrapper

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AssociationCypherMethods

#arrow_cypher

Methods included from Dependent::AssociationMethods

#add_destroy_callbacks, #validate_dependent

Methods included from Shared::RelTypeConverters

#decorated_rel_type, decorated_rel_type, rel_transformer

Constructor Details

#initialize(type, direction, name, options = {type: nil}) ⇒ Association

Returns a new instance of Association.



14
15
16
17
18
19
20
21
# File 'lib/neo4j/active_node/has_n/association.rb', line 14

def initialize(type, direction, name, options = {type: nil})
  validate_init_arguments(type, direction, name, options)
  @type = type.to_sym
  @name = name
  @direction = direction.to_sym
  @target_class_name_from_name = name.to_s.classify
  apply_vars_from_options(options)
end

Instance Attribute Details

#dependentObject (readonly)

Returns the value of attribute dependent.



12
13
14
# File 'lib/neo4j/active_node/has_n/association.rb', line 12

def dependent
  @dependent
end

#directionObject (readonly)

Returns the value of attribute direction.



12
13
14
# File 'lib/neo4j/active_node/has_n/association.rb', line 12

def direction
  @direction
end

#model_classObject (readonly)

Returns the value of attribute model_class.



12
13
14
# File 'lib/neo4j/active_node/has_n/association.rb', line 12

def model_class
  @model_class
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/neo4j/active_node/has_n/association.rb', line 12

def name
  @name
end

#relationshipObject (readonly)

Returns the value of attribute relationship.



12
13
14
# File 'lib/neo4j/active_node/has_n/association.rb', line 12

def relationship
  @relationship
end

#relationship_class_nameObject (readonly)

Returns the value of attribute relationship_class_name.



121
122
123
# File 'lib/neo4j/active_node/has_n/association.rb', line 121

def relationship_class_name
  @relationship_class_name
end

#typeObject (readonly)

Returns the value of attribute type.



12
13
14
# File 'lib/neo4j/active_node/has_n/association.rb', line 12

def type
  @type
end

Instance Method Details

#_create_relationship(start_object, node_or_nodes, properties) ⇒ Object



144
145
146
# File 'lib/neo4j/active_node/has_n/association.rb', line 144

def _create_relationship(start_object, node_or_nodes, properties)
  RelFactory.create(start_object, node_or_nodes, properties, self)
end

#create_methodObject



140
141
142
# File 'lib/neo4j/active_node/has_n/association.rb', line 140

def create_method
  unique? ? :create_unique : :create
end

#creates_unique_optionObject



136
137
138
# File 'lib/neo4j/active_node/has_n/association.rb', line 136

def creates_unique_option
  @unique || :none
end

#derive_model_classObject



23
24
25
26
27
28
29
30
# File 'lib/neo4j/active_node/has_n/association.rb', line 23

def derive_model_class
  refresh_model_class! if pending_model_refresh?
  return @model_class unless @model_class.nil?
  return nil if relationship_class.nil?
  dir_class = direction == :in ? :from_class : :to_class
  return false if relationship_class.send(dir_class).to_s.to_sym == :any
  relationship_class.send(dir_class)
end

#discovered_modelObject



90
91
92
93
94
# File 'lib/neo4j/active_node/has_n/association.rb', line 90

def discovered_model
  target_classes.select do |constant|
    constant.ancestors.include?(::Neo4j::ActiveNode)
  end
end

#inverse_of?(other) ⇒ Boolean

Returns:



70
71
72
# File 'lib/neo4j/active_node/has_n/association.rb', line 70

def inverse_of?(other)
  origin_association == other
end

#pending_model_refresh?Boolean

Returns:



56
57
58
# File 'lib/neo4j/active_node/has_n/association.rb', line 56

def pending_model_refresh?
  !!@pending_model_refresh
end

#queue_model_refresh!Object



39
40
41
# File 'lib/neo4j/active_node/has_n/association.rb', line 39

def queue_model_refresh!
  @pending_model_refresh = true
end

#refresh_model_class!Object



32
33
34
35
36
37
# File 'lib/neo4j/active_node/has_n/association.rb', line 32

def refresh_model_class!
  @pending_model_refresh = @target_classes_or_nil = nil

  # Using #to_s on purpose here to take care of classes/strings/symbols
  @model_class = ClassArguments.constantize_argument(@model_class.to_s) if @model_class
end

#relationship_classObject



127
128
129
# File 'lib/neo4j/active_node/has_n/association.rb', line 127

def relationship_class
  @relationship_class ||= @relationship_class_name && @relationship_class_name.constantize
end

#relationship_class?Boolean Also known as: rel_class?

Returns:



148
149
150
# File 'lib/neo4j/active_node/has_n/association.rb', line 148

def relationship_class?
  !!relationship_class
end

#relationship_class_typeObject



123
124
125
# File 'lib/neo4j/active_node/has_n/association.rb', line 123

def relationship_class_type
  relationship_class._type.to_sym
end

#relationship_type(create = false) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/neo4j/active_node/has_n/association.rb', line 106

def relationship_type(create = false)
  case
  when relationship_class
    relationship_class_type
  when !@relationship_type.nil?
    @relationship_type
  when @origin
    origin_type
  else
    # I think that this line is no longer readed since we require either
    # `type`, `rel_class`, or `origin` in associations
    (create || exceptional_target_class?) && decorated_rel_type(@name)
  end
end

#target_classObject



96
97
98
99
100
101
102
103
104
# File 'lib/neo4j/active_node/has_n/association.rb', line 96

def target_class
  return @target_class if @target_class

  return if !(target_class_names && target_class_names.size == 1)

  class_const = ClassArguments.constantize_argument(target_class_names[0])

  @target_class = class_const
end

#target_class_namesObject



60
61
62
63
64
65
66
67
68
# File 'lib/neo4j/active_node/has_n/association.rb', line 60

def target_class_names
  option = target_class_option(derive_model_class)

  @target_class_names ||= if option.is_a?(Array)
                            option.map(&:to_s)
                          elsif option
                            [option.to_s]
                          end
end

#target_class_option(model_class) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/neo4j/active_node/has_n/association.rb', line 43

def target_class_option(model_class)
  case model_class
  when nil
    @target_class_name_from_name ? "#{association_model_namespace}::#{@target_class_name_from_name}" : @target_class_name_from_name
  when Array
    model_class.map { |sub_model_class| target_class_option(sub_model_class) }
  when false
    false
  else
    model_class.to_s[0, 2] == '::' ? model_class.to_s : "::#{model_class}"
  end
end

#target_classesObject



74
75
76
# File 'lib/neo4j/active_node/has_n/association.rb', line 74

def target_classes
  ClassArguments.constantize_argument(target_class_names)
end

#target_classes_or_nilObject



78
79
80
# File 'lib/neo4j/active_node/has_n/association.rb', line 78

def target_classes_or_nil
  @target_classes_or_nil ||= discovered_model if target_class_names
end

#target_where_clause(var = name) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/neo4j/active_node/has_n/association.rb', line 82

def target_where_clause(var = name)
  return if model_class == false

  Array.new(target_classes).map do |target_class|
    "#{var}:`#{target_class.mapped_label_name}`"
  end.join(' OR ')
end

#unique?Boolean

Returns:



131
132
133
134
# File 'lib/neo4j/active_node/has_n/association.rb', line 131

def unique?
  return relationship_class.unique? if rel_class?
  @origin ? origin_association.unique? : !!@unique
end