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
gotime-cassandra_object-4.9.0 lib/cassandra_object/belongs_to.rb
gotime-cassandra_object-4.8.1 lib/cassandra_object/belongs_to.rb
gotime-cassandra_object-4.8.0 lib/cassandra_object/belongs_to.rb
gotime-cassandra_object-4.7.3 lib/cassandra_object/belongs_to.rb
gotime-cassandra_object-4.7.2 lib/cassandra_object/belongs_to.rb