Class: RPCMapper::Association::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rpc_mapper/association.rb

Direct Known Subclasses

BelongsTo, Has

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options={})
  self.source_klass = klass
  self.id = id.to_sym
  self.options = options
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/rpc_mapper/association.rb', line 6

def id
  @id
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/rpc_mapper/association.rb', line 6

def options
  @options
end

#source_klassObject

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

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


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

Returns:

  • (Boolean)


51
52
53
# File 'lib/rpc_mapper/association.rb', line 51

def eager_loadable?
  RPCMapper::Relation::FINDER_OPTIONS.inject(true) { |condition, key| condition && !options[key].respond_to?(:call) }
end

#foreign_keyObject



30
31
32
# File 'lib/rpc_mapper/association.rb', line 30

def foreign_key
  options[:foreign_key]
end

#polymorphic?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


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_keyObject



26
27
28
# File 'lib/rpc_mapper/association.rb', line 26

def primary_key
  options[:primary_key] || :id
end

#scope(object) ⇒ Object

Raises:

  • (NotImplementedError)


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 options[:polymorphic]
    eval [options[:polymorphic_namespace], object.send("#{id}_type")].compact.join('::') if object
  else
    raise(ArgumentError, ":class_name option required for association declaration.") unless options[:class_name]
    options[:class_name] = "::#{options[:class_name]}" unless options[:class_name] =~ /^::/
    eval(options[:class_name])
  end

  RPCMapper::Base.resolve_leaf_klass klass
end

#typeObject

Raises:

  • (NotImplementedError)


14
15
16
# File 'lib/rpc_mapper/association.rb', line 14

def type
  raise NotImplementedError, "You must define the type in subclasses."
end