Sha256: 42a13722c6a8d2cd129098be876a41095aeefaf5d44992504e5ab19d6f85f36c

Contents?: true

Size: 1.28 KB

Versions: 5

Compression:

Stored size: 1.28 KB

Contents

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

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

    included do
      class_attribute :key_factory
      key :uuid
    end

    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)
        self.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 key
      @key ||= self.class.next_key(self)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gotime-cassandra_object-2.11.9 lib/cassandra_object/identity.rb
gotime-cassandra_object-2.11.8 lib/cassandra_object/identity.rb
gotime-cassandra_object-2.11.7 lib/cassandra_object/identity.rb
gotime-cassandra_object-2.11.6 lib/cassandra_object/identity.rb
gotime-cassandra_object-2.11.5 lib/cassandra_object/identity.rb