Class: MassiveRecord::ORM::Base
- Inherits:
-
Object
- Object
- MassiveRecord::ORM::Base
- Includes:
- ActiveModel::Conversion
- Defined in:
- lib/massive_record/orm/base.rb
Class Method Summary collapse
- .===(other) ⇒ Object
- .base_class ⇒ Object
- .inheritance_attribute ⇒ Object
- .reset_table_name_configuration! ⇒ Object
- .set_inheritance_attribute(value = nil, &block) ⇒ Object (also: inheritance_attribute=)
- .table_name ⇒ Object
- .table_name=(name) ⇒ Object (also: set_table_name)
- .table_name_without_pre_and_suffix ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #clone ⇒ Object
- #freeze ⇒ Object
- #frozen? ⇒ Boolean
- #hash ⇒ Object
- #id ⇒ Object
- #id=(id) ⇒ Object
-
#init_with(coder) ⇒ Object
Initialize an empty model object from
coder
. -
#initialize(*args) ⇒ Base
constructor
Initialize a new object.
- #inspect ⇒ Object
-
#raw_data ⇒ Object
The raw data is raw values returned by the adapter.
- #readonly! ⇒ Object
- #readonly? ⇒ Boolean
-
#reinit_with(coder) ⇒ Object
:nodoc:.
-
#update_raw_data_for_column_family(column_family, new_values) ⇒ Object
:nodoc:.
Constructor Details
#initialize(*args) ⇒ 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 You can call initialize in multiple ways:
ORMClass.new(attr_one: value, attr_two: value)
ORMClass.new("the-id-of-the-new-record")
ORMClass.new("the-id-of-the-new-record", attr_one: value, attr_two: value)
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/massive_record/orm/base.rb', line 159 def initialize(*args) attributes = args. id = args.first @new_record = true @destroyed = @readonly = false @relation_proxy_cache = {} @raw_data = Hash.new { |h,k| h[k] = {} } clear_dirty_states! self.attributes_raw = attributes_from_field_definition.merge('id' => id) self.attributes = attributes _run_initialize_callbacks end |
Class Method Details
.===(other) ⇒ Object
132 133 134 |
# File 'lib/massive_record/orm/base.rb', line 132 def ===(other) other.is_a? self end |
.base_class ⇒ Object
117 118 119 |
# File 'lib/massive_record/orm/base.rb', line 117 def base_class class_of_descendant(self) end |
.inheritance_attribute ⇒ Object
122 123 124 |
# File 'lib/massive_record/orm/base.rb', line 122 def inheritance_attribute @inheritance_attribute ||= "type" end |
.reset_table_name_configuration! ⇒ Object
112 113 114 115 |
# File 'lib/massive_record/orm/base.rb', line 112 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=
126 127 128 |
# File 'lib/massive_record/orm/base.rb', line 126 def set_inheritance_attribute(value = nil, &block) define_attr_method :inheritance_attribute, value, &block end |
.table_name ⇒ Object
99 100 101 |
# File 'lib/massive_record/orm/base.rb', line 99 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
107 108 109 |
# File 'lib/massive_record/orm/base.rb', line 107 def table_name=(name) self.table_name_overriden = name end |
.table_name_without_pre_and_suffix ⇒ Object
103 104 105 |
# File 'lib/massive_record/orm/base.rb', line 103 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?
214 215 216 |
# File 'lib/massive_record/orm/base.rb', line 214 def ==(other) other.equal?(self) || other.instance_of?(self.class) && id == other.id end |
#clone ⇒ Object
266 267 268 269 270 |
# File 'lib/massive_record/orm/base.rb', line 266 def clone object = self.class.new object.init_with('attributes' => attributes.select{|k| !['id', 'created_at', 'updated_at'].include?(k)}) object end |
#freeze ⇒ Object
223 224 225 226 |
# File 'lib/massive_record/orm/base.rb', line 223 def freeze @attributes.freeze self end |
#frozen? ⇒ Boolean
228 229 230 |
# File 'lib/massive_record/orm/base.rb', line 228 def frozen? @attributes.frozen? end |
#hash ⇒ Object
219 220 221 |
# File 'lib/massive_record/orm/base.rb', line 219 def hash id.hash end |
#id ⇒ Object
242 243 244 245 246 247 248 |
# File 'lib/massive_record/orm/base.rb', line 242 def id if read_attribute(:id).blank? && respond_to?(:default_id, true) @attributes["id"] = default_id end read_attribute(:id) end |
#id=(id) ⇒ Object
250 251 252 253 |
# File 'lib/massive_record/orm/base.rb', line 250 def id=(id) id = id.to_s unless id.blank? write_attribute(:id, 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'
193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/massive_record/orm/base.rb', line 193 def init_with(coder) @new_record = false @destroyed = @readonly = false @relation_proxy_cache = {} reinit_with(coder) fill_attributes_with_default_values_where_nil_is_not_allowed _run_find_callbacks _run_initialize_callbacks self end |
#inspect ⇒ Object
233 234 235 236 237 238 239 |
# File 'lib/massive_record/orm/base.rb', line 233 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 |
#raw_data ⇒ Object
The raw data is raw values returned by the adapter. It is a nested hash like:
{
'family' => {
'attr1' => 'value'
'attr2' => 'value'
},
'addresses' => {
'address-1' => {'serialized' => 'attributes', 'for' => 'address-2'}
}
...
}
289 290 291 |
# File 'lib/massive_record/orm/base.rb', line 289 def raw_data @raw_data.dup end |
#readonly! ⇒ Object
261 262 263 |
# File 'lib/massive_record/orm/base.rb', line 261 def readonly! @readonly = true end |
#readonly? ⇒ Boolean
257 258 259 |
# File 'lib/massive_record/orm/base.rb', line 257 def readonly? !!@readonly end |
#reinit_with(coder) ⇒ Object
:nodoc:
207 208 209 210 211 |
# File 'lib/massive_record/orm/base.rb', line 207 def reinit_with(coder) # :nodoc: @raw_data = Hash.new { |h,k| h[k] = {} } @raw_data.update(coder['raw_data']) if coder.has_key? 'raw_data' self.attributes_raw = coder['attributes'] end |
#update_raw_data_for_column_family(column_family, new_values) ⇒ Object
:nodoc:
293 294 295 |
# File 'lib/massive_record/orm/base.rb', line 293 def update_raw_data_for_column_family(column_family, new_values) # :nodoc: @raw_data[column_family] = new_values end |