Class: Volt::RepoCache::Association
- Inherits:
-
Object
- Object
- Volt::RepoCache::Association
- Includes:
- Util
- Defined in:
- lib/volt/repo_cache/association.rb
Instance Attribute Summary collapse
-
#foreign_collection_name ⇒ Object
readonly
Returns the value of attribute foreign_collection_name.
-
#foreign_id_field ⇒ Object
readonly
Returns the value of attribute foreign_id_field.
-
#foreign_model_class ⇒ Object
readonly
Returns the value of attribute foreign_model_class.
-
#foreign_model_class_name ⇒ Object
readonly
Returns the value of attribute foreign_model_class_name.
-
#foreign_name ⇒ Object
readonly
Returns the value of attribute foreign_name.
-
#local_collection ⇒ Object
readonly
Returns the value of attribute local_collection.
-
#local_id_field ⇒ Object
readonly
Returns the value of attribute local_id_field.
-
#local_name_plural ⇒ Object
readonly
Returns the value of attribute local_name_plural.
-
#local_name_singular ⇒ Object
readonly
Returns the value of attribute local_name_singular.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #belongs_to? ⇒ Boolean
- #cache ⇒ Object
-
#foreign_collection ⇒ Object
Must be lazy initialization since we don’t know order in which collections will be loaded to cache.
- #has_any? ⇒ Boolean
- #has_many? ⇒ Boolean
- #has_one? ⇒ Boolean
-
#initialize(local_collection, foreign_name, type) ⇒ Association
constructor
A new instance of Association.
-
#inspect ⇒ Object
Hide circular references to local and foreign collections for inspection.
-
#reciprocal ⇒ Object
Returns the reciprocal association which may be nil if the foreign_collection is not interested (has not specified) the reciprocal association.
- #reciprocated? ⇒ Boolean
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_name ⇒ Object (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_field ⇒ Object (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_class ⇒ Object (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_name ⇒ Object (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_name ⇒ Object (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_collection ⇒ Object (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_field ⇒ Object (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_plural ⇒ Object (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_singular ⇒ Object (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 |
#type ⇒ Object (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
88 89 90 |
# File 'lib/volt/repo_cache/association.rb', line 88 def belongs_to? type == :belongs_to end |
#cache ⇒ Object
39 40 41 |
# File 'lib/volt/repo_cache/association.rb', line 39 def cache @local_collection.cache end |
#foreign_collection ⇒ Object
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
84 85 86 |
# File 'lib/volt/repo_cache/association.rb', line 84 def has_any? has_one? || has_many? end |
#has_many? ⇒ Boolean
80 81 82 |
# File 'lib/volt/repo_cache/association.rb', line 80 def has_many? type == :has_many end |
#has_one? ⇒ Boolean
76 77 78 |
# File 'lib/volt/repo_cache/association.rb', line 76 def has_one? type == :has_one end |
#inspect ⇒ Object
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 |
#reciprocal ⇒ Object
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
72 73 74 |
# File 'lib/volt/repo_cache/association.rb', line 72 def reciprocated? !!reciprocal end |