gotime-cassandra_object 4.10.4 → 4.10.5

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: 721e97034ca554b1891149cefcf4ced8a268494c
4
- data.tar.gz: a0681f45de661d989e8dba2744ef3a57fd079746
3
+ metadata.gz: 7065981f0726fd662b2d6a42d8b2c8938e56b974
4
+ data.tar.gz: cbd7d2ba31303203624b2bce28c5fc3b61737d38
5
5
  SHA512:
6
- metadata.gz: 59686c33844856c912b04a222c0b075d72dbc49112c1e7c2ae9efc65342183b014a63a0f53d3c5624fff58dd908213faac2ac6a1fb66252c2550187addcd8e02
7
- data.tar.gz: f7d0975b82c5f8d247aa575bf821eddefa9696c7e998ab4cb450255367474caf91df3b2adc74ab0552725900c162131adfd3a59207d9ed6988dbf2a669a4ac6f
6
+ metadata.gz: 32474823df1bbb7ffe2b750896c88174b28310fe1f26212d848fabbb29a267215e41c2367cde0f0706fb748362730a1d806d3ae5d9cb76f227f9aebca1c28b62
7
+ data.tar.gz: 735e41f64fc8e187f047ab3ceef93fb607a1db4b4e4fd1bccd0bd11e1b2c0db487a1b413f82bbf146a7be1dcc6e63a40c6eb05f3ddf543142c59f43aa43c431f
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'gotime-cassandra_object'
5
- s.version = '4.10.4'
5
+ s.version = '4.10.5'
6
6
  s.description = 'Cassandra ActiveModel'
7
7
  s.summary = 'Cassandra ActiveModel'
8
8
  s.authors = ["Michael Koziarski", "gotime"]
@@ -5,41 +5,6 @@ require 'cassandra_object/types'
5
5
 
6
6
  module CassandraObject
7
7
  class Base
8
- class << self
9
- def column_family=(column_family)
10
- @column_family = column_family
11
- end
12
-
13
- def column_family
14
- @column_family ||= base_class.name.pluralize
15
- end
16
-
17
- def base_class
18
- class_of_active_record_descendant(self)
19
- end
20
-
21
- def config=(config)
22
- @@config = config.is_a?(Hash) ? CassandraObject::Config.new(config) : config
23
- end
24
-
25
- def config
26
- @@config
27
- end
28
-
29
- private
30
-
31
- # Returns the class descending directly from ActiveRecord::Base or an
32
- # abstract class, if any, in the inheritance hierarchy.
33
- def class_of_active_record_descendant(klass)
34
- if klass == Base || klass.superclass == Base
35
- klass
36
- elsif klass.superclass.nil?
37
- raise "#{name} doesn't belong in a hierarchy descending from CassandraObject"
38
- else
39
- class_of_active_record_descendant(klass.superclass)
40
- end
41
- end
42
- end
43
8
 
44
9
  extend ActiveModel::Naming
45
10
  include ActiveModel::Conversion
@@ -61,6 +26,7 @@ module CassandraObject
61
26
  include Savepoints
62
27
  include Scoping
63
28
  include Core
29
+ extend Model
64
30
 
65
31
  include Serialization
66
32
  end
@@ -0,0 +1,73 @@
1
+ module CassandraObject
2
+ class CounterBase
3
+ class << self
4
+ def base_class
5
+ self
6
+ end
7
+
8
+ def config
9
+ Base.config
10
+ end
11
+
12
+ def get(group, counter=nil)
13
+ cql_string = "SELECT #{select_counter(counter)} FROM #{column_family} WHERE KEY = ?" # Multiple keys at once doesn't result right
14
+
15
+ results = []
16
+ attributes = execute_cql(cql_string, group).fetch_hash
17
+ attributes.delete("KEY")
18
+ results << attributes
19
+
20
+ if !group.is_a?(Array) && results.size <= 1
21
+ results = results.first
22
+ if counter && !counter.is_a?(Array) && results.size <= 1
23
+ results = results.values.first
24
+ end
25
+ end
26
+ results
27
+ end
28
+
29
+ def update(group, counter, count=nil)
30
+ execute_cql "UPDATE #{column_family} SET #{counter_updates(counter, count)} WHERE KEY = '#{group}'"
31
+ end
32
+
33
+ private
34
+
35
+ def counter_updates(counter, count=nil)
36
+ if !count.nil?
37
+ counters = if counter.is_a?(Array); counter else [counter] end
38
+ counters.map do |c| "'#{c}' = '#{c}' #{incr_decr(count)}" end.join ', '
39
+ else
40
+ counter.map do |c, count| "'#{c}' = '#{c}' #{incr_decr(count)}" end.join ', '
41
+ end
42
+ end
43
+
44
+ def incr_decr(count)
45
+ if count >= 0
46
+ "+ #{count}"
47
+ else
48
+ "- #{count * -1}"
49
+ end
50
+ end
51
+
52
+ def select_counter(counter)
53
+ if counter.nil?
54
+ "*"
55
+ else
56
+ counter.is_a?(Array) ? counter.map do |c| "'#{c}'" end.join(",") : "'#{counter}'"
57
+ end
58
+ end
59
+
60
+ end
61
+
62
+ extend ActiveModel::Naming
63
+ include ActiveModel::Conversion
64
+ extend ActiveSupport::DescendantsTracker
65
+
66
+ include Connection
67
+ include Consistency
68
+ include Identity
69
+ include Inspect
70
+ extend Model
71
+
72
+ end
73
+ end
@@ -0,0 +1,37 @@
1
+ module CassandraObject
2
+ module Model
3
+ def column_family=(column_family)
4
+ @column_family = column_family
5
+ end
6
+
7
+ def column_family
8
+ @column_family ||= base_class.name.pluralize
9
+ end
10
+
11
+ def base_class
12
+ class_of_active_record_descendant(self)
13
+ end
14
+
15
+ def config=(config)
16
+ @@config = config.is_a?(Hash) ? CassandraObject::Config.new(config) : config
17
+ end
18
+
19
+ def config
20
+ @@config
21
+ end
22
+
23
+ private
24
+
25
+ # Returns the class descending directly from ActiveRecord::Base or an
26
+ # abstract class, if any, in the inheritance hierarchy.
27
+ def class_of_active_record_descendant(klass)
28
+ if klass == Base || klass.superclass == Base
29
+ klass
30
+ elsif klass.superclass.nil?
31
+ raise "#{name} doesn't belong in a hierarchy descending from CassandraObject"
32
+ else
33
+ class_of_active_record_descendant(klass.superclass)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -14,8 +14,10 @@ module CassandraObject
14
14
  autoload :Connection
15
15
  autoload :Consistency
16
16
  autoload :Core
17
+ autoload :CounterBase
17
18
  autoload :Identity
18
19
  autoload :Inspect
20
+ autoload :Model
19
21
  autoload :Persistence
20
22
  autoload :Savepoints
21
23
  autoload :Schema
@@ -0,0 +1,43 @@
1
+ require 'test_helper'
2
+
3
+ class CassandraObject::CounterBaseTest < CassandraObject::TestCase
4
+ class AppCounts < CassandraObject::CounterBase
5
+ self.column_family = 'AppCounts'
6
+ end
7
+
8
+ def setup
9
+ CassandraObject::Schema.create_column_family 'AppCounts', 'default_validation' => 'CounterColumnType', 'replicate_on_write' => 'true'
10
+ end
11
+
12
+ def teardown
13
+ CassandraObject::Schema.drop_column_family 'AppCounts'
14
+ end
15
+
16
+ test 'class_loading' do
17
+ assert_equal CassandraObject::CounterBase, CassandraObject::CounterBase
18
+ end
19
+
20
+ test 'single update' do
21
+
22
+ AppCounts.update('poop', 'smells', 0)
23
+
24
+ assert_equal 0, AppCounts.get("poop", "smells")
25
+
26
+ AppCounts.update('poop', 'smells', 60)
27
+
28
+ assert_equal 60, AppCounts.get("poop", "smells")
29
+
30
+ assert_equal Hash['smells' => 60], AppCounts.get("poop")
31
+
32
+ end
33
+
34
+ test 'multiple update' do
35
+
36
+ AppCounts.update('poop', {'hankey_sightings' => 3, 'christmas_days' => 5})
37
+
38
+ assert_equal Hash['hankey_sightings' => 3, 'christmas_days' => 5], AppCounts.get("poop")
39
+
40
+ assert_equal 5, AppCounts.get("poop", "christmas_days")
41
+ end
42
+
43
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gotime-cassandra_object
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.10.4
4
+ version: 4.10.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Koziarski
@@ -98,10 +98,12 @@ files:
98
98
  - lib/cassandra_object/connection.rb
99
99
  - lib/cassandra_object/consistency.rb
100
100
  - lib/cassandra_object/core.rb
101
+ - lib/cassandra_object/counter_base.rb
101
102
  - lib/cassandra_object/errors.rb
102
103
  - lib/cassandra_object/identity.rb
103
104
  - lib/cassandra_object/inspect.rb
104
105
  - lib/cassandra_object/log_subscriber.rb
106
+ - lib/cassandra_object/model.rb
105
107
  - lib/cassandra_object/persistence.rb
106
108
  - lib/cassandra_object/railtie.rb
107
109
  - lib/cassandra_object/savepoints.rb
@@ -147,6 +149,7 @@ files:
147
149
  - test/unit/connection_test.rb
148
150
  - test/unit/consistency_test.rb
149
151
  - test/unit/core_test.rb
152
+ - test/unit/counter_base_test.rb
150
153
  - test/unit/identity_test.rb
151
154
  - test/unit/inspect_test.rb
152
155
  - test/unit/log_subscriber_test.rb