Sha256: b87a18d33ddc7d3eff4d7e6595dc0aa045677d0fa11b4fd126b9d50e0afa637f

Contents?: true

Size: 1.41 KB

Versions: 5

Compression:

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

      def primary_key
        'id'
      end
    end

    def key
      @key ||= self.class.next_key(self)
    end

    def id
      key.to_s
    end

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gotime-cassandra_object-2.11.0 lib/cassandra_object/identity.rb
gotime-cassandra_object-2.10.11 lib/cassandra_object/identity.rb
gotime-cassandra_object-2.10.10 lib/cassandra_object/identity.rb
gotime-cassandra_object-2.10.9 lib/cassandra_object/identity.rb
gotime-cassandra_object-2.10.8 lib/cassandra_object/identity.rb