Class: ActiveGraph::Node::HasN::Association

Inherits:
Object
  • Object
show all
Includes:
Dependent::AssociationMethods, AssociationCypherMethods, Shared::RelTypeConverters
Defined in:
lib/active_graph/node/has_n/association.rb,
lib/active_graph/node/has_n/association/rel_factory.rb,
lib/active_graph/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.



11
12
13
14
15
16
17
18
# File 'lib/active_graph/node/has_n/association.rb', line 11

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.



9
10
11
# File 'lib/active_graph/node/has_n/association.rb', line 9

def dependent
  @dependent
end

#directionObject (readonly)

Returns the value of attribute direction.



9
10
11
# File 'lib/active_graph/node/has_n/association.rb', line 9

def direction
  @direction
end

#model_classObject (readonly)

Returns the value of attribute model_class.



9
10
11
# File 'lib/active_graph/node/has_n/association.rb', line 9

def model_class
  @model_class
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/active_graph/node/has_n/association.rb', line 9

def name
  @name
end

#relationshipObject (readonly)

Returns the value of attribute relationship.



9
10
11
# File 'lib/active_graph/node/has_n/association.rb', line 9

def relationship
  @relationship
end

#relationship_class_nameObject (readonly)

Returns the value of attribute relationship_class_name.



118
119
120
# File 'lib/active_graph/node/has_n/association.rb', line 118

def relationship_class_name
  @relationship_class_name
end

#typeObject (readonly)

Returns the value of attribute type.



9
10
11
# File 'lib/active_graph/node/has_n/association.rb', line 9

def type
  @type
end

Instance Method Details

#_create_relationship(start_object, node_or_nodes, properties) ⇒ Object



141
142
143
# File 'lib/active_graph/node/has_n/association.rb', line 141

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

#create_methodObject



137
138
139
# File 'lib/active_graph/node/has_n/association.rb', line 137

def create_method
  unique? ? :create_unique : :create
end

#creates_unique_optionObject



133
134
135
# File 'lib/active_graph/node/has_n/association.rb', line 133

def creates_unique_option
  @unique || :none
end

#derive_model_classObject



20
21
22
23
24
25
26
27
# File 'lib/active_graph/node/has_n/association.rb', line 20

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



87
88
89
90
91
# File 'lib/active_graph/node/has_n/association.rb', line 87

def discovered_model
  target_classes.select do |constant|
    constant.ancestors.include?(::ActiveGraph::Node)
  end
end

#inverse_of?(other) ⇒ Boolean

Returns:



67
68
69
# File 'lib/active_graph/node/has_n/association.rb', line 67

def inverse_of?(other)
  origin_association == other
end

#pending_model_refresh?Boolean

Returns:



53
54
55
# File 'lib/active_graph/node/has_n/association.rb', line 53

def pending_model_refresh?
  !!@pending_model_refresh
end

#queue_model_refresh!Object



36
37
38
# File 'lib/active_graph/node/has_n/association.rb', line 36

def queue_model_refresh!
  @pending_model_refresh = true
end

#refresh_model_class!Object



29
30
31
32
33
34
# File 'lib/active_graph/node/has_n/association.rb', line 29

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



124
125
126
# File 'lib/active_graph/node/has_n/association.rb', line 124

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

#relationship_class?Boolean Also known as: rel_class?

Returns:



145
146
147
# File 'lib/active_graph/node/has_n/association.rb', line 145

def relationship_class?
  !!relationship_class
end

#relationship_class_typeObject



120
121
122
# File 'lib/active_graph/node/has_n/association.rb', line 120

def relationship_class_type
  relationship_class.type.to_sym
end

#relationship_type(create = false) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/active_graph/node/has_n/association.rb', line 103

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



93
94
95
96
97
98
99
100
101
# File 'lib/active_graph/node/has_n/association.rb', line 93

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



57
58
59
60
61
62
63
64
65
# File 'lib/active_graph/node/has_n/association.rb', line 57

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



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/active_graph/node/has_n/association.rb', line 40

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



71
72
73
# File 'lib/active_graph/node/has_n/association.rb', line 71

def target_classes
  ClassArguments.constantize_argument(target_class_names)
end

#target_classes_or_nilObject



75
76
77
# File 'lib/active_graph/node/has_n/association.rb', line 75

def target_classes_or_nil
  @target_classes_or_nil ||= discovered_model if target_class_names
end

#target_where_clause(var = name) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/active_graph/node/has_n/association.rb', line 79

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:



128
129
130
131
# File 'lib/active_graph/node/has_n/association.rb', line 128

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