Class: RPCMapper::Association::Base

Inherits:
Object
  • Object
show all
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

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.



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

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.



23
24
25
# File 'lib/rpc_mapper/association.rb', line 23

def id
  @id
end

#optionsObject

Returns the value of attribute options.



23
24
25
# File 'lib/rpc_mapper/association.rb', line 23

def options
  @options
end

#source_klassObject

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

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


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

Returns:

  • (Boolean)


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 && !options[key].respond_to?(:call)
  end
end

#foreign_keyObject



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

def foreign_key
  options[:foreign_key]
end

#polymorphic?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


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_keyObject



46
47
48
# File 'lib/rpc_mapper/association.rb', line 46

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

#scope(object) ⇒ Object

Raises:

  • (NotImplementedError)


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 options[:polymorphic] && object
    poly_type = object.is_a?(RPCMapper::Base) ? object.send("#{id}_type") : object
  end

  klass = if poly_type
    type_string = [
      options[: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 options[:class_name]
      raise(ArgumentError,
            ":class_name option required for association declaration.")
    end

    unless options[:class_name] =~ /^::/
      options[:class_name] = "::#{options[:class_name]}"
    end

    eval(options[:class_name])
  end

  RPCMapper::Base.resolve_leaf_klass klass
end

#typeObject

Raises:

  • (NotImplementedError)


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

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