Sha256: 2137b4e3ff0b6d0712d35d9c8b394e684ae311e9380a43ef0391638d6950e431

Contents?: true

Size: 1.23 KB

Versions: 25

Compression:

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

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

Version data entries

25 entries across 25 versions & 2 rubygems

Version Path
gotime-cassandra_object-2.7.1 lib/cassandra_object/identity.rb
gotime-cassandra_object-2.7.0 lib/cassandra_object/identity.rb
gotime-cassandra_object-2.6.4 lib/cassandra_object/identity.rb
gotime-cassandra_object-2.6.3 lib/cassandra_object/identity.rb
gotime-cassandra_object-2.6.2 lib/cassandra_object/identity.rb
gotime-cassandra_object-2.6.1 lib/cassandra_object/identity.rb
gotime-cassandra_object-2.6.0 lib/cassandra_object/identity.rb
gotime-cassandra_object-2.5.0 lib/cassandra_object/identity.rb
gotime-cassandra_object-2.4.2 lib/cassandra_object/identity.rb
gotime-cassandra_object-2.4.1 lib/cassandra_object/identity.rb
gotime-cassandra_object-2.4.0 lib/cassandra_object/identity.rb
gotime-cassandra_object-2.3.6 lib/cassandra_object/identity.rb
gotime-cassandra_object-2.3.5 lib/cassandra_object/identity.rb
gotime-cassandra_object-2.3.4 lib/cassandra_object/identity.rb
gotime-cassandra_object-2.3.3 lib/cassandra_object/identity.rb
gotime-cassandra_object-2.3.2 lib/cassandra_object/identity.rb
gotime-cassandra_object-2.3.1 lib/cassandra_object/identity.rb
gotime-cassandra_object-2.3.0 lib/cassandra_object/identity.rb
sessionm-cassandra_object-2.2.10 lib/cassandra_object/identity.rb
sessionm-cassandra_object-2.2.8 lib/cassandra_object/identity.rb