Sha256: 8df25d74c7dbd80aa96425a006749e368266f70b6af8be47d3a12515afa39c17

Contents?: true

Size: 1.26 KB

Versions: 41

Compression:

Stored size: 1.26 KB

Contents

module CassandraObject
  module Identity
    extend ActiveSupport::Concern
    extend ActiveSupport::Autoload

    autoload :Key
    autoload :AbstractKeyFactory
    autoload :UUIDKeyFactory
    autoload :NaturalKeyFactory
    autoload :HashedNaturalKeyFactory
    autoload :CustomKeyFactory

    module ClassMethods
      # Indicate what kind of key the model will have: uuid or natural
      #
      # @param [:uuid, :natural] the type of key
      # @param the options you want to pass along to the key factory (like :attributes => :name, for a natural key).
      # 
      def key(name_or_factory = :uuid, *options)
        @key_factory = case name_or_factory
        when :uuid
          UUIDKeyFactory.new
        when :natural
          NaturalKeyFactory.new(*options)
        when :custom
          CustomKeyFactory.new(*options)
        else
          name_or_factory
        end
      end

      def next_key(object = nil)
        @key_factory.next_key(object).tap do |key|
          raise "Keys may not be nil" if key.nil?
        end
      end

      def parse_key(string)
        @key_factory.parse(string)
      end
    end

    def id
      key.to_s
    end
    alias :quoted_id :id

    def id=(key)
      self.key = self.class.parse_key(key)
      id
    end
  end
end

Version data entries

41 entries across 41 versions & 1 rubygems

Version Path
sessionm-cassandra_object-2.3.2 lib/cassandra_object/identity.rb
sessionm-cassandra_object-2.3.1 lib/cassandra_object/identity.rb
sessionm-cassandra_object-2.3.0 lib/cassandra_object/identity.rb
sessionm-cassandra_object-2.2.55 lib/cassandra_object/identity.rb
sessionm-cassandra_object-2.2.54 lib/cassandra_object/identity.rb
sessionm-cassandra_object-2.2.53 lib/cassandra_object/identity.rb
sessionm-cassandra_object-2.2.52 lib/cassandra_object/identity.rb
sessionm-cassandra_object-2.2.51 lib/cassandra_object/identity.rb
sessionm-cassandra_object-2.2.50 lib/cassandra_object/identity.rb
sessionm-cassandra_object-2.2.49 lib/cassandra_object/identity.rb
sessionm-cassandra_object-2.2.48 lib/cassandra_object/identity.rb
sessionm-cassandra_object-2.2.47 lib/cassandra_object/identity.rb
sessionm-cassandra_object-2.2.46 lib/cassandra_object/identity.rb
sessionm-cassandra_object-2.2.45 lib/cassandra_object/identity.rb
sessionm-cassandra_object-2.2.44 lib/cassandra_object/identity.rb
sessionm-cassandra_object-2.2.43 lib/cassandra_object/identity.rb
sessionm-cassandra_object-2.2.42 lib/cassandra_object/identity.rb
sessionm-cassandra_object-2.2.41 lib/cassandra_object/identity.rb
sessionm-cassandra_object-2.2.40 lib/cassandra_object/identity.rb
sessionm-cassandra_object-2.2.39 lib/cassandra_object/identity.rb