Class: MassiveRecord::ORM::IdFactory

Inherits:
Table
  • Object
show all
Includes:
Singleton
Defined in:
lib/massive_record/orm/id_factory.rb

Overview

A factory class for unique IDs for any given tables.

Usage:

IdFactory.next_for(:cars) # => 1
IdFactory.next_for(:cars) # => 2
IdFactory.next_for(AClassRespondingToTableName)         # => 1
IdFactory.next_for("a_class_responding_to_table_names") # => 2

Storage:

Stored in id_factories table, under column family named tables.
Field name equals to tables it has generated ids for, and it's
values is integers (if the adapter supports it).

Constant Summary collapse

COLUMN_FAMILY_FOR_TABLES =
:tables
ID =
"id_factory"

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Schema::TableInterface

#add_field_to_column_family

Methods inherited from Base

#==, ===, base_class, #clone, #freeze, #frozen?, #hash, inheritance_attribute, #init_with, #initialize, #inspect, #readonly!, #readonly?, reset_table_name_configuration!, set_inheritance_attribute, table_name, table_name=, table_name_without_pre_and_suffix

Constructor Details

This class inherits a constructor from MassiveRecord::ORM::Base

Class Method Details

.instanceObject

Returns the factory, singleton class. It will be a reloaded version each time instance is retrieved, or else it will fetch self from the database, or if all other fails return a new of self.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/massive_record/orm/id_factory.rb', line 37

def self.instance
  if table_exists?
    begin
      if @instance
        @instance.reload
      else
        @instance = find(ID)
      end
    rescue RecordNotFound
      @instance = nil
    end
  end

  @instance = new unless @instance
  @instance
end

.next_for(table) ⇒ Object

Delegates to the instance, just a shout cut.



57
58
59
# File 'lib/massive_record/orm/id_factory.rb', line 57

def self.next_for(table)
  instance.next_for(table)
end

Instance Method Details

#idObject



74
75
76
# File 'lib/massive_record/orm/id_factory.rb', line 74

def id
  ID
end

#next_for(table) ⇒ Object

Returns a new and unique id for a given table name Table can a symbol, string or an object responding to table_name



67
68
69
70
# File 'lib/massive_record/orm/id_factory.rb', line 67

def next_for(table)
  table = table.respond_to?(:table_name) ? table.table_name : table.to_s
  next_id :table => table
end