Class: RPCMapper::Association::Base
- Inherits:
-
Object
- Object
- RPCMapper::Association::Base
- Defined in:
- lib/rpc_mapper/association.rb
Overview
Association::Base
This is the base class for all associations. It defines the basic structure of an association. The basic nomenclature is as follows:
TODO: Clean up this nomenclature. Source and Target should be switched. From the configuration side this is already done but the internal naming is backwards.
Source: The start point of the association. The source class is the class on which the association is defined.
Proxy: On a through association the proxy is the class on which the target association lives
Target: The class that will ultimately be returned by the association
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
-
#options ⇒ Object
Returns the value of attribute options.
-
#source_klass ⇒ Object
Returns the value of attribute source_klass.
Instance Method Summary collapse
- #collection? ⇒ Boolean
-
#eager_loadable? ⇒ Boolean
TRP: Only eager loadable if association query does not depend on instance data.
- #foreign_key ⇒ Object
-
#initialize(klass, id, options = {}) ⇒ Base
constructor
A new instance of Base.
- #polymorphic? ⇒ Boolean
- #primary_key ⇒ Object
- #scope(object) ⇒ Object
- #target_klass(object = nil) ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(klass, id, options = {}) ⇒ Base
Returns a new instance of Base.
25 26 27 28 29 |
# File 'lib/rpc_mapper/association.rb', line 25 def initialize(klass, id, ={}) self.source_klass = klass self.id = id.to_sym self. = end |
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
23 24 25 |
# File 'lib/rpc_mapper/association.rb', line 23 def id @id end |
#options ⇒ Object
Returns the value of attribute options.
23 24 25 |
# File 'lib/rpc_mapper/association.rb', line 23 def @options end |
#source_klass ⇒ Object
Returns the value of attribute source_klass.
23 24 25 |
# File 'lib/rpc_mapper/association.rb', line 23 def source_klass @source_klass end |
Instance Method Details
#collection? ⇒ Boolean
42 43 44 |
# File 'lib/rpc_mapper/association.rb', line 42 def collection? raise NotImplementedError, "You must define collection? in subclasses." end |
#eager_loadable? ⇒ Boolean
TRP: Only eager loadable if association query does not depend on instance data
94 95 96 97 98 |
# File 'lib/rpc_mapper/association.rb', line 94 def eager_loadable? RPCMapper::Relation::FINDER_OPTIONS.inject(true) do |condition, key| condition && ![key].respond_to?(:call) end end |
#foreign_key ⇒ Object
50 51 52 |
# File 'lib/rpc_mapper/association.rb', line 50 def foreign_key [:foreign_key] end |
#polymorphic? ⇒ Boolean
35 36 37 38 39 40 |
# File 'lib/rpc_mapper/association.rb', line 35 def polymorphic? raise( NotImplementedError, "You must define how your association is polymorphic in subclasses." ) end |
#primary_key ⇒ Object
46 47 48 |
# File 'lib/rpc_mapper/association.rb', line 46 def primary_key [:primary_key] || :id end |
#scope(object) ⇒ Object
88 89 90 |
# File 'lib/rpc_mapper/association.rb', line 88 def scope(object) raise NotImplementedError, "You must define scope in subclasses" end |
#target_klass(object = nil) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/rpc_mapper/association.rb', line 54 def target_klass(object=nil) if [:polymorphic] && object poly_type = object.is_a?(RPCMapper::Base) ? object.send("#{id}_type") : object end klass = if poly_type type_string = [ [:polymorphic_namespace], sanitize_type_attribute(poly_type) ].compact.join('::') begin eval(type_string) rescue NameError => err raise( RPCMapper::PolymorphicAssociationTypeError, "No constant defined called #{type_string}" ) end else unless [:class_name] raise(ArgumentError, ":class_name option required for association declaration.") end unless [:class_name] =~ /^::/ [:class_name] = "::#{[:class_name]}" end eval([:class_name]) end RPCMapper::Base.resolve_leaf_klass klass end |
#type ⇒ Object
31 32 33 |
# File 'lib/rpc_mapper/association.rb', line 31 def type raise NotImplementedError, "You must define the type in subclasses." end |