Sha256: cd46a66d834131669b9a3d56544458e23c546759b9e384d20691fc974e5d720c

Contents?: true

Size: 1.54 KB

Versions: 20

Compression:

Stored size: 1.54 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 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

20 entries across 20 versions & 1 rubygems

Version Path
gotime-cassandra_object-4.5.1 lib/cassandra_object/belongs_to.rb
gotime-cassandra_object-4.5.0 lib/cassandra_object/belongs_to.rb
gotime-cassandra_object-4.4.5 lib/cassandra_object/belongs_to.rb
gotime-cassandra_object-4.4.4 lib/cassandra_object/belongs_to.rb
gotime-cassandra_object-4.4.3 lib/cassandra_object/belongs_to.rb
gotime-cassandra_object-4.4.0 lib/cassandra_object/belongs_to.rb
gotime-cassandra_object-4.3.2 lib/cassandra_object/belongs_to.rb
gotime-cassandra_object-4.3.1 lib/cassandra_object/belongs_to.rb
gotime-cassandra_object-4.3.0 lib/cassandra_object/belongs_to.rb
gotime-cassandra_object-4.2.2 lib/cassandra_object/belongs_to.rb
gotime-cassandra_object-4.2.0 lib/cassandra_object/belongs_to.rb
gotime-cassandra_object-4.1.0 lib/cassandra_object/belongs_to.rb
gotime-cassandra_object-4.0.2 lib/cassandra_object/belongs_to.rb
gotime-cassandra_object-4.0.1 lib/cassandra_object/belongs_to.rb
gotime-cassandra_object-4.0.0 lib/cassandra_object/belongs_to.rb
gotime-cassandra_object-3.0.5 lib/cassandra_object/belongs_to.rb
gotime-cassandra_object-3.0.4 lib/cassandra_object/belongs_to.rb
gotime-cassandra_object-3.0.3 lib/cassandra_object/belongs_to.rb
gotime-cassandra_object-3.0.2 lib/cassandra_object/belongs_to.rb
gotime-cassandra_object-3.0.1 lib/cassandra_object/belongs_to.rb