whi-cassie 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b2075427502462684fe7e66c2bcad907c14c9d59
4
- data.tar.gz: 4cde5df3910792c24792b59aa02df037b1275541
3
+ metadata.gz: 4ce98efa46e27e9c8e289912b596535b739d919f
4
+ data.tar.gz: c0f52098326237086aa9562a785ebee36b590a94
5
5
  SHA512:
6
- metadata.gz: 0ab1b02c223b3a59b021f18e59f098ddc58371d7ee5c09494f027f31865addf4b8b1525f0775fd946a590388d62cb92af66bb2fd047f32c1cff129ca45101f1c
7
- data.tar.gz: 2faf7b37e2ed54b39f69b396eddc5a0614e7ec5b6f912d7307ad7ea4d9a67305005f04779fec3e7e884f4f800f35279aebaec3b28e1d12d12bc84151123cb831
6
+ metadata.gz: b1023cae31d7fabc151f84db3f2a796f2519134753554968a62fcb771f535d893147c9cc59c2f231dd6586a05d08e2ca9160d9363911ed1b7d248cdea68161d0
7
+ data.tar.gz: c80b5b7e097f41262ca7dfa3816f92d00272e01ab96ffb0a2a9ba3eccbd99b4013254306c8db1eb6f4d3eb53858cf14b146283ead04df0ce072b4c622d5c4e94
data/HISTORY.txt CHANGED
@@ -1,3 +1,7 @@
1
+ 1.0.3
2
+
3
+ Fix bugs preventing counter and set data types from working on models.
4
+
1
5
  1.0.2
2
6
 
3
7
  Set cluster logger on the Cassandra cluster if not already set.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.2
1
+ 1.0.3
data/lib/cassie/model.rb CHANGED
@@ -46,6 +46,7 @@ module Cassie::Model
46
46
  class_attribute :_columns, :instance_reader => false, :instance_writer => false
47
47
  class_attribute :_column_aliases, :instance_reader => false, :instance_writer => false
48
48
  class_attribute :_ordering_keys, :instance_reader => false, :instance_writer => false
49
+ class_attribute :_counter_table, :instance_reader => false, :instance_writer => false
49
50
  define_model_callbacks :create, :update, :save, :destroy
50
51
  self._columns = {}
51
52
  self._column_aliases = HashWithIndifferentAccess.new
@@ -66,6 +67,11 @@ module Cassie::Model
66
67
  # Defining a column will also define getter and setter methods for both the column name
67
68
  # and the alias name (if specified). So `column :i, :int, as: :id` will define the methods
68
69
  # `i`, `i=`, `id`, and `id=`.
70
+ #
71
+ # If you define a counter column then it will define methods for `increment_i!` and `decrement_i!`
72
+ # which take an optional amount argument. Note that if you have a counter column you cannot have
73
+ # any other non-primary key columns and you cannot call create, update, or save and must use the
74
+ # increment and decrement commands.
69
75
  def column(name, type, as: nil)
70
76
  name = name.to_sym
71
77
  type_class = nil
@@ -77,13 +83,33 @@ module Cassie::Model
77
83
 
78
84
  self._columns = _columns.merge(name => type_class)
79
85
  self._column_aliases = self._column_aliases.merge(name => name)
80
-
81
- define_method("#{name}="){ |value| instance_variable_set(:"@#{name}", self.class.send(:coerce, value, type_class)) }
82
- attr_reader name
83
- if as && as.to_s != name.to_s
86
+
87
+ aliased = (as && as.to_s != name.to_s)
88
+ if aliased
84
89
  self._column_aliases = self._column_aliases.merge(as => name)
85
- define_method(as){ send(name) }
86
- define_method("#{as}="){|value| send("#{name}=", value) }
90
+ end
91
+
92
+ if type.to_s == "counter".freeze
93
+ self._counter_table = true
94
+
95
+ define_method(name){ instance_variable_get(:"@#{name}") || 0 }
96
+ define_method("#{name}="){ |value| instance_variable_set(:"@#{name}", value.to_i) }
97
+
98
+ define_method("increment_#{name}!"){ |amount=1, ttl: nil| send(:adjust_counter!, name, amount, ttl: ttl) }
99
+ define_method("decrement_#{name}!"){ |amount=1, ttl: nil| send(:adjust_counter!, name, -amount, ttl: ttl) }
100
+ if aliased
101
+ define_method(as){ send(name) }
102
+ define_method("increment_#{as}!"){ |amount=1, ttl: nil| send("increment_#{name}!", amount, ttl: ttl) }
103
+ define_method("decrement_#{as}!"){ |amount=1, ttl: nil| send("increment_#{name}!", amount, ttl: ttl) }
104
+ end
105
+ else
106
+ attr_reader name
107
+ define_method("#{name}="){ |value| instance_variable_set(:"@#{name}", self.class.send(:coerce, value, type_class)) }
108
+ attr_reader name
109
+ if aliased
110
+ define_method(as){ send(name) }
111
+ define_method("#{as}="){|value| send("#{name}=", value) }
112
+ end
87
113
  end
88
114
  end
89
115
 
@@ -383,7 +409,7 @@ module Cassie::Model
383
409
  elsif type_class == Cassandra::Types::List
384
410
  Array.new(value)
385
411
  elsif type_class == Cassandra::Types::Set
386
- Array.new(value).to_set
412
+ Set.new(value)
387
413
  elsif type_class == Cassandra::Types::Map
388
414
  Hash[value]
389
415
  else
@@ -402,20 +428,26 @@ module Cassie::Model
402
428
  @persisted
403
429
  end
404
430
 
431
+ # Return true if the table is used for a counter.
432
+ def counter_table?
433
+ !!self.class._counter_table
434
+ end
435
+
405
436
  # Save a record. Returns true if the record was persisted and false if it was invalid.
406
437
  # This method will run the save callbacks as well as either the update or create
407
438
  # callbacks as necessary.
408
439
  def save(validate: true, ttl: nil)
440
+ raise ArgumentError.new("Cannot call save on a counter table") if counter_table?
409
441
  valid_record = (validate ? valid? : true)
410
442
  if valid_record
411
443
  run_callbacks(:save) do
412
444
  if persisted?
413
445
  run_callbacks(:update) do
414
- self.class.connection.update(self.class.full_table_name, values_hash, key_hash, :ttl => persistence_ttl || ttl)
446
+ self.class.connection.update(self.class.full_table_name, values_hash, key_hash, :ttl => (ttl || persistence_ttl))
415
447
  end
416
448
  else
417
449
  run_callbacks(:create) do
418
- self.class.connection.insert(self.class.full_table_name, attributes, :ttl => persistence_ttl || ttl)
450
+ self.class.connection.insert(self.class.full_table_name, attributes, :ttl => (ttl || persistence_ttl))
419
451
  @persisted = true
420
452
  end
421
453
  end
@@ -478,12 +510,26 @@ module Cassie::Model
478
510
 
479
511
  private
480
512
 
513
+ # Used for updating counter columns.
514
+ def adjust_counter!(name, amount, ttl: nil)
515
+ amount = amount.to_i
516
+ if amount != 0
517
+ run_callbacks(:update) do
518
+ adjustment = (amount < 0 ? "#{name} = #{name} - #{amount.abs}" : "#{name} = #{name} + #{amount}")
519
+ self.class.connection.update(self.class.full_table_name, adjustment, key_hash, :ttl => (ttl || persistence_ttl))
520
+ end
521
+ end
522
+ record = self.class.find(key_hash)
523
+ value = (record ? record.send(name) : send(name) + amount)
524
+ send("#{name}=", value)
525
+ end
526
+
481
527
  # Returns a hash of value except for the ones that constitute the primary key
482
528
  def values_hash
483
529
  pk = self.class.primary_key
484
530
  hash = {}
485
531
  self.class.column_names.each do |name|
486
- hash[name] = send(name) unless pk.include?(name)
532
+ hash[name] = send(name) unless pk.include?(name)
487
533
  end
488
534
  hash
489
535
  end
data/lib/cassie.rb CHANGED
@@ -219,9 +219,13 @@ class Cassie
219
219
  key_cql, key_values = key_clause(key_hash)
220
220
  update_cql = []
221
221
  update_values = []
222
- values_hash.each do |column, value|
223
- update_cql << "#{column} = ?"
224
- update_values << value
222
+ if values_hash.is_a?(String)
223
+ update_cql << values_hash
224
+ else
225
+ values_hash.each do |column, value|
226
+ update_cql << "#{column} = ?"
227
+ update_values << value
228
+ end
225
229
  end
226
230
  values = update_values + key_values
227
231
 
@@ -189,163 +189,318 @@ describe Cassie::Model do
189
189
  let(:model){ Cassie::TypeTester.new }
190
190
 
191
191
  it "should work with varchar columns" do
192
- model.varchar = "foo"
193
- model.varchar.should == "foo"
194
- model.varchar = nil
195
- model.varchar.should == nil
192
+ model.varchar_value = "foo"
193
+ model.varchar_value.should == "foo"
194
+ model.save
195
+ id = model.id
196
+ model = Cassie::TypeTester.find(:id => id)
197
+ model.varchar_value.should == "foo"
198
+
199
+ model.varchar_value = nil
200
+ model.varchar_value.should == nil
201
+ model.save
202
+ model = Cassie::TypeTester.find(:id => id)
203
+ model.varchar_value.should == nil
196
204
  end
197
205
 
198
206
  it "should work with ascii columns" do
199
- model.ascii = "foo"
200
- model.ascii.should == "foo"
201
- model.ascii = nil
202
- model.ascii.should == nil
207
+ model.ascii_value = "foo"
208
+ model.ascii_value.should == "foo"
209
+ model.save
210
+ id = model.id
211
+ model = Cassie::TypeTester.find(:id => id)
212
+ model.ascii_value.should == "foo"
213
+
214
+ model.ascii_value = nil
215
+ model.ascii_value.should == nil
216
+ model.save
217
+ model = Cassie::TypeTester.find(:id => id)
218
+ model.ascii_value.should == nil
203
219
  end
204
220
 
205
221
  it "should work with text columns" do
206
- model.text = "foo"
207
- model.text.should == "foo"
208
- model.text = nil
209
- model.text.should == nil
222
+ model.text_value = "foo"
223
+ model.text_value.should == "foo"
224
+ model.save
225
+ id = model.id
226
+ model = Cassie::TypeTester.find(:id => id)
227
+ model.text_value.should == "foo"
228
+
229
+ model.text_value = nil
230
+ model.text_value.should == nil
231
+ model.save
232
+ model = Cassie::TypeTester.find(:id => id)
233
+ model.text_value.should == nil
210
234
  end
211
235
 
212
236
  it "should work with blob columns" do
213
- model.blob = "foo"
214
- model.blob.should == "foo"
215
- model.blob = nil
216
- model.blob.should == nil
237
+ model.blob_value = "foo"
238
+ model.blob_value.should == "foo"
239
+ model.save
240
+ id = model.id
241
+ model = Cassie::TypeTester.find(:id => id)
242
+ model.blob_value.should == "foo"
243
+
244
+ model.blob_value = nil
245
+ model.blob_value.should == nil
246
+ model.save
247
+ model = Cassie::TypeTester.find(:id => id)
248
+ model.blob_value.should == nil
217
249
  end
218
250
 
219
251
  it "should work with int columns" do
220
- model.int = "1"
221
- model.int.should == 1
222
- model.int = 2
223
- model.int.should == 2
224
- model.int = nil
225
- model.int.should == nil
252
+ model.int_value = "1"
253
+ model.int_value.should == 1
254
+ model.int_value = 2
255
+ model.int_value.should == 2
256
+ model.save
257
+ id = model.id
258
+ model = Cassie::TypeTester.find(:id => id)
259
+ model.int_value.should == 2
260
+
261
+ model.int_value = nil
262
+ model.int_value.should == nil
263
+ model.save
264
+ model = Cassie::TypeTester.find(:id => id)
265
+ model.int_value.should == nil
226
266
  end
227
267
 
228
268
  it "should work with bigint columns" do
229
- model.bigint = "1"
230
- model.bigint.should == 1
231
- model.bigint = 2
232
- model.bigint.should == 2
233
- model.bigint = nil
234
- model.bigint.should == nil
269
+ model.bigint_value = "1"
270
+ model.bigint_value.should == 1
271
+ model.bigint_value = 2
272
+ model.bigint_value.should == 2
273
+ model.save
274
+ id = model.id
275
+ model = Cassie::TypeTester.find(:id => id)
276
+ model.bigint_value.should == 2
277
+
278
+ model.bigint_value = nil
279
+ model.bigint_value.should == nil
280
+ model.save
281
+ model = Cassie::TypeTester.find(:id => id)
282
+ model.bigint_value.should == nil
235
283
  end
236
284
 
237
285
  it "should work with varint columns" do
238
- model.varint = "1"
239
- model.varint.should == 1
240
- model.varint = 2
241
- model.varint.should == 2
242
- model.varint = nil
243
- model.varint.should == nil
244
- end
245
-
246
- it "should work with counter columns" do
247
- model.counter = "1"
248
- model.counter.should == 1
249
- model.counter = 2
250
- model.counter.should == 2
251
- model.counter = nil
252
- model.counter.should == nil
286
+ model.varint_value = "1"
287
+ model.varint_value.should == 1
288
+ model.varint_value = 2
289
+ model.varint_value.should == 2
290
+ model.save
291
+ id = model.id
292
+ model = Cassie::TypeTester.find(:id => id)
293
+ model.varint_value.should == 2
294
+
295
+ model.varint_value = nil
296
+ model.varint_value.should == nil
297
+ model.save
298
+ model = Cassie::TypeTester.find(:id => id)
299
+ model.varint_value.should == nil
253
300
  end
254
301
 
255
302
  it "should work with float columns" do
256
- model.float = "1.1"
257
- model.float.should == 1.1
258
- model.float = 2.2
259
- model.float.should == 2.2
260
- model.float = nil
261
- model.float.should == nil
303
+ model.float_value = "1.1"
304
+ model.float_value.should == 1.1
305
+ model.float_value = 2.2
306
+ model.float_value.should == 2.2
307
+ model.save
308
+ id = model.id
309
+ model = Cassie::TypeTester.find(:id => id)
310
+ model.float_value.round(4).should == 2.2
311
+
312
+ model.float_value = nil
313
+ model.float_value.should == nil
314
+ model.save
315
+ model = Cassie::TypeTester.find(:id => id)
316
+ model.float_value.should == nil
262
317
  end
263
318
 
264
319
  it "should work with double columns" do
265
- model.double = "1.1"
266
- model.double.should == 1.1
267
- model.double = 2.2
268
- model.double.should == 2.2
269
- model.double = nil
270
- model.double.should == nil
320
+ model.double_value = "1.1"
321
+ model.double_value.should == 1.1
322
+ model.double_value = 2.2
323
+ model.double_value.should == 2.2
324
+ model.save
325
+ id = model.id
326
+ model = Cassie::TypeTester.find(:id => id)
327
+ model.double_value.should == 2.2
328
+
329
+ model.double_value = nil
330
+ model.double_value.should == nil
331
+ model.save
332
+ model = Cassie::TypeTester.find(:id => id)
333
+ model.double_value.should == nil
271
334
  end
272
335
 
273
336
  it "should work with decimal columns" do
274
- model.decimal = "1.1"
275
- model.decimal.should == 1.1
276
- model.decimal.should be_a(BigDecimal)
277
- model.decimal = BigDecimal.new("3.3", 2)
278
- model.decimal.should == BigDecimal.new("3.3", 2)
279
- model.decimal = nil
280
- model.decimal.should == nil
337
+ model.decimal_value = "1.1"
338
+ model.decimal_value.should == 1.1
339
+ model.decimal_value.should be_a(BigDecimal)
340
+ model.decimal_value = BigDecimal.new("3.3", 2)
341
+ model.decimal_value.should == BigDecimal.new("3.3", 2)
342
+ model.save
343
+ id = model.id
344
+ model = Cassie::TypeTester.find(:id => id)
345
+ model.decimal_value.should == BigDecimal.new("3.3", 2)
346
+
347
+ model.decimal_value = nil
348
+ model.decimal_value.should == nil
349
+ model.save
350
+ model = Cassie::TypeTester.find(:id => id)
351
+ model.decimal_value.should == nil
281
352
  end
282
353
 
283
354
  it "should work with timestamp columns" do
284
- model.timestamp = "2015-04-23T15:23:30"
285
- model.timestamp.should == Time.new(2015, 4, 23, 15, 23, 30)
286
- model.timestamp = Time.new(2015, 4, 23, 15, 25, 30)
287
- model.timestamp.should == Time.new(2015, 4, 23, 15, 25, 30)
288
- model.timestamp = nil
289
- model.timestamp.should == nil
355
+ model.timestamp_value = "2015-04-23T15:23:30"
356
+ model.timestamp_value.should == Time.new(2015, 4, 23, 15, 23, 30)
357
+ model.timestamp_value = Time.new(2015, 4, 23, 15, 25, 30)
358
+ model.timestamp_value.should == Time.new(2015, 4, 23, 15, 25, 30)
359
+ model.save
360
+ id = model.id
361
+ model = Cassie::TypeTester.find(:id => id)
362
+ model.timestamp_value.should == Time.new(2015, 4, 23, 15, 25, 30)
363
+
364
+ model.timestamp_value = nil
365
+ model.timestamp_value.should == nil
366
+ model.save
367
+ model = Cassie::TypeTester.find(:id => id)
368
+ model.timestamp_value.should == nil
290
369
  end
291
370
 
292
371
  it "should work with boolean columns" do
293
- model.boolean = true
294
- model.boolean.should == true
295
- model.boolean = false
296
- model.boolean.should == false
297
- model.boolean = nil
298
- model.boolean.should == nil
372
+ model.boolean_value = true
373
+ model.boolean_value.should == true
374
+ model.boolean_value = false
375
+ model.boolean_value.should == false
376
+ model.save
377
+ id = model.id
378
+ model = Cassie::TypeTester.find(:id => id)
379
+ model.boolean_value.should == false
380
+
381
+ model.boolean_value = nil
382
+ model.boolean_value.should == nil
383
+ model.save
384
+ model = Cassie::TypeTester.find(:id => id)
385
+ model.boolean_value.should == nil
299
386
  end
300
387
 
301
388
  it "should work with inet columns" do
302
- model.inet = "127.0.0.1"
303
- model.inet.should == IPAddr.new("127.0.0.1")
304
- model.inet = IPAddr.new("10.1.0.1")
305
- model.inet.should == IPAddr.new("10.1.0.1")
306
- model.inet = nil
307
- model.inet.should == nil
389
+ model.inet_value = "127.0.0.1"
390
+ model.inet_value.should == IPAddr.new("127.0.0.1")
391
+ model.inet_value = IPAddr.new("10.1.0.1")
392
+ model.inet_value.should == IPAddr.new("10.1.0.1")
393
+ model.save
394
+ id = model.id
395
+ model = Cassie::TypeTester.find(:id => id)
396
+ model.inet_value.should == IPAddr.new("10.1.0.1")
397
+
398
+ model.inet_value = nil
399
+ model.inet_value.should == nil
400
+ model.save
401
+ model = Cassie::TypeTester.find(:id => id)
402
+ model.inet_value.should == nil
308
403
  end
309
404
 
310
405
  it "should work with uuid columns" do
311
- model.uuid = "eed6d678-ea0b-11e4-8772-793f91a64daf"
312
- model.uuid.should == Cassandra::Uuid.new("eed6d678-ea0b-11e4-8772-793f91a64daf")
313
- model.uuid = Cassandra::Uuid.new("fed6d678-ea0b-11e4-8772-793f91a64daf")
314
- model.uuid.should == Cassandra::Uuid.new("fed6d678-ea0b-11e4-8772-793f91a64daf")
315
- model.uuid = nil
316
- model.uuid.should == nil
406
+ model.uuid_value = "eed6d678-ea0b-11e4-8772-793f91a64daf"
407
+ model.uuid_value.should == Cassandra::Uuid.new("eed6d678-ea0b-11e4-8772-793f91a64daf")
408
+ model.uuid_value = Cassandra::Uuid.new("fed6d678-ea0b-11e4-8772-793f91a64daf")
409
+ model.uuid_value.should == Cassandra::Uuid.new("fed6d678-ea0b-11e4-8772-793f91a64daf")
410
+ model.save
411
+ id = model.id
412
+ model = Cassie::TypeTester.find(:id => id)
413
+ model.uuid_value.should == Cassandra::Uuid.new("fed6d678-ea0b-11e4-8772-793f91a64daf")
414
+
415
+ model.uuid_value = nil
416
+ model.uuid_value.should == nil
417
+ model.save
418
+ model = Cassie::TypeTester.find(:id => id)
419
+ model.uuid_value.should == nil
317
420
  end
318
421
 
319
422
  it "should work with timeuuid columns" do
320
- model.timeuuid = "eed6d678-ea0b-11e4-8772-793f91a64daf"
321
- model.timeuuid.should == Cassandra::TimeUuid.new("eed6d678-ea0b-11e4-8772-793f91a64daf")
322
- model.timeuuid = Cassandra::TimeUuid.new("fed6d678-ea0b-11e4-8772-793f91a64daf")
323
- model.timeuuid.should == Cassandra::TimeUuid.new("fed6d678-ea0b-11e4-8772-793f91a64daf")
324
- model.timeuuid = nil
325
- model.timeuuid.should == nil
423
+ model.timeuuid_value = "eed6d678-ea0b-11e4-8772-793f91a64daf"
424
+ model.timeuuid_value.should == Cassandra::TimeUuid.new("eed6d678-ea0b-11e4-8772-793f91a64daf")
425
+ model.timeuuid_value = Cassandra::TimeUuid.new("fed6d678-ea0b-11e4-8772-793f91a64daf")
426
+ model.timeuuid_value.should == Cassandra::TimeUuid.new("fed6d678-ea0b-11e4-8772-793f91a64daf")
427
+ model.save
428
+ id = model.id
429
+ model = Cassie::TypeTester.find(:id => id)
430
+ model.timeuuid_value.should == Cassandra::TimeUuid.new("fed6d678-ea0b-11e4-8772-793f91a64daf")
431
+
432
+ model.timeuuid_value = nil
433
+ model.timeuuid_value.should == nil
434
+ model.save
435
+ model = Cassie::TypeTester.find(:id => id)
436
+ model.timeuuid_value.should == nil
326
437
  end
327
438
 
328
439
  it "should work with list columns" do
329
- model.list = ["a", "b", "c"]
330
- model.list.should == ["a", "b", "c"]
331
- model.list = nil
332
- model.list.should == nil
440
+ model.list_value = ["a", "b", "c"]
441
+ model.list_value.should == ["a", "b", "c"]
442
+ model.save
443
+ id = model.id
444
+ model = Cassie::TypeTester.find(:id => id)
445
+ model.list_value.should == ["a", "b", "c"]
446
+
447
+ model.list_value = nil
448
+ model.list_value.should == nil
449
+ model.save
450
+ model = Cassie::TypeTester.find(:id => id)
451
+ model.list_value.should == nil
333
452
  end
334
453
 
335
454
  it "should work with set columns" do
336
- model.set = ["a", "b", "c", "a"]
337
- model.set.should == ["a", "b", "c"].to_set
338
- model.set = nil
339
- model.set.should == nil
455
+ model.set_value = ["a", "b", "c", "a"]
456
+ model.set_value.should == ["a", "b", "c"].to_set
457
+ model.save
458
+ id = model.id
459
+ model = Cassie::TypeTester.find(:id => id)
460
+ model.set_value.should == ["a", "b", "c"].to_set
461
+
462
+ model.set_value = nil
463
+ model.set_value.should == nil
464
+ model.save
465
+ model = Cassie::TypeTester.find(:id => id)
466
+ model.set_value.should == nil
340
467
  end
341
468
 
342
469
  it "should work with map columns" do
343
- model.map = [["a", "b"], ["c", "d"]]
344
- model.map.should == {"a" => "b", "c" => "d"}
345
- model.map = {"e" => "f", "g" => "h"}
346
- model.map.should == {"e" => "f", "g" => "h"}
347
- model.map = nil
348
- model.map.should == nil
470
+ model.map_value = [["a", "b"], ["c", "d"]]
471
+ model.map_value.should == {"a" => "b", "c" => "d"}
472
+ model.map_value = {"e" => "f", "g" => "h"}
473
+ model.map_value.should == {"e" => "f", "g" => "h"}
474
+ model.save
475
+ id = model.id
476
+ model = Cassie::TypeTester.find(:id => id)
477
+ model.map_value.should == {"e" => "f", "g" => "h"}
478
+
479
+ model.map_value = nil
480
+ model.map_value.should == nil
481
+ model.save
482
+ model = Cassie::TypeTester.find(:id => id)
483
+ model.map_value.should == nil
484
+ end
485
+
486
+ it "should work with counter columns" do
487
+ id = SecureRandom.uuid
488
+ model = Cassie::TypeTesterCounter.new(:id => id)
489
+ model.counter_value.should == 0
490
+ model.increment_counter_value!
491
+ model.counter_value.should == 1
492
+ model = Cassie::TypeTesterCounter.find(:id => id)
493
+ model.counter_value.should == 1
494
+
495
+ model.increment_counter_value!
496
+ model.counter_value.should == 2
497
+ model = Cassie::TypeTesterCounter.find(:id => id)
498
+ model.counter_value.should == 2
499
+
500
+ model.decrement_counter_value!
501
+ model.counter_value.should == 1
502
+ model = Cassie::TypeTesterCounter.find(:id => id)
503
+ model.counter_value.should == 1
349
504
  end
350
505
  end
351
506
  end
@@ -1,23 +1,42 @@
1
+ require 'securerandom'
2
+
1
3
  class Cassie::TypeTester
2
4
  include Cassie::Model
3
5
 
4
- column :int, :int
5
- column :varint, :varint
6
- column :bigint, :bigint
7
- column :float, :float
8
- column :double, :double
9
- column :decimal, :decimal
10
- column :ascii, :ascii
11
- column :varchar, :varchar
12
- column :text, :text
13
- column :blob, :blob
14
- column :boolean, :boolean
15
- column :timestamp, :timestamp
16
- column :counter, :counter
17
- column :uuid, :uuid
18
- column :timeuuid, :timeuuid
19
- column :inet, :inet
20
- column :list, :list
21
- column :set, :set
22
- column :map, :map
6
+ self.table_name = "type_testers"
7
+ self.keyspace = "test"
8
+ self.primary_key = [:id]
9
+
10
+ column :id, :varchar
11
+ column :int_value, :int
12
+ column :varint_value, :varint
13
+ column :bigint_value, :bigint
14
+ column :float_value, :float
15
+ column :double_value, :double
16
+ column :decimal_value, :decimal
17
+ column :ascii_value, :ascii
18
+ column :varchar_value, :varchar
19
+ column :text_value, :text
20
+ column :blob_value, :blob
21
+ column :boolean_value, :boolean
22
+ column :timestamp_value, :timestamp
23
+ column :uuid_value, :uuid
24
+ column :timeuuid_value, :timeuuid
25
+ column :inet_value, :inet
26
+ column :list_value, :list
27
+ column :set_value, :set
28
+ column :map_value, :map
29
+
30
+ before_create{ self.id = SecureRandom.uuid }
31
+ end
32
+
33
+ class Cassie::TypeTesterCounter
34
+ include Cassie::Model
35
+
36
+ self.table_name = "type_tester_counters"
37
+ self.keyspace = "test"
38
+ self.primary_key = [:id]
39
+
40
+ column :id, :varchar
41
+ column :counter_value, :counter, as: :counter_column
23
42
  end
data/spec/schema/test.cql CHANGED
@@ -3,4 +3,33 @@ CREATE TABLE things (
3
3
  id INT,
4
4
  val VARCHAR,
5
5
  PRIMARY KEY (owner, id)
6
- ) WITH CLUSTERING ORDER BY (id DESC)
6
+ ) WITH CLUSTERING ORDER BY (id DESC);
7
+
8
+ CREATE TABLE type_testers (
9
+ id VARCHAR,
10
+ int_value INT,
11
+ varint_value VARINT,
12
+ bigint_value BIGINT,
13
+ float_value FLOAT,
14
+ double_value DOUBLE,
15
+ decimal_value DECIMAL,
16
+ ascii_value ASCII,
17
+ varchar_value VARCHAR,
18
+ text_value TEXT,
19
+ blob_value BLOB,
20
+ boolean_value BOOLEAN,
21
+ timestamp_value TIMESTAMP,
22
+ uuid_value UUID,
23
+ timeuuid_value TIMEUUID,
24
+ inet_value INET,
25
+ list_value LIST<VARCHAR>,
26
+ set_value SET<VARCHAR>,
27
+ map_value MAP<VARCHAR, VARCHAR>,
28
+ PRIMARY KEY (id)
29
+ );
30
+
31
+ CREATE TABLE type_tester_counters (
32
+ id VARCHAR,
33
+ counter_value COUNTER,
34
+ PRIMARY KEY (id)
35
+ );
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: whi-cassie
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - We Heart It
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-02-25 00:00:00.000000000 Z
12
+ date: 2016-03-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: cassandra-driver