Class: RPCMapper::Association::Base
- Inherits:
-
Object
- Object
- RPCMapper::Association::Base
- Defined in:
- lib/rpc_mapper/association.rb
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.
8 9 10 11 12 |
# File 'lib/rpc_mapper/association.rb', line 8 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.
6 7 8 |
# File 'lib/rpc_mapper/association.rb', line 6 def id @id end |
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/rpc_mapper/association.rb', line 6 def @options end |
#source_klass ⇒ Object
Returns the value of attribute source_klass.
6 7 8 |
# File 'lib/rpc_mapper/association.rb', line 6 def source_klass @source_klass end |
Instance Method Details
#collection? ⇒ Boolean
22 23 24 |
# File 'lib/rpc_mapper/association.rb', line 22 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
51 52 53 |
# File 'lib/rpc_mapper/association.rb', line 51 def eager_loadable? RPCMapper::Relation::FINDER_OPTIONS.inject(true) { |condition, key| condition && ![key].respond_to?(:call) } end |
#foreign_key ⇒ Object
30 31 32 |
# File 'lib/rpc_mapper/association.rb', line 30 def foreign_key [:foreign_key] end |
#polymorphic? ⇒ Boolean
18 19 20 |
# File 'lib/rpc_mapper/association.rb', line 18 def polymorphic? raise NotImplementedError, "You must define how your association is polymorphic in subclasses." end |
#primary_key ⇒ Object
26 27 28 |
# File 'lib/rpc_mapper/association.rb', line 26 def primary_key [:primary_key] || :id end |
#scope(object) ⇒ Object
46 47 48 |
# File 'lib/rpc_mapper/association.rb', line 46 def scope(object) raise NotImplementedError, "You must define scope in subclasses" end |
#target_klass(object = nil) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/rpc_mapper/association.rb', line 34 def target_klass(object=nil) klass = if [:polymorphic] eval [[:polymorphic_namespace], object.send("#{id}_type")].compact.join('::') if object else raise(ArgumentError, ":class_name option required for association declaration.") unless [:class_name] [:class_name] = "::#{[:class_name]}" unless [:class_name] =~ /^::/ eval([:class_name]) end RPCMapper::Base.resolve_leaf_klass klass end |
#type ⇒ Object
14 15 16 |
# File 'lib/rpc_mapper/association.rb', line 14 def type raise NotImplementedError, "You must define the type in subclasses." end |