class ActiveGraph::Node::HasN::Association

Constants

VALID_ASSOCIATION_OPTION_KEYS

the “:labels” option is not used by the association per-say. Instead, if provided,it is used by the association getter as a default getter options argument

Attributes

dependent[R]
direction[R]
model_class[R]
name[R]
relationship[R]
relationship_class_name[R]
type[R]

Public Class Methods

new(type, direction, name, options = {type: nil}) click to toggle source
   # File lib/active_graph/node/has_n/association.rb
14 def initialize(type, direction, name, options = {type: nil})
15   validate_init_arguments(type, direction, name, options)
16   @type = type.to_sym
17   @name = name
18   @direction = direction.to_sym
19   @target_class_name_from_name = name.to_s.classify
20   apply_vars_from_options(options)
21 end

Public Instance Methods

_create_relationship(start_object, node_or_nodes, properties) click to toggle source
    # File lib/active_graph/node/has_n/association.rb
144 def _create_relationship(start_object, node_or_nodes, properties)
145   RelFactory.create(start_object, node_or_nodes, properties, self)
146 end
create_method() click to toggle source
    # File lib/active_graph/node/has_n/association.rb
140 def create_method
141   unique? ? :create_unique : :create
142 end
creates_unique_option() click to toggle source
    # File lib/active_graph/node/has_n/association.rb
136 def creates_unique_option
137   @unique || :none
138 end
derive_model_class() click to toggle source
   # File lib/active_graph/node/has_n/association.rb
23 def derive_model_class
24   refresh_model_class! if pending_model_refresh?
25   return @model_class unless @model_class.nil?
26   return nil if relationship_class.nil?
27   dir_class = direction == :in ? :from_class : :to_class
28   return false if relationship_class.send(dir_class).to_s.to_sym == :any
29   relationship_class.send(dir_class)
30 end
discovered_model() click to toggle source
   # File lib/active_graph/node/has_n/association.rb
90 def discovered_model
91   target_classes.select do |constant|
92     constant.ancestors.include?(::ActiveGraph::Node)
93   end
94 end
inverse_of?(other) click to toggle source
   # File lib/active_graph/node/has_n/association.rb
70 def inverse_of?(other)
71   origin_association == other
72 end
pending_model_refresh?() click to toggle source
   # File lib/active_graph/node/has_n/association.rb
56 def pending_model_refresh?
57   !!@pending_model_refresh
58 end
queue_model_refresh!() click to toggle source
   # File lib/active_graph/node/has_n/association.rb
39 def queue_model_refresh!
40   @pending_model_refresh = true
41 end
refresh_model_class!() click to toggle source
   # File lib/active_graph/node/has_n/association.rb
32 def refresh_model_class!
33   @pending_model_refresh = @target_classes_or_nil = nil
34 
35   # Using #to_s on purpose here to take care of classes/strings/symbols
36   @model_class = ClassArguments.constantize_argument(@model_class.to_s) if @model_class
37 end
rel_class?()
Alias for: relationship_class?
relationship_class() click to toggle source
    # File lib/active_graph/node/has_n/association.rb
127 def relationship_class
128   @relationship_class ||= @relationship_class_name && @relationship_class_name.constantize
129 end
relationship_class?() click to toggle source
    # File lib/active_graph/node/has_n/association.rb
148 def relationship_class?
149   !!relationship_class
150 end
Also aliased as: rel_class?
relationship_class_type() click to toggle source
    # File lib/active_graph/node/has_n/association.rb
123 def relationship_class_type
124   relationship_class.type.to_sym
125 end
relationship_type(create = false) click to toggle source
    # File lib/active_graph/node/has_n/association.rb
106 def relationship_type(create = false)
107   case
108   when relationship_class
109     relationship_class_type
110   when !@relationship_type.nil?
111     @relationship_type
112   when @origin
113     origin_type
114   else
115     # I think that this line is no longer readed since we require either
116     # `type`, `rel_class`, or `origin` in associations
117     (create || exceptional_target_class?) && decorated_rel_type(@name)
118   end
119 end
target_class() click to toggle source
    # File lib/active_graph/node/has_n/association.rb
 96 def target_class
 97   return @target_class if @target_class
 98 
 99   return if !(target_class_names && target_class_names.size == 1)
100 
101   class_const = ClassArguments.constantize_argument(target_class_names[0])
102 
103   @target_class = class_const
104 end
target_class_names() click to toggle source
   # File lib/active_graph/node/has_n/association.rb
60 def target_class_names
61   option = target_class_option(derive_model_class)
62 
63   @target_class_names ||= if option.is_a?(Array)
64                             option.map(&:to_s)
65                           elsif option
66                             [option.to_s]
67                           end
68 end
target_class_option(model_class) click to toggle source
   # File lib/active_graph/node/has_n/association.rb
43 def target_class_option(model_class)
44   case model_class
45   when nil
46     @target_class_name_from_name ? "#{association_model_namespace}::#{@target_class_name_from_name}" : @target_class_name_from_name
47   when Array
48     model_class.map { |sub_model_class| target_class_option(sub_model_class) }
49   when false
50     false
51   else
52     model_class.to_s[0, 2] == '::' ? model_class.to_s : "::#{model_class}"
53   end
54 end
target_classes() click to toggle source
   # File lib/active_graph/node/has_n/association.rb
74 def target_classes
75   ClassArguments.constantize_argument(target_class_names)
76 end
target_classes_or_nil() click to toggle source
   # File lib/active_graph/node/has_n/association.rb
78 def target_classes_or_nil
79   @target_classes_or_nil ||= discovered_model if target_class_names
80 end
target_where_clause(var = name) click to toggle source
   # File lib/active_graph/node/has_n/association.rb
82 def target_where_clause(var = name)
83   return if model_class == false
84 
85   Array.new(target_classes).map do |target_class|
86     "#{var}:`#{target_class.mapped_label_name}`"
87   end.join(' OR ')
88 end
unique?() click to toggle source
    # File lib/active_graph/node/has_n/association.rb
131 def unique?
132   return relationship_class.unique? if rel_class?
133   @origin ? origin_association.unique? : !!@unique
134 end

Private Instance Methods

apply_vars_from_options(options) click to toggle source
    # File lib/active_graph/node/has_n/association.rb
182 def apply_vars_from_options(options)
183   @relationship_class_name = options[:rel_class] && options[:rel_class].to_s
184   @relationship_type = options[:type] && options[:type].to_sym
185 
186   @model_class = options[:model_class]
187   @origin = options[:origin] && options[:origin].to_sym
188   @dependent = options[:dependent].try(:to_sym)
189   @unique = options[:unique]
190 end
association_model_namespace() click to toggle source
    # File lib/active_graph/node/has_n/association.rb
155 def association_model_namespace
156   ActiveGraph::Config.association_model_namespace_string
157 end
base_declaration() click to toggle source

Return basic details about association as declared in the model @example

has_many :in, :bands, type: :has_band
    # File lib/active_graph/node/has_n/association.rb
195 def base_declaration
196   "#{type} #{direction.inspect}, #{name.inspect}"
197 end
check_valid_type_and_dir(type, direction) click to toggle source
    # File lib/active_graph/node/has_n/association.rb
233 def check_valid_type_and_dir(type, direction)
234   fail ArgumentError, "Invalid association type: #{type.inspect} (valid value: :has_many and :has_one)" if ![:has_many, :has_one].include?(type.to_sym)
235   fail ArgumentError, "Invalid direction: #{direction.inspect} (valid value: :out, :in, and :both)" if ![:out, :in, :both].include?(direction.to_sym)
236 end
exceptional_target_class?() click to toggle source

Determine if model class as derived from the association name would be different than the one specified via the model_class key @example

has_many :friends                 # Would return false
has_many :friends, model_class: Friend  # Would return false
has_many :friends, model_class: Person  # Would return true
    # File lib/active_graph/node/has_n/association.rb
253 def exceptional_target_class?
254   # TODO: Exceptional if target_class.nil?? (when model_class false)
255 
256   target_class && target_class.name != @target_class_name_from_name
257 end
get_direction(create, reverse = false) click to toggle source
    # File lib/active_graph/node/has_n/association.rb
159 def get_direction(create, reverse = false)
160   dir = (create && @direction == :both) ? :out : @direction
161   if reverse
162     case dir
163     when :in then :out
164     when :out then :in
165     else :both
166     end
167   else
168     dir
169   end
170 end
origin_association() click to toggle source
    # File lib/active_graph/node/has_n/association.rb
172 def origin_association
173   target_class && target_class.associations[@origin]
174 end
origin_type() click to toggle source
    # File lib/active_graph/node/has_n/association.rb
176 def origin_type
177   origin_association.relationship_type
178 end
type_keys_error_message(keys) click to toggle source
    # File lib/active_graph/node/has_n/association.rb
224 def type_keys_error_message(keys)
225   type_keys = (keys & [:type, :origin, :rel_class])
226   if type_keys.size > 1
227     "Only one of 'type', 'origin', or 'rel_class' options are allowed for associations"
228   elsif type_keys.empty?
229     "The 'type' option must be specified( even if it is `nil`) or `origin`/`rel_class` must be specified"
230   end
231 end
validate_association_options!(_association_name, options) click to toggle source
    # File lib/active_graph/node/has_n/association.rb
210 def validate_association_options!(_association_name, options)
211   ClassArguments.validate_argument!(options[:model_class], 'model_class')
212   ClassArguments.validate_argument!(options[:rel_class], 'rel_class')
213 
214   message = case
215             when (message = type_keys_error_message(options.keys))
216               message
217             when !(unknown_keys = options.keys - VALID_ASSOCIATION_OPTION_KEYS).empty?
218               "Unknown option(s) specified: #{unknown_keys.join(', ')}"
219             end
220 
221   fail ArgumentError, message if message
222 end
validate_init_arguments(type, direction, name, options) click to toggle source
    # File lib/active_graph/node/has_n/association.rb
199 def validate_init_arguments(type, direction, name, options)
200   validate_association_options!(name, options)
201   validate_option_combinations(options)
202   validate_dependent(options[:dependent].try(:to_sym))
203   check_valid_type_and_dir(type, direction)
204 end
validate_option_combinations(options) click to toggle source
    # File lib/active_graph/node/has_n/association.rb
238 def validate_option_combinations(options)
239   [[:type, :origin],
240    [:type, :rel_class],
241    [:origin, :rel_class]].each do |key1, key2|
242     if options[key1] && options[key2]
243       fail ArgumentError, "Cannot specify both :#{key1} and :#{key2} (#{base_declaration})"
244     end
245   end
246 end
validate_origin!() click to toggle source
    # File lib/active_graph/node/has_n/association.rb
259 def validate_origin!
260   return if not @origin
261 
262   association = origin_association
263 
264   message = case
265             when !target_class
266               'Cannot use :origin without a model_class (implied or explicit)'
267             when !association
268               "Origin `#{@origin.inspect}` association not found for #{target_class} (specified in #{base_declaration})"
269             when @direction == association.direction
270               "Origin `#{@origin.inspect}` (specified in #{base_declaration}) has same direction `#{@direction}`)"
271             end
272 
273   fail ArgumentError, message if message
274 end