Class: MassiveRecord::ORM::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Conversion
Defined in:
lib/massive_record/orm/base.rb

Direct Known Subclasses

Column, Table

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

Initialize a new object. Send in attributes which we’ll dynamically set up read- and write methods for and assign to instance variables. How read- and write methods are defined might change over time when the DSL for describing column families and fields are in place



66
67
68
69
70
71
72
73
# File 'lib/massive_record/orm/base.rb', line 66

def initialize(attributes = {})
  self.attributes_raw = attributes_from_field_definition.merge(attributes)
  self.attributes = attributes
  @new_record = true
  @destroyed = @readonly = false

  _run_initialize_callbacks
end

Class Method Details

.reset_table_name_configuration!Object



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

def reset_table_name_configuration!
  @table_name = self.table_name_overriden = nil
  self.table_name_prefix = self.table_name_suffix = ""
end

.table_nameObject



44
45
46
# File 'lib/massive_record/orm/base.rb', line 44

def table_name
  @table_name ||= table_name_prefix + (table_name_overriden.blank? ? self.to_s.demodulize.underscore.pluralize : table_name_overriden) + table_name_suffix
end

.table_name=(name) ⇒ Object Also known as: set_table_name



48
49
50
# File 'lib/massive_record/orm/base.rb', line 48

def table_name=(name)
  self.table_name_overriden = name
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



99
100
101
# File 'lib/massive_record/orm/base.rb', line 99

def ==(other)
  other.equal?(self) || other.instance_of?(self.class) && id == other.id
end

#freezeObject



105
106
107
# File 'lib/massive_record/orm/base.rb', line 105

def freeze
  @attributes.freeze
end

#frozen?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/massive_record/orm/base.rb', line 109

def frozen?
  @attributes.frozen?
end

#idObject



123
124
125
126
127
128
129
# File 'lib/massive_record/orm/base.rb', line 123

def id
  if read_attribute(:id).blank? && respond_to?(:default_id, true)
    @attributes["id"] = default_id
  end

  read_attribute(:id)
end

#init_with(coder) ⇒ Object

Initialize an empty model object from coder. coder must contain the attributes necessary for initializing an empty model object. For example:

This should be used after finding a record from the database, as it will trust the coder’s attributes and not load it with default values.

class Person < MassiveRecord::ORM::Table
end

person = Person.allocate
person.init_with('attributes' => { 'name' => 'Alice' })
person.name # => 'Alice'


88
89
90
91
92
93
94
95
96
# File 'lib/massive_record/orm/base.rb', line 88

def init_with(coder)
  @new_record = false
  @destroyed = @readonly = false

  self.attributes_raw = coder['attributes']

  _run_find_callbacks
  _run_initialize_callbacks
end

#inspectObject



114
115
116
117
118
119
120
# File 'lib/massive_record/orm/base.rb', line 114

def inspect
  attributes_as_string = known_attribute_names_for_inspect.collect do |attr_name|
    "#{attr_name}: #{attribute_for_inspect(attr_name)}"
  end.join(', ')

  "#<#{self.class} id: #{id.inspect}, #{attributes_as_string}>"
end

#readonly!Object



137
138
139
# File 'lib/massive_record/orm/base.rb', line 137

def readonly!
  @readonly = true
end

#readonly?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/massive_record/orm/base.rb', line 133

def readonly?
  !!@readonly
end