Module: Cequel::Model::Persistence

Extended by:
ActiveSupport::Concern
Defined in:
lib/cequel/model/persistence.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#_hydrate(row) ⇒ Object



134
135
136
137
138
139
140
141
142
143
# File 'lib/cequel/model/persistence.rb', line 134

def _hydrate(row)
  @_cequel = InstanceInternals.new(self)
  tap do
    key_alias = self.class.key_alias.to_s
    key_alias = 'KEY' if key_alias.upcase == 'KEY'
    @_cequel.key = row[key_alias]
    @_cequel.attributes = row.except(key_alias)
    persisted!
  end
end

#destroyObject



119
120
121
# File 'lib/cequel/model/persistence.rb', line 119

def destroy
  data_set.delete
end

#insertObject

Raises:



97
98
99
100
101
102
# File 'lib/cequel/model/persistence.rb', line 97

def insert
  raise MissingKey if @_cequel.key.nil?
  return if @_cequel.attributes.empty?
  self.class.column_family.insert(attributes)
  persisted!
end

#persisted!Object



145
146
147
# File 'lib/cequel/model/persistence.rb', line 145

def persisted!
  @_cequel.persisted = true
end

#persisted?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/cequel/model/persistence.rb', line 153

def persisted?
  !!@_cequel.persisted
end

#reloadObject



123
124
125
126
127
128
129
130
131
132
# File 'lib/cequel/model/persistence.rb', line 123

def reload
  result = data_set.first
  key_alias = self.class.key_alias
  if result.keys == [key_alias.to_s]
    raise RecordNotFound,
      "Couldn't find #{self.class.name} with #{key_alias}=#{@_cequel.key}"
  end
  _hydrate(result)
  self
end

#saveObject



84
85
86
# File 'lib/cequel/model/persistence.rb', line 84

def save
  persisted? ? update : insert
end

#transient!Object



149
150
151
# File 'lib/cequel/model/persistence.rb', line 149

def transient!
  @_cequel.persisted = false
end

#transient?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/cequel/model/persistence.rb', line 157

def transient?
  !persisted?
end

#updateObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/cequel/model/persistence.rb', line 104

def update
  update_attributes, delete_attributes = {}, []
  changed.each do |attr|
    new = read_attribute(attr)
    if new.nil?
      delete_attributes << attr
    else
      update_attributes[attr] = new
    end
  end
  data_set.update(update_attributes) if update_attributes.any?
  data_set.delete(*delete_attributes) if delete_attributes.any?
  transient! if @_cequel.attributes.empty?
end

#update_attribute(column, value) ⇒ Object



93
94
95
# File 'lib/cequel/model/persistence.rb', line 93

def update_attribute(column, value)
  update_attributes(column => value)
end

#update_attributes(attributes) ⇒ Object



88
89
90
91
# File 'lib/cequel/model/persistence.rb', line 88

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