class Neo4j::ActiveNode::HasN::Association
Constants
- VALID_ASSOCIATION_OPTION_KEYS
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/neo4j/active_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.pluralize.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/neo4j/active_node/has_n/association.rb 147 def _create_relationship(start_object, node_or_nodes, properties) 148 RelFactory.create(start_object, node_or_nodes, properties, self) 149 end
callback(type)
click to toggle source
# File lib/neo4j/active_node/has_n/association.rb 102 def callback(type) 103 @callbacks[type] 104 end
create_method()
click to toggle source
# File lib/neo4j/active_node/has_n/association.rb 143 def create_method 144 unique? ? :create_unique : :create 145 end
creates_unique_option()
click to toggle source
# File lib/neo4j/active_node/has_n/association.rb 139 def creates_unique_option 140 @unique || :none 141 end
derive_model_class()
click to toggle source
# File lib/neo4j/active_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/neo4j/active_node/has_n/association.rb 86 def discovered_model 87 target_classes.select do |constant| 88 constant.ancestors.include?(::Neo4j::ActiveNode) 89 end 90 end
pending_model_refresh?()
click to toggle source
# File lib/neo4j/active_node/has_n/association.rb 56 def pending_model_refresh? 57 !!@pending_model_refresh 58 end
perform_callback(caller, other_node, type)
click to toggle source
# File lib/neo4j/active_node/has_n/association.rb 106 def perform_callback(caller, other_node, type) 107 return if callback(type).nil? 108 caller.send(callback(type), other_node) 109 end
queue_model_refresh!()
click to toggle source
# File lib/neo4j/active_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/neo4j/active_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
relationship_class()
click to toggle source
# File lib/neo4j/active_node/has_n/association.rb 130 def relationship_class 131 @relationship_class ||= @relationship_class_name && @relationship_class_name.constantize 132 end
relationship_class?()
click to toggle source
# File lib/neo4j/active_node/has_n/association.rb 151 def relationship_class? 152 !!relationship_class 153 end
Also aliased as: rel_class?
relationship_class_type()
click to toggle source
# File lib/neo4j/active_node/has_n/association.rb 126 def relationship_class_type 127 relationship_class._type.to_sym 128 end
relationship_type(create = false)
click to toggle source
# File lib/neo4j/active_node/has_n/association.rb 111 def relationship_type(create = false) 112 case 113 when relationship_class 114 relationship_class_type 115 when !@relationship_type.nil? 116 @relationship_type 117 when @origin 118 origin_type 119 else 120 (create || exceptional_target_class?) && decorated_rel_type(@name) 121 end 122 end
target_class()
click to toggle source
# File lib/neo4j/active_node/has_n/association.rb 92 def target_class 93 return @target_class if @target_class 94 95 return if !(target_class_names && target_class_names.size == 1) 96 97 class_const = ClassArguments.constantize_argument(target_class_names[0]) 98 99 @target_class = class_const 100 end
target_class_names()
click to toggle source
# File lib/neo4j/active_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/neo4j/active_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/neo4j/active_node/has_n/association.rb 70 def target_classes 71 ClassArguments.constantize_argument(target_class_names) 72 end
target_classes_or_nil()
click to toggle source
# File lib/neo4j/active_node/has_n/association.rb 74 def target_classes_or_nil 75 @target_classes_or_nil ||= discovered_model if target_class_names 76 end
target_where_clause()
click to toggle source
# File lib/neo4j/active_node/has_n/association.rb 78 def target_where_clause 79 return if model_class == false 80 81 Array.new(target_classes).map do |target_class| 82 "#{name}:`#{target_class.mapped_label_name}`" 83 end.join(' OR ') 84 end
unique?()
click to toggle source
# File lib/neo4j/active_node/has_n/association.rb 134 def unique? 135 return relationship_class.unique? if rel_class? 136 @origin ? origin_association.unique? : !!@unique 137 end
Private Instance Methods
apply_vars_from_options(options)
click to toggle source
# File lib/neo4j/active_node/has_n/association.rb 185 def apply_vars_from_options(options) 186 @relationship_class_name = options[:rel_class] && options[:rel_class].to_s 187 @relationship_type = options[:type] && options[:type].to_sym 188 189 @model_class = options[:model_class] 190 @callbacks = {before: options[:before], after: options[:after]} 191 @origin = options[:origin] && options[:origin].to_sym 192 @dependent = options[:dependent].try(:to_sym) 193 @unique = options[:unique] 194 end
association_model_namespace()
click to toggle source
# File lib/neo4j/active_node/has_n/association.rb 158 def association_model_namespace 159 Neo4j::Config.association_model_namespace_string 160 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/neo4j/active_node/has_n/association.rb 199 def base_declaration 200 "#{type} #{direction.inspect}, #{name.inspect}" 201 end
check_valid_type_and_dir(type, direction)
click to toggle source
# File lib/neo4j/active_node/has_n/association.rb 235 def check_valid_type_and_dir(type, direction) 236 fail ArgumentError, "Invalid association type: #{type.inspect} (valid value: :has_many and :has_one)" if ![:has_many, :has_one].include?(type.to_sym) 237 fail ArgumentError, "Invalid direction: #{direction.inspect} (valid value: :out, :in, and :both)" if ![:out, :in, :both].include?(direction.to_sym) 238 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/neo4j/active_node/has_n/association.rb 255 def exceptional_target_class? 256 # TODO: Exceptional if target_class.nil?? (when model_class false) 257 258 target_class && target_class.name != @target_class_name_from_name 259 end
get_direction(create, reverse = false)
click to toggle source
# File lib/neo4j/active_node/has_n/association.rb 162 def get_direction(create, reverse = false) 163 dir = (create && @direction == :both) ? :out : @direction 164 if reverse 165 case dir 166 when :in then :out 167 when :out then :in 168 else :both 169 end 170 else 171 dir 172 end 173 end
origin_association()
click to toggle source
# File lib/neo4j/active_node/has_n/association.rb 175 def origin_association 176 target_class.associations[@origin] 177 end
origin_type()
click to toggle source
# File lib/neo4j/active_node/has_n/association.rb 179 def origin_type 180 origin_association.relationship_type 181 end
type_keys_error_message(keys)
click to toggle source
# File lib/neo4j/active_node/has_n/association.rb 226 def type_keys_error_message(keys) 227 type_keys = (keys & [:type, :origin, :rel_class]) 228 if type_keys.size > 1 229 "Only one of 'type', 'origin', or 'rel_class' options are allowed for associations" 230 elsif type_keys.empty? 231 "The 'type' option must be specified( even if it is `nil`) or `origin`/`rel_class` must be specified" 232 end 233 end
validate_association_options!(_association_name, options)
click to toggle source
# File lib/neo4j/active_node/has_n/association.rb 212 def validate_association_options!(_association_name, options) 213 ClassArguments.validate_argument!(options[:model_class], 'model_class') 214 ClassArguments.validate_argument!(options[:rel_class], 'rel_class') 215 216 message = case 217 when (message = type_keys_error_message(options.keys)) 218 message 219 when (unknown_keys = options.keys - VALID_ASSOCIATION_OPTION_KEYS).size > 0 220 "Unknown option(s) specified: #{unknown_keys.join(', ')}" 221 end 222 223 fail ArgumentError, message if message 224 end
validate_init_arguments(type, direction, name, options)
click to toggle source
# File lib/neo4j/active_node/has_n/association.rb 203 def validate_init_arguments(type, direction, name, options) 204 validate_association_options!(name, options) 205 validate_option_combinations(options) 206 validate_dependent(options[:dependent].try(:to_sym)) 207 check_valid_type_and_dir(type, direction) 208 end
validate_option_combinations(options)
click to toggle source
# File lib/neo4j/active_node/has_n/association.rb 240 def validate_option_combinations(options) 241 [[:type, :origin], 242 [:type, :rel_class], 243 [:origin, :rel_class]].each do |key1, key2| 244 if options[key1] && options[key2] 245 fail ArgumentError, "Cannot specify both :#{key1} and :#{key2} (#{base_declaration})" 246 end 247 end 248 end
validate_origin!()
click to toggle source
# File lib/neo4j/active_node/has_n/association.rb 261 def validate_origin! 262 return if not @origin 263 264 association = origin_association 265 266 message = case 267 when !target_class 268 'Cannot use :origin without a model_class (implied or explicit)' 269 when !association 270 "Origin `#{@origin.inspect}` association not found for #{target_class} (specified in #{base_declaration})" 271 when @direction == association.direction 272 "Origin `#{@origin.inspect}` (specified in #{base_declaration}) has same direction `#{@direction}`)" 273 end 274 275 fail ArgumentError, message if message 276 end