Class: Cassandro::Model
- Inherits:
-
Object
show all
- Defined in:
- lib/cassandro/model.rb
Defined Under Namespace
Classes: ModelException
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attrs = {}, persisted = false) ⇒ Model
Returns a new instance of Model.
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/cassandro/model.rb', line 7
def initialize(attrs = {}, persisted = false)
@attributes = {}
@errors = {}
@persisted = persisted
attrs.each do |att, val|
next unless self.respond_to?(:"#{att}=")
case val.class.name
when "Set"
send(:"#{att}=", val.to_a)
when "Cassandra::Uuid"
send(:"#{att}=", val.to_s)
when "Time"
send(:"#{att}=", val.to_datetime)
else
send(:"#{att}=", val)
end
end
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
5
6
7
|
# File 'lib/cassandro/model.rb', line 5
def attributes
@attributes
end
|
Class Method Details
.[](value) ⇒ Object
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
# File 'lib/cassandro/model.rb', line 155
def self.[](value)
return nil if value.nil? || (value.respond_to?(:empty?) && value.empty?)
if value.is_a?(Hash)
where = "#{value.keys.map{ |k| "#{k} = ?" }.join(' AND ')} ALLOW FILTERING"
values = value.map{ |k,v| casts[k] == :uuid ? Cassandra::Uuid.new(v) : v }
else
where = "#{partition_key} = ?"
values = [casts[partition_key] == :uuid ? Cassandra::Uuid.new(value) : value]
end
query = " SELECT *\n FROM \#{table_name}\n WHERE \#{where}\n QUERY\n\n st = Cassandro.client.prepare(query)\n result = Cassandro.client.execute(st, *values)\n\n return nil unless result.any?\n\n self.new(self.parse_row(result.first), true)\nend\n"
|
.all ⇒ Object
187
188
189
190
191
192
193
194
195
196
|
# File 'lib/cassandro/model.rb', line 187
def self.all
query = "SELECT * FROM #{self.table_name}"
rows = Cassandro.execute(query)
all = []
rows.each do |row|
all << new(self.parse_row(row), true)
end
all
end
|
.attribute(name, type = String, options = {}) ⇒ Object
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/cassandro/model.rb', line 126
def self.attribute(name, type = String, options = {})
attributes << name
casts[name] = type
define_method(name) do
@attributes[name]
end
define_method(:"#{name}=") do |value|
@attributes[name] = value
end
end
|
.attributes ⇒ Object
246
247
248
|
# File 'lib/cassandro/model.rb', line 246
def self.attributes
@attributes ||= []
end
|
.cast_as(key, value) ⇒ Object
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
# File 'lib/cassandro/model.rb', line 274
def self.cast_as(key, value)
return "NULL" if value.nil?
case casts[key]
when :text
"'#{value}'"
when :int, :integer
value.to_i
when :datetime
value.strftime("%Q").to_i
else
"#{value}"
end
end
|
.casts ⇒ Object
262
263
264
|
# File 'lib/cassandro/model.rb', line 262
def self.casts
@cast ||= {}
end
|
.count(key = nil, value = nil) ⇒ Object
214
215
216
217
218
219
220
221
222
223
224
225
226
227
|
# File 'lib/cassandro/model.rb', line 214
def self.count(key = nil, value = nil)
query = "SELECT count(*) FROM #{table_name}"
if key && value
key = key.to_sym
query << " WHERE #{key} = ? ALLOW FILTERING"
st = Cassandro.client.prepare(query)
results = Cassandro.client.execute(st, value)
else
results = Cassandro.client.execute(query)
end
results.first["count"]
end
|
.create(attrs = {}) ⇒ Object
180
181
182
183
184
185
|
# File 'lib/cassandro/model.rb', line 180
def self.create(attrs = {})
model = new(attrs)
model.save(true)
model
end
|
.create_with_ttl(seconds, attrs = {}) ⇒ Object
301
302
303
304
305
306
307
308
309
|
# File 'lib/cassandro/model.rb', line 301
def self.create_with_ttl(seconds, attrs = {})
old_ttl = self.options[:ttl]
self.options[:ttl] = seconds.to_i
result = self.create(attrs)
old_ttl ? self.options[:ttl] = old_ttl : self.options.delete(:ttl)
result
end
|
.destroy_all ⇒ Object
229
230
231
232
233
234
235
236
237
|
# File 'lib/cassandro/model.rb', line 229
def self.destroy_all
begin
query = "TRUNCATE #{table_name}"
st = Cassandro.execute(query)
st.is_a? Cassandra::Client::VoidResult
rescue e
false
end
end
|
.options ⇒ Object
293
294
295
|
# File 'lib/cassandro/model.rb', line 293
def self.options
@options ||= {}
end
|
.parse_row(row) ⇒ Object
359
360
361
|
# File 'lib/cassandro/model.rb', line 359
def self.parse_row(row)
row.each{ |k,v| row[k] = [] if casts[k.to_sym] == :set && v.nil? }
end
|
.partition_key ⇒ Object
289
290
291
|
# File 'lib/cassandro/model.rb', line 289
def self.partition_key
pk.first
end
|
.pk ⇒ Object
250
251
252
|
# File 'lib/cassandro/model.rb', line 250
def self.pk
@pk ||= []
end
|
.primary_key(keys) ⇒ Object
139
140
141
142
143
144
145
|
# File 'lib/cassandro/model.rb', line 139
def self.primary_key(keys)
if keys.is_a?(Array)
pk.push(*keys)
else
pk << keys
end
end
|
.query(where, *values) ⇒ Object
239
240
241
242
243
|
# File 'lib/cassandro/model.rb', line 239
def self.query(where, *values)
query = "SELECT * FROM #{table_name} WHERE #{where} ALLOW FILTERING"
st = Cassandro.client.prepare(query)
Cassandro.client.execute(st, *values)
end
|
.table(name) ⇒ Object
122
123
124
|
# File 'lib/cassandro/model.rb', line 122
def self.table(name)
self.table_name = name.to_s
end
|
.table_name ⇒ Object
254
255
256
|
# File 'lib/cassandro/model.rb', line 254
def self.table_name
@table_name ||= name.downcase
end
|
.table_name=(name) ⇒ Object
258
259
260
|
# File 'lib/cassandro/model.rb', line 258
def self.table_name=(name)
@table_name = name
end
|
.ttl(seconds) ⇒ Object
297
298
299
|
# File 'lib/cassandro/model.rb', line 297
def self.ttl(seconds)
self.options[:ttl] = seconds.to_i
end
|
.unique(keys) ⇒ Object
147
148
149
150
151
152
153
|
# File 'lib/cassandro/model.rb', line 147
def self.unique(keys)
if keys.is_a?(Array)
uniques.push(*keys)
else
uniques << keys
end
end
|
.uniqueness_defined? ⇒ Boolean
270
271
272
|
# File 'lib/cassandro/model.rb', line 270
def self.uniqueness_defined?
uniques.any?
end
|
.uniques ⇒ Object
266
267
268
|
# File 'lib/cassandro/model.rb', line 266
def self.uniques
@unique ||= []
end
|
.where(key, value) ⇒ Object
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
# File 'lib/cassandro/model.rb', line 198
def self.where(key, value)
key = key.to_sym
results = []
query = "SELECT * FROM #{table_name} WHERE #{key} = ? ALLOW FILTERING"
st = Cassandro.client.prepare(query)
rows = Cassandro.client.execute(st, value)
rows.each do |result|
results << new(result, true)
end
results
end
|
Instance Method Details
#cast(attribute) ⇒ Object
43
44
45
|
# File 'lib/cassandro/model.rb', line 43
def cast(attribute)
self.class.cast_as(attribute, @attributes[attribute])
end
|
#clear_errors ⇒ Object
35
36
37
|
# File 'lib/cassandro/model.rb', line 35
def clear_errors
@errors = {}
end
|
#destroy ⇒ Object
114
115
116
117
118
119
120
|
# File 'lib/cassandro/model.rb', line 114
def destroy
query = " DELETE FROM \#{self.class.table_name}\n WHERE \#{self.class.pk.flatten.map { |k| \"\#{k.to_s} = \#{self.class.cast_as(k, @attributes[k])}\" }.join(' AND ')}\n QUERY\n Cassandro.execute(query)\nend\n"
|
#errors ⇒ Object
31
32
33
|
# File 'lib/cassandro/model.rb', line 31
def errors
@errors
end
|
#persisted? ⇒ Boolean
27
28
29
|
# File 'lib/cassandro/model.rb', line 27
def persisted?
@persisted
end
|
#save(insert_check = false) ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/cassandro/model.rb', line 82
def save(insert_check = false)
clear_errors
if self.class.casts[:id] == :uuid
@attributes[:id] ||= SecureRandom.uuid
end
self.class.pk.flatten.each do |key|
if @attributes[key].nil?
@errors[:primary_key] = "#{key.to_s}_cant_be_nil"
return false
end
end
if !persisted? && self.class.uniqueness_defined? && !unique?
@errors[:unique] = "#{self.class.to_s.downcase}_not_unique"
return false
end
st = self.statement_for(:insert, :insert_check => insert_check)
begin
r = Cassandro.client.execute(st, *self.native_attributes)
raise ModelException.new('not_applied') unless !insert_check || (insert_check && r.first["[applied]"])
@persisted = true
rescue => e
@attributes[:id] = nil if !persisted? && @attributes.has_key?(:id)
@errors[:save] = e.message
false
end
end
|
#unique? ⇒ Boolean
47
48
49
|
# File 'lib/cassandro/model.rb', line 47
def unique?
self.class.uniqueness_defined? ? self.class[@attributes.slice(*self.class.uniques)].nil? : true
end
|
#update_attributes(attrs = {}) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/cassandro/model.rb', line 51
def update_attributes(attrs = {})
attrs = attrs.inject({}) do |memo, (k,v)|
memo[k.to_sym] = (v.nil? || v.to_s.empty?) ? nil : v
memo
end
p_keys = []
fields = []
self.class.pk.flatten.each do |k|
p_keys << "#{k} = #{cast(k)}"
end
attrs.keys.each do |field|
fields << "#{field} = ?"
end
query = "UPDATE #{self.class.table_name} SET #{fields.join(", ")} "
query += "WHERE #{p_keys.join(" AND ")}"
begin
st = Cassandro.client.prepare(query)
Cassandro.client.execute(st, *native_attributes(attrs))
@attributes.merge!(attrs)
true
rescue Exception => e
@errors[:update_error] = e.message
false
end
end
|
#valid? ⇒ Boolean
39
40
41
|
# File 'lib/cassandro/model.rb', line 39
def valid?
self.class.pk.any? && @errors.empty?
end
|