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
- #readonly! ⇒ Object
- #readonly? ⇒ Boolean
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)
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/massive_record/orm/base.rb', line 142 def initialize(*args) attributes = args. id = args.first @new_record = true @destroyed = @readonly = false @relation_proxy_cache = {} self.attributes_raw = attributes_from_field_definition.merge('id' => id) self.attributes = attributes clear_dirty_states! _run_initialize_callbacks end |
Class Method Details
.===(other) ⇒ Object
115 116 117 |
# File 'lib/massive_record/orm/base.rb', line 115 def ===(other) other.is_a? self end |
.base_class ⇒ Object
100 101 102 |
# File 'lib/massive_record/orm/base.rb', line 100 def base_class class_of_descendant(self) end |
.inheritance_attribute ⇒ Object
105 106 107 |
# File 'lib/massive_record/orm/base.rb', line 105 def inheritance_attribute @inheritance_attribute ||= "type" end |
.reset_table_name_configuration! ⇒ Object
95 96 97 98 |
# File 'lib/massive_record/orm/base.rb', line 95 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=
109 110 111 |
# File 'lib/massive_record/orm/base.rb', line 109 def set_inheritance_attribute(value = nil, &block) define_attr_method :inheritance_attribute, value, &block end |
.table_name ⇒ Object
82 83 84 |
# File 'lib/massive_record/orm/base.rb', line 82 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
90 91 92 |
# File 'lib/massive_record/orm/base.rb', line 90 def table_name=(name) self.table_name_overriden = name end |
.table_name_without_pre_and_suffix ⇒ Object
86 87 88 |
# File 'lib/massive_record/orm/base.rb', line 86 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?
189 190 191 |
# File 'lib/massive_record/orm/base.rb', line 189 def ==(other) other.equal?(self) || other.instance_of?(self.class) && id == other.id end |
#clone ⇒ Object
240 241 242 243 244 |
# File 'lib/massive_record/orm/base.rb', line 240 def clone object = self.class.new object.init_with('attributes' => attributes.select{|k| !['id', 'created_at', 'updated_at'].include?(k)}) object end |
#freeze ⇒ Object
198 199 200 |
# File 'lib/massive_record/orm/base.rb', line 198 def freeze @attributes.freeze end |
#frozen? ⇒ Boolean
202 203 204 |
# File 'lib/massive_record/orm/base.rb', line 202 def frozen? @attributes.frozen? end |
#hash ⇒ Object
194 195 196 |
# File 'lib/massive_record/orm/base.rb', line 194 def hash id.hash end |
#id ⇒ Object
216 217 218 219 220 221 222 |
# File 'lib/massive_record/orm/base.rb', line 216 def id if read_attribute(:id).blank? && respond_to?(:default_id, true) @attributes["id"] = default_id end read_attribute(:id) end |
#id=(id) ⇒ Object
224 225 226 227 |
# File 'lib/massive_record/orm/base.rb', line 224 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'
174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/massive_record/orm/base.rb', line 174 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 |
#inspect ⇒ Object
207 208 209 210 211 212 213 |
# File 'lib/massive_record/orm/base.rb', line 207 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
235 236 237 |
# File 'lib/massive_record/orm/base.rb', line 235 def readonly! @readonly = true end |
#readonly? ⇒ Boolean
231 232 233 |
# File 'lib/massive_record/orm/base.rb', line 231 def readonly? !!@readonly end |