class Volt::RepoCache::Association

Attributes

foreign_collection_name[R]
foreign_id_field[R]
foreign_model_class[R]
foreign_model_class_name[R]
foreign_name[R]
local_collection[R]
local_id_field[R]
local_name_plural[R]
local_name_singular[R]
type[R]

Public Class Methods

new(local_collection, foreign_name, type) click to toggle source
# 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

Public Instance Methods

belongs_to?() click to toggle source
# File lib/volt/repo_cache/association.rb, line 85
def belongs_to?
  type == :belongs_to
end
cache() click to toggle source
# File lib/volt/repo_cache/association.rb, line 39
def cache
  @local_collection.cache
end
foreign_collection() click to toggle source

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

# File lib/volt/repo_cache/association.rb, line 46
def foreign_collection
  @foreign_collection ||= cache.collections[@foreign_collection_name]
end
has_any?() click to toggle source
# File lib/volt/repo_cache/association.rb, line 81
def has_any?
  has_one? || has_many?
end
has_many?() click to toggle source
# File lib/volt/repo_cache/association.rb, line 77
def has_many?
  type == :has_many
end
has_one?() click to toggle source
# File lib/volt/repo_cache/association.rb, line 73
def has_one?
  type == :has_one
end
inspect() click to toggle source

Hide circular references to local and foreign collections for inspection.

Calls superclass method
# 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
reciprocal() click to toggle source

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.

# File lib/volt/repo_cache/association.rb, line 59
def reciprocal
  unless @reciprocal
    @reciprocal = foreign_collection.associations.values.detect do |a|
      a.foreign_collection.name == local_collection.name
    end
    @reciprocal = :nil unless @reciprocal
  end
  @reciprocal == :nil ? nil : @reciprocal
end
reciprocated?() click to toggle source
# File lib/volt/repo_cache/association.rb, line 69
def reciprocated?
  !!reciprocal
end

Private Instance Methods

__debug(level, file, line, method, msg = nil) click to toggle source
# File lib/volt/repo_cache/association.rb, line 95
def __debug(level, file, line, method, msg = nil)
   s = "#{file}[#{line}]:#{self.class.name}##{method}: #{msg}"
   if RUBY_PLATFORM == 'opal'
     Volt.logger.debug s
   else
     puts s
   end
 end
uncache() click to toggle source
# File lib/volt/repo_cache/association.rb, line 91
def uncache
  @local_collection = @foreign_collection = @reciprocal = nil
end