Sha256: f3e91e3ada950027920b7cdbff1bdb10132f111eadb972905e1f90b184de05ab

Contents?: true

Size: 1.63 KB

Versions: 45

Compression:

Stored size: 1.63 KB

Contents

module CassandraObject
  module BelongsTo
    extend ActiveSupport::Concern

    included do
      class_attribute :belongs_to_reflections
      self.belongs_to_reflections = {}
    end

    module ClassMethods
      # === Options
      # [:class_name]
      #   Use if the class cannot be inferred from the association
      # [:polymorphic]
      #   Specify if the association is polymorphic
      # Example:
      #   class Driver < CassandraObject::Base
      #   end
      #   class Truck < CassandraObject::Base
      #   end
      def belongs_to(name, options = {})
        CassandraObject::BelongsTo::Builder.build(self, name, options)
      end

      def generated_belongs_to_methods
        @generated_belongs_to_methods ||= begin
          mod = const_set(:GeneratedBelongsToMethods, Module.new)
          include mod
          mod
        end
      end
    end

    # Returns the belongs_to instance for the given name, instantiating it if it doesn't already exist
    def belongs_to_association(name)
      association = belongs_to_instance_get(name)

      if association.nil?
        association = CassandraObject::BelongsTo::Association.new(self, belongs_to_reflections[name])
        belongs_to_instance_set(name, association)
      end

      association
    end

    private
      def clear_belongs_to_cache
        belongs_to_cache.clear if persisted?
      end

      def belongs_to_cache
        @belongs_to_cache ||= {}
      end

      def belongs_to_instance_get(name)
        belongs_to_cache[name.to_sym]
      end

      def belongs_to_instance_set(name, association)
        belongs_to_cache[name.to_sym] = association
      end
  end
end

Version data entries

45 entries across 45 versions & 3 rubygems

Version Path
extendi-cassandra_object-1.1.1 lib/cassandra_object/belongs_to.rb
extendi-cassandra_object-1.1.0 lib/cassandra_object/belongs_to.rb
extendi-cassandra_object-1.0.20 lib/cassandra_object/belongs_to.rb
extendi-cassandra_object-1.0.19 lib/cassandra_object/belongs_to.rb
extendi-cassandra_object-1.0.18 lib/cassandra_object/belongs_to.rb
extendi-cassandra_object-1.0.17 lib/cassandra_object/belongs_to.rb
extendi-cassandra_object-1.0.16 lib/cassandra_object/belongs_to.rb
extendi-cassandra_object-1.0.15 lib/cassandra_object/belongs_to.rb
extendi-cassandra_object-1.0.14 lib/cassandra_object/belongs_to.rb
extendi-cassandra_object-1.0.13 lib/cassandra_object/belongs_to.rb
extendi-cassandra_object-1.0.12 lib/cassandra_object/belongs_to.rb
extendi-cassandra_object-1.0.11 lib/cassandra_object/belongs_to.rb
extendi-cassandra_object-1.0.10 lib/cassandra_object/belongs_to.rb
extendi-cassandra_object-1.0.9 lib/cassandra_object/belongs_to.rb
extendi-cassandra_object-1.0.8 lib/cassandra_object/belongs_to.rb
extendi-cassandra_object-1.0.7 lib/cassandra_object/belongs_to.rb
extendi-cassandra_object-1.0.6 lib/cassandra_object/belongs_to.rb
extendi-cassandra_object-1.0.5 lib/cassandra_object/belongs_to.rb
extendi-cassandra_object-1.0.4 lib/cassandra_object/belongs_to.rb
extendi-cassandra_object-1.0.1 lib/cassandra_object/belongs_to.rb