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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/massive_record/orm/base.rb', line 108

def initialize(attributes = {})
  @new_record = true
  @destroyed = @readonly = false
  @relation_proxy_cache = {}

  attributes = {} if attributes.nil?

  self.attributes_raw = attributes_from_field_definition
  self.attributes = attributes

  clear_dirty_states!

  _run_initialize_callbacks
end

Class Method Details

.===(other) ⇒ Object



85
86
87
# File 'lib/massive_record/orm/base.rb', line 85

def ===(other)
  other.is_a? self
end

.base_classObject



70
71
72
# File 'lib/massive_record/orm/base.rb', line 70

def base_class
  class_of_descendant(self)
end

.inheritance_attributeObject



75
76
77
# File 'lib/massive_record/orm/base.rb', line 75

def inheritance_attribute
  @inheritance_attribute ||= "type"
end

.reset_table_name_configuration!Object



65
66
67
68
# File 'lib/massive_record/orm/base.rb', line 65

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

.set_inheritance_attribute(value = nil, &block) ⇒ Object Also known as: inheritance_attribute=



79
80
81
# File 'lib/massive_record/orm/base.rb', line 79

def set_inheritance_attribute(value = nil, &block)
  define_attr_method :inheritance_attribute, value, &block
end

.table_nameObject



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

def table_name
  @table_name ||= table_name_prefix + table_name_without_pre_and_suffix + table_name_suffix
end

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



60
61
62
# File 'lib/massive_record/orm/base.rb', line 60

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

.table_name_without_pre_and_suffixObject



56
57
58
# File 'lib/massive_record/orm/base.rb', line 56

def table_name_without_pre_and_suffix
  (table_name_overriden.blank? ? base_class.to_s.demodulize.underscore.pluralize : table_name_overriden)
end

Instance Method Details

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



154
155
156
# File 'lib/massive_record/orm/base.rb', line 154

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

#cloneObject



200
201
202
203
204
# File 'lib/massive_record/orm/base.rb', line 200

def clone
  object = self.class.new
  object.init_with('attributes' => attributes.select{|k| !['id', 'created_at', 'updated_at'].include?(k)})
  object
end

#freezeObject



163
164
165
# File 'lib/massive_record/orm/base.rb', line 163

def freeze
  @attributes.freeze
end

#frozen?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/massive_record/orm/base.rb', line 167

def frozen?
  @attributes.frozen?
end

#hashObject



159
160
161
# File 'lib/massive_record/orm/base.rb', line 159

def hash
  id.hash
end

#idObject



181
182
183
184
185
186
187
# File 'lib/massive_record/orm/base.rb', line 181

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
  column_family :base do
    field :name
  end
end

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


139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/massive_record/orm/base.rb', line 139

def init_with(coder)
  @new_record = false
  @destroyed = @readonly = false
  @relation_proxy_cache = {}

  self.attributes_raw = coder['attributes']
  fill_attributes_with_default_values_where_nil_is_not_allowed

  _run_find_callbacks
  _run_initialize_callbacks

  self
end

#inspectObject



172
173
174
175
176
177
178
# File 'lib/massive_record/orm/base.rb', line 172

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



195
196
197
# File 'lib/massive_record/orm/base.rb', line 195

def readonly!
  @readonly = true
end

#readonly?Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/massive_record/orm/base.rb', line 191

def readonly?
  !!@readonly
end