Sha256: 7efdf0ea4c3c097073552e10aef11ccea6ce97aea9db1e1af3fb4537721bc38d

Contents?: true

Size: 1.18 KB

Versions: 21

Compression:

Stored size: 1.18 KB

Contents

module CassandraObject
  module BelongsTo
    class Association
      attr_reader :owner, :reflection
      attr_accessor :record_variable
      delegate :options, to: :reflection

      def initialize(owner, reflection)
        @loaded = false
        @owner = owner
        @reflection = reflection
      end

      def reader
        unless loaded?
          if record_id = owner.send(reflection.foreign_key).presence
            self.record_variable = association_class.find_by_id(record_id)
          else
            self.record_variable = nil
          end
          @loaded = true
        end

        record_variable
      end

      def writer(record)
        self.record_variable = record
        @loaded = true
        owner.send("#{reflection.foreign_key}=", record.try(:id))
        if reflection.polymorphic?
          owner.send("#{reflection.polymorphic_column}=", record.class.name)
        end
      end

      def association_class
        association_class_name.constantize
      end

      def association_class_name
        reflection.polymorphic? ? owner.send(reflection.polymorphic_column) : reflection.class_name
      end

      def loaded?
        @loaded
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

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