Class: MassiveRecord::ORM::Embedded

Inherits:
Base
  • Object
show all
Includes:
InTheMiddleOfSavingTracker, Schema::EmbeddedInterface
Defined in:
lib/massive_record/orm/embedded.rb

Constant Summary collapse

DATABASE_ID_SEPARATOR =
'|'

Instance Attribute Summary

Attributes included from InTheMiddleOfSavingTracker

#in_the_middle_of_saving

Class Method Summary collapse

Instance Method Summary collapse

Methods included from InTheMiddleOfSavingTracker

#save, #save!, #valid?

Methods included from Schema::EmbeddedInterface

#add_field, #attributes_db_raw_data_hash, #attributes_to_row_values_hash

Methods inherited from Base

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

Constructor Details

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

Class Method Details

.database_id(klass, id) ⇒ Object



32
33
34
# File 'lib/massive_record/orm/embedded.rb', line 32

def self.database_id(klass, id)
  [klass.base_class.to_s.underscore, id].join(DATABASE_ID_SEPARATOR)
end

.parse_database_id(database_id) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/massive_record/orm/embedded.rb', line 19

def self.parse_database_id(database_id)
  if splitted = database_id.split(DATABASE_ID_SEPARATOR) and splitted.length == 2
    splitted
  else
    fail InvalidEmbeddedDatabaseId.new(
      <<-TXT
        Expected database id '#{database_id}' to be on a format like
        base_class_here#{DATABASE_ID_SEPARATOR}record_id_here
      TXT
    )
  end
end

Instance Method Details

#database_idObject

Database id is base_class plus the record’s id. It is given, as we might want to embed records in an existing column family, or share a family for multiple types. In which case, we’ll end up with a column family like this:

| key | attributes |


| “address|123” | { :street => “Askerveien”, :number => “12”, etc… } | | “address|124” | { :street => “Askerveien”, :number => “12”, etc… } | | “name” | “Thorbjorn Hermansen” | | “age” | “30” |

..in this case we fetch embedded records to collection addresses by scoping on keys which starts with base_class name. The records itself will only have id equal to 123 and 124.



53
54
55
56
57
# File 'lib/massive_record/orm/embedded.rb', line 53

def database_id # :nodoc:
  if id
    self.class.database_id(self.class, id)
  end
end

#database_id=(database_id) ⇒ Object

Writer for database id. Used when loading records to easily set record’s id. Should not have the need to be used in other situations.



63
64
65
# File 'lib/massive_record/orm/embedded.rb', line 63

def database_id=(database_id) # :nodoc:
  self.id = self.class.parse_database_id(database_id)[1]
end