Sha256: d07f4e727ea86132ca17942dbdabd8a9c1dbe5fb60215e31315b3a17b73b039a

Contents?: true

Size: 1.74 KB

Versions: 11

Compression:

Stored size: 1.74 KB

Contents

require 'cassandra_object/identity/abstract_key_factory'
require 'cassandra_object/identity/key'
require 'cassandra_object/identity/uuid_key_factory'
require 'cassandra_object/identity/natural_key_factory'

module CassandraObject
  # Some docs will be needed here but the gist of this is simple.  Instead of returning a string, Base#key  now returns a key object.
  # There are corresponding key factories which generate them
  module Identity
    extend ActiveSupport::Concern
    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)
        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
    
    module InstanceMethods

      def ==(comparison_object)
        comparison_object.equal?(self) ||
          (comparison_object.instance_of?(self.class) &&
            comparison_object.key == key &&
            !comparison_object.new_record?)
      end

      def eql?(comparison_object)
        self == (comparison_object)
      end

      def hash
        key.to_s.hash
      end
      
      def to_param
        key.to_param
      end

      def to_key
        [key] if key
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
gotime-cassandra_object-0.8.2 lib/cassandra_object/identity.rb
gotime-cassandra_object-0.8.1 lib/cassandra_object/identity.rb
gotime-cassandra_object-0.8.0 lib/cassandra_object/identity.rb
gotime-cassandra_object-0.7.8 lib/cassandra_object/identity.rb
gotime-cassandra_object-0.7.7 lib/cassandra_object/identity.rb
gotime-cassandra_object-0.7.6 lib/cassandra_object/identity.rb
gotime-cassandra_object-0.7.5 lib/cassandra_object/identity.rb
gotime-cassandra_object-0.7.4 lib/cassandra_object/identity.rb
gotime-cassandra_object-0.7.3 lib/cassandra_object/identity.rb
gotime-cassandra_object-0.7.2 lib/cassandra_object/identity.rb
gotime-cassandra_object-0.7.1 lib/cassandra_object/identity.rb