Module: MassiveRecord::ORM::Persistence

Extended by:
ActiveSupport::Concern
Defined in:
lib/massive_record/orm/persistence.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#atomic_increment!(attr_name, by = 1) ⇒ Object

Atomic increment of an attribute. Please note that it’s the adapter (or the wrapper) which needs to guarantee that the update is atomic, and as of writing this the Thrift adapter / wrapper does not do this anatomic.



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

def atomic_increment!(attr_name, by = 1)
  ensure_that_we_have_table_and_column_families!
  attr_name = attr_name.to_s

  row = row_for_record
  row.values = attributes_to_row_values_hash([attr_name])
  self[attr_name] = row.atomic_increment(attributes_schema[attr_name].unique_name, by).to_i
end

#decrement(attr_name, by = 1) ⇒ Object



98
99
100
101
102
103
# File 'lib/massive_record/orm/persistence.rb', line 98

def decrement(attr_name, by = 1)
  raise NotNumericalFieldError unless attributes_schema[attr_name.to_s].type == :integer
  self[attr_name] ||= 0
  self[attr_name] -= by
  self
end

#decrement!(attr_name, by = 1) ⇒ Object



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

def decrement!(attr_name, by = 1)
  decrement(attr_name, by).update_attribute(attr_name, self[attr_name])
end

#destroyObject Also known as: delete



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

def destroy
  @destroyed = row_for_record.destroy and freeze
end

#destroyed?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/massive_record/orm/persistence.rb', line 27

def destroyed?
  @destroyed
end

#increment(attr_name, by = 1) ⇒ Object



74
75
76
77
78
79
# File 'lib/massive_record/orm/persistence.rb', line 74

def increment(attr_name, by = 1)
  raise NotNumericalFieldError unless attributes_schema[attr_name.to_s].type == :integer
  self[attr_name] ||= 0
  self[attr_name] += by
  self
end

#increment!(attr_name, by = 1) ⇒ Object



81
82
83
# File 'lib/massive_record/orm/persistence.rb', line 81

def increment!(attr_name, by = 1)
  increment(attr_name, by).update_attribute(attr_name, self[attr_name])
end

#new_record?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/massive_record/orm/persistence.rb', line 19

def new_record?
  @new_record
end

#persisted?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/massive_record/orm/persistence.rb', line 23

def persisted?
  !(new_record? || destroyed?)
end

#reloadObject



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

def reload
  self.attributes_raw = self.class.find(id).attributes
  self
end

#saveObject



37
38
39
# File 'lib/massive_record/orm/persistence.rb', line 37

def save(*)
  create_or_update
end

#save!Object



41
42
43
# File 'lib/massive_record/orm/persistence.rb', line 41

def save!(*)
  create_or_update or raise RecordNotSaved
end

#touchObject

TODO This actually does nothing atm, but it’s here and callbacks on it

is working.


62
63
64
# File 'lib/massive_record/orm/persistence.rb', line 62

def touch
  true
end

#update_attribute(attr_name, value) ⇒ Object



45
46
47
48
# File 'lib/massive_record/orm/persistence.rb', line 45

def update_attribute(attr_name, value)
  send("#{attr_name}=", value)
  save(:validate => false)
end

#update_attributes(attributes) ⇒ Object



50
51
52
53
# File 'lib/massive_record/orm/persistence.rb', line 50

def update_attributes(attributes)
  self.attributes = attributes
  save
end

#update_attributes!(attributes) ⇒ Object



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

def update_attributes!(attributes)
  self.attributes = attributes
  save!
end