Class: Volt::RepoCache::Association

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/volt/repo_cache/association.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

adder, arrify, creator, debug, friend?, friends_only, not_yet_implemented, prefix_method, remover, setter, subclass_responsibility, time, unsupported

Constructor Details

#initialize(local_collection, foreign_name, type) ⇒ Association

Returns a new instance of Association.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/volt/repo_cache/association.rb', line 12

def initialize(local_collection, foreign_name, type)
  _local_name = local_collection.name.to_s.sub(/^_/, '')
  @local_name_singular = _local_name.singularize.to_sym
  @local_name_plural = _local_name.pluralize.to_sym
  @local_collection = local_collection
  @foreign_name = foreign_name
  @type = type
  @foreign_model_class_name = @foreign_name.to_s.singularize.camelize
  @foreign_model_class = Object.const_get(@foreign_model_class_name)
  @foreign_collection_name = :"_#{@foreign_name.to_s.pluralize}"
  @foreign_id_field = has_any? ? :"#{@local_collection.model_class_name.underscore}_id" : :id
  @local_id_field = belongs_to? ? :"#{@foreign_name.to_s}_id" : :id
end

Instance Attribute Details

#foreign_collection_nameObject (readonly)

Returns the value of attribute foreign_collection_name.



8
9
10
# File 'lib/volt/repo_cache/association.rb', line 8

def foreign_collection_name
  @foreign_collection_name
end

#foreign_id_fieldObject (readonly)

Returns the value of attribute foreign_id_field.



10
11
12
# File 'lib/volt/repo_cache/association.rb', line 10

def foreign_id_field
  @foreign_id_field
end

#foreign_model_classObject (readonly)

Returns the value of attribute foreign_model_class.



9
10
11
# File 'lib/volt/repo_cache/association.rb', line 9

def foreign_model_class
  @foreign_model_class
end

#foreign_model_class_nameObject (readonly)

Returns the value of attribute foreign_model_class_name.



9
10
11
# File 'lib/volt/repo_cache/association.rb', line 9

def foreign_model_class_name
  @foreign_model_class_name
end

#foreign_nameObject (readonly)

Returns the value of attribute foreign_name.



8
9
10
# File 'lib/volt/repo_cache/association.rb', line 8

def foreign_name
  @foreign_name
end

#local_collectionObject (readonly)

Returns the value of attribute local_collection.



7
8
9
# File 'lib/volt/repo_cache/association.rb', line 7

def local_collection
  @local_collection
end

#local_id_fieldObject (readonly)

Returns the value of attribute local_id_field.



10
11
12
# File 'lib/volt/repo_cache/association.rb', line 10

def local_id_field
  @local_id_field
end

#local_name_pluralObject (readonly)

Returns the value of attribute local_name_plural.



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

def local_name_plural
  @local_name_plural
end

#local_name_singularObject (readonly)

Returns the value of attribute local_name_singular.



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

def local_name_singular
  @local_name_singular
end

#typeObject (readonly)

Returns the value of attribute type.



10
11
12
# File 'lib/volt/repo_cache/association.rb', line 10

def type
  @type
end

Instance Method Details

#belongs_to?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/volt/repo_cache/association.rb', line 88

def belongs_to?
  type == :belongs_to
end

#cacheObject



39
40
41
# File 'lib/volt/repo_cache/association.rb', line 39

def cache
  @local_collection.cache
end

#foreign_collectionObject

Must be lazy initialization since we don’t know order in which collections will be loaded to cache.



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

def foreign_collection
  @foreign_collection ||= cache.collections[@foreign_collection_name]
end

#has_any?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/volt/repo_cache/association.rb', line 84

def has_any?
  has_one? || has_many?
end

#has_many?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/volt/repo_cache/association.rb', line 80

def has_many?
  type == :has_many
end

#has_one?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/volt/repo_cache/association.rb', line 76

def has_one?
  type == :has_one
end

#inspectObject

Hide circular references to local and foreign collections for inspection.



28
29
30
31
32
33
34
35
36
37
# File 'lib/volt/repo_cache/association.rb', line 28

def inspect
  __local = @local_collection
  __foreign = @foreign_collection
  @local_collection = "{{#{@local_collection ? @local_collection.name : :nil}}"
  @foreign_collection = "{{#{@foreign_collection ? @foreign_collection.name : :nil}}"
  result = super
  @local_collection = __local
  @foreign_collection = __foreign
  result
end

#reciprocalObject

Returns the reciprocal association which may be nil if the foreign_collection is not interested (has not specified) the reciprocal association. It may be, for example, that this association is a belongs_to, but there is no reciprocal has_one or has_many association in the ‘owner’. Must be lazy initialization since it depends on foreign_collection being lazily initialized.



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/volt/repo_cache/association.rb', line 59

def reciprocal
  unless @reciprocal
    # debug __method__, __LINE__, ""
    @reciprocal = foreign_collection.associations.values.detect do |a|
      # debug __method__, __LINE__, "#{a.foreign_collection.name} ?==? #{local_collection.name}"
      a.foreign_collection.name == local_collection.name
    end
    @reciprocal = :nil unless @reciprocal
    # debug __method__, __LINE__, "reciprocal of #{self.inspect} is #{@reciprocal.inspect}"
  end
  @reciprocal == :nil ? nil : @reciprocal
end

#reciprocated?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/volt/repo_cache/association.rb', line 72

def reciprocated?
  !!reciprocal
end