Class: DataMapper::Associations::Relationship
- Object
- DataMapper::Associations::Relationship
Attributes
Instance Attributes
| name | [RW] | public |
Returns the value of attribute name. |
|---|---|---|---|
| options | [RW] | public |
Returns the value of attribute options. |
| query | [RW] | public |
Returns the value of attribute query. |
| repository_name | [RW] | public |
Returns the value of attribute repository_name. |
Constants
- OPTIONS
- [ :class_name, :child_key, :parent_key, :min, :max, :through ]
Constructor Summary
private
initialize(name, repository_name, child_model_name, parent_model_name, options = {}, &loader)
+child_model_name and child_properties refers to the FK, parent_model_name and parent_properties refer to the PK. For more information: http://edocs.bea.com/kodo/docs41/full/html/jdo_overview_mapping_join.html I wash my hands of it!
[View source]
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
# File 'dm-core/lib/dm-core/associations/relationship.rb', line 71 def initialize(name, repository_name, child_model_name, parent_model_name, options = {}, &loader) raise ArgumentError, "+name+ should be a Symbol, but was #{name.class}", caller unless Symbol === name raise ArgumentError, "+repository_name+ must be a Symbol, but was #{repository_name.class}", caller unless Symbol === repository_name raise ArgumentError, "+child_model_name+ must be a String, but was #{child_model_name.class}", caller unless String === child_model_name raise ArgumentError, "+parent_model_name+ must be a String, but was #{parent_model_name.class}", caller unless String === parent_model_name if child_properties = options[:child_key] raise ArgumentError, "+options[:child_key]+ must be an Array or nil, but was #{child_properties.class}", caller unless Array === child_properties end if parent_properties = options[:parent_key] raise ArgumentError, "+parent_properties+ must be an Array or nil, but was #{parent_properties.class}", caller unless Array === parent_properties end @name = name @repository_name = repository_name @child_model_name = child_model_name @child_properties = child_properties # may be nil @query = options.reject { |k,v| OPTIONS.include?(k) } @parent_model_name = parent_model_name @parent_properties = parent_properties # may be nil @options = options @loader = loader end
Public Visibility
Public Instance Method Summary
| #attach_parent(child, parent) | |
|---|---|
| #child_key | |
| #child_model | |
| #get_children(parent, options = {}, finder = :all) | |
| #get_parent(child) | |
| #parent_key | |
| #parent_model |
Public Instance Methods Inherited from Object
Public Instance Method Details
attach_parent
public
attach_parent(child, parent)
[View source]
53 54 55
# File 'dm-core/lib/dm-core/associations/relationship.rb', line 53 def attach_parent(child, parent) child_key.set(child, parent && parent_key.get(parent)) end
child_key
public
child_key
[View source]
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# File 'dm-core/lib/dm-core/associations/relationship.rb', line 8 def child_key @child_key ||= begin model_properties = child_model.properties(repository_name) child_key = parent_key.zip(@child_properties || []).map do |parent_property,property_name| # TODO: use something similar to DM::NamingConventions to determine the property name property_name ||= "#{name}_#{parent_property.name}".to_sym model_properties[property_name] || DataMapper.repository(repository_name) { child_model.property(property_name, parent_property.type) } end PropertySet.new(child_key) end end
child_model
public
child_model
[View source]
61 62 63 64 65 66
# File 'dm-core/lib/dm-core/associations/relationship.rb', line 61 def child_model find_const(@child_model_name) end
get_children
public
get_children(parent, options = {}, finder = :all)
[View source]
34 35 36 37 38 39 40 41
# File 'dm-core/lib/dm-core/associations/relationship.rb', line 34 def get_children(parent, options = {}, finder = :all) bind_values = parent_key.get(parent) query = child_key.to_query(bind_values) DataMapper.repository(repository_name) do child_model.send(finder, @query.merge(options).merge(query)) end end
get_parent
public
get_parent(child)
[View source]
43 44 45 46 47 48 49 50 51
# File 'dm-core/lib/dm-core/associations/relationship.rb', line 43 def get_parent(child) bind_values = child_key.get(child) return nil if bind_values.any? { |bind_value| bind_value.nil? } query = parent_key.to_query(bind_values) DataMapper.repository(repository_name) do parent_model.first(@query.merge(query)) end end
parent_key
public
parent_key
[View source]
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
# File 'dm-core/lib/dm-core/associations/relationship.rb', line 22 def parent_key @parent_key ||= begin parent_key = if @parent_properties parent_model.properties(repository_name).slice(*@parent_properties) else parent_model.key(repository_name) end PropertySet.new(parent_key) end end
parent_model
public
parent_model
[View source]
57 58 59 60 61 62
# File 'dm-core/lib/dm-core/associations/relationship.rb', line 57 def parent_model find_const(@parent_model_name) end