believer 0.2.13 → 0.2.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -69,15 +69,20 @@ class AlbumSales < Believer::Base
69
69
  primary_key :artist_name, :name
70
70
  end
71
71
 
72
- album_sales = AlbumSales.new
72
+ album_sales = AlbumSales.new(
73
73
  album_sales.sales # Returns a Believer::Counter, with a value of 0
74
74
  album_sales.sales.incr # Increment counter by 1, value is 1
75
75
  album_sales.sales.incr(6) # Increment counter by 3, value is 7
76
76
  album_sales.sales.decr # Decrement counter by 1, value is 6
77
77
  album_sales.sales.decr(3) # Decrement counter by 3, value is 3
78
- album_sales.sales.reset! # Reset it to the initial value, which is 0
78
+ album_sales.sales.undo_changes! # Reset it to the initial value, which is 0
79
79
  album_sales.sales = 4 # Explicitly set the value...
80
80
  album_sales.sales.to_i # ...which is now 4
81
+ album_sales.save
82
+
83
+ album_sales.sales.reset! # Reset the counter value to 0
84
+ album_sales.save
85
+ album_sales.sales == 0 # True
81
86
  ```
82
87
 
83
88
  #### The primary_key class method
@@ -328,5 +333,6 @@ To make this a little less labor intensive, you can include the module Believer:
328
333
  This module will implement an after(:each) hook, which deletes all Believer::Base instance/records created in the span
329
334
  of the test.
330
335
 
336
+
331
337
  ## Class documentation
332
338
  For API go [here](http://rubydoc.info/gems/believer/frames).
data/lib/believer.rb CHANGED
@@ -37,6 +37,7 @@ require 'believer/environment/rails_env'
37
37
  require 'believer/environment/merb_env'
38
38
  require 'believer/environment'
39
39
  require 'believer/connection'
40
+ require 'believer/key_space'
40
41
  require 'believer/counter'
41
42
  require 'believer/values'
42
43
  require 'believer/column'
@@ -48,6 +49,7 @@ require 'believer/where_clause'
48
49
  require 'believer/limit'
49
50
  require 'believer/order_by'
50
51
  require 'believer/filter_command'
52
+ require 'believer/act_as_enumerable'
51
53
  require 'believer/query'
52
54
  require 'believer/empty_result'
53
55
  require 'believer/delete'
@@ -0,0 +1,50 @@
1
+ module Believer
2
+ module ActAsEnumerable
3
+
4
+ def self.included(base)
5
+ base.delegate *(Enumerable.instance_methods(false).map {|enum_method| enum_method.to_sym}), :to => :to_a
6
+ base.delegate :each, :size, :[], :to => :to_a
7
+ end
8
+
9
+ def count
10
+ return loaded_objects.size unless loaded_objects.nil?
11
+
12
+ count_q = clone
13
+ count_q.selects = ['COUNT(*)']
14
+ result = count_q.execute
15
+
16
+ cnt = -1
17
+ result.each do |row|
18
+ cnt = row['count']
19
+ end
20
+ cnt
21
+ end
22
+
23
+ # Tests if there are any records present which conform to the argument filter(s)
24
+ # @param args [Object] a filter condition. This argument has the same usage as the where method
25
+ def exists?(*args)
26
+ return count > 0 if args.nil? || args.size == 0
27
+ where(*args).exists?
28
+ end
29
+
30
+ def first
31
+ return loaded_objects.first unless loaded_objects.nil?
32
+ clone.limit(1)[0]
33
+ end
34
+
35
+ def last
36
+ return loaded_objects.last unless loaded_objects.nil?
37
+ raise "Cannot retrieve last if no order column is set" if @order_by.nil?
38
+ lq = clone.limit(1)
39
+ lq.order_by = @order_by.inverse
40
+ lq[0]
41
+ end
42
+
43
+ def any?
44
+ return loaded_objects.any? unless loaded_objects.nil?
45
+ q = clone
46
+ q.limit(1)
47
+ q.count > 0
48
+ end
49
+ end
50
+ end
@@ -8,6 +8,11 @@ module Believer
8
8
  end
9
9
 
10
10
  def reset!
11
+ @value = 0
12
+ self
13
+ end
14
+
15
+ def undo_changes!
11
16
  @value = initial_value
12
17
  self
13
18
  end
@@ -38,5 +38,24 @@ module Believer
38
38
  return nil
39
39
  end
40
40
 
41
+ def to_cql_properties(properties)
42
+ return '' if properties.nil? || properties.empty?
43
+
44
+ props_s = properties.keys.map { |k|
45
+ v = properties[k]
46
+ v_s = nil
47
+ if v.is_a?(Hash)
48
+ v_s = v.to_json.gsub(/\"/) { |m| "'" }
49
+ elsif v.is_a?(String)
50
+ v_s = "'#{v}'"
51
+ else
52
+ v_s = v.to_s
53
+ end
54
+ "#{k} = #{v_s}"
55
+ }.join("\nAND ")
56
+
57
+ props_s
58
+ end
59
+
41
60
  end
42
61
  end
@@ -1,5 +1,8 @@
1
1
  module Believer
2
2
  class CreateTable < Command
3
+ include CqlHelper
4
+
5
+ attr_accessor :table_properties
3
6
 
4
7
  def to_cql
5
8
  keys = []
@@ -17,6 +20,11 @@ module Believer
17
20
  s << ",\n"
18
21
  s << "PRIMARY KEY (#{keys.join(',')})"
19
22
  s << "\n)"
23
+
24
+ unless table_properties.nil? || table_properties.empty?
25
+ s << " WITH #{to_cql_properties(table_properties)}"
26
+ end
27
+
20
28
  s
21
29
  end
22
30
 
data/lib/believer/ddl.rb CHANGED
@@ -9,8 +9,8 @@ module Believer
9
9
  ::Believer::DropTable.new(:record_class => self).execute
10
10
  end
11
11
 
12
- def create_table
13
- ::Believer::CreateTable.new(:record_class => self).execute
12
+ def create_table(properties = {})
13
+ ::Believer::CreateTable.new(:record_class => self, :table_properties => properties).execute
14
14
  end
15
15
 
16
16
  end
@@ -78,38 +78,6 @@ module Believer
78
78
  connection
79
79
  end
80
80
 
81
- def drop_keyspace
82
- conn = create_connection(:connect_to_keyspace => false)
83
- conn.execute("DROP KEYSPACE #{connection_configuration[:keyspace]}")
84
- end
85
-
86
- def create_keyspace(properties = {}, connection = nil)
87
- conn = connection || create_connection(:connect_to_keyspace => false)
88
-
89
- default_properties = {:replication => {:class => 'SimpleStrategy', :replication_factor => 1}}
90
- ks_props = default_properties.merge(properties)
91
-
92
- ks_props_s = ks_props.keys.map { |k|
93
- v = ks_props[k]
94
- v_s = nil
95
- if v.is_a?(Hash)
96
- v_s = v.to_json.gsub(/\"/) { |m| "'" }
97
- elsif v.is_a?(String)
98
- v_s = "'#{v}'"
99
- else
100
- v_s = v.to_s
101
- end
102
- "#{k} = #{v_s}"
103
- }.join("\nAND ")
104
-
105
- ks_def = <<-KS_DEF
106
- CREATE KEYSPACE #{connection_configuration[:keyspace]}
107
- WITH #{ks_props_s}
108
- KS_DEF
109
-
110
- conn.execute(ks_def)
111
- end
112
-
113
81
  end
114
82
 
115
83
  end
@@ -0,0 +1,30 @@
1
+ module Believer
2
+ class KeySpace
3
+ include CqlHelper
4
+
5
+ DEFAULT_PROPERTIES = {:replication => {:class => 'SimpleStrategy', :replication_factor => 1}}
6
+
7
+ def initialize(environment)
8
+ @environment = environment
9
+ end
10
+
11
+ def name
12
+ @environment.connection_configuration[:keyspace]
13
+ end
14
+
15
+ def drop
16
+ connection = @environment.create_connection(:connect_to_keyspace => false)
17
+ connection.execute("DROP KEYSPACE #{name}")
18
+ connection.close
19
+ end
20
+
21
+ def create(properties = {})
22
+ conn = @environment.create_connection(:connect_to_keyspace => false)
23
+ ks_props = DEFAULT_PROPERTIES.merge(properties)
24
+ ks_props_s = to_cql_properties(ks_props)
25
+ ks_def = "CREATE KEYSPACE #{name} WITH #{ks_props_s}"
26
+ conn.execute(ks_def)
27
+ end
28
+
29
+ end
30
+ end
@@ -3,14 +3,13 @@ require 'believer/extending'
3
3
  module Believer
4
4
  class Query < FilterCommand
5
5
  include Extending
6
+ include ActAsEnumerable
6
7
 
7
8
  DEFAULT_FILTER_LIMIT = 10000
8
9
 
9
10
  attr_accessor :record_class, :selects, :order_by, :limit_to, :filtering_allowed
10
11
  alias :filtering_allowed? :filtering_allowed
11
12
 
12
- delegate *(Enumerable.instance_methods(false).map {|enum_method| enum_method.to_sym}), :to => :to_a
13
- delegate :each, :size, :[], :to => :to_a
14
13
  delegate :primary_key_columns, :to => :record_class
15
14
 
16
15
  def initialize(attrs)
@@ -147,39 +146,6 @@ module Believer
147
146
  @loaded_objects
148
147
  end
149
148
 
150
- def count
151
- return @loaded_objects.size unless @loaded_objects.nil?
152
-
153
- count_q = clone
154
- count_q.selects = ['COUNT(*)']
155
- result = count_q.execute
156
-
157
- cnt = -1
158
- result.each do |row|
159
- cnt = row['count']
160
- end
161
- cnt
162
- end
163
-
164
- # Tests if there are any records present which conform to the argument filter(s)
165
- # @param args [Object] a filter condition. This argument has the same usage as the where method
166
- def exists?(*args)
167
- return count > 0 if args.nil? || args.size == 0
168
- where(*args).exists?
169
- end
170
-
171
- def first
172
- return @loaded_objects.first unless @loaded_objects.nil?
173
- clone.limit(1)[0]
174
- end
175
-
176
- def last
177
- return @loaded_objects.last unless @loaded_objects.nil?
178
- raise "Cannot retrieve last if no order column is set" if @order_by.nil?
179
- lq = clone.limit(1)
180
- lq.order_by = @order_by.inverse
181
- lq[0]
182
- end
183
149
 
184
150
  protected
185
151
  def loaded_objects
@@ -11,14 +11,15 @@ module Believer
11
11
 
12
12
  connection = env.create_connection(:connect_to_keyspace => false)
13
13
 
14
+ keyspace = ::Believer::KeySpace.new(env)
14
15
  # First delete the existing keyspace
15
16
  begin
16
17
  puts "Dropping keyspace"
17
- env.drop_keyspace
18
+ keyspace.drop
18
19
  rescue Cql::QueryError
19
20
  end
20
21
 
21
- env.create_keyspace({}, connection)
22
+ keyspace.create({})
22
23
  connection.use(env.connection_configuration[:keyspace])
23
24
 
24
25
  classes = options[:classes]
@@ -1,5 +1,5 @@
1
1
  module Believer
2
2
  module Version
3
- VERSION = '0.2.13'
3
+ VERSION = '0.2.14'
4
4
  end
5
5
  end
@@ -20,12 +20,42 @@ describe Believer::Counting do
20
20
  expect(album_sales.sales.to_i).to eql 2
21
21
  end
22
22
 
23
- it 'should be able to reset a counter' do
23
+ it 'should be able to undo changes in a counter' do
24
24
  album_sales = Test::AlbumSales.new
25
25
  album_sales.sales.incr
26
26
  expect(album_sales.has_counter_diffs?).to eql true
27
- album_sales.sales.reset!
27
+ album_sales.sales.undo_changes!
28
28
  expect(album_sales.has_counter_diffs?).to eql false
29
29
  end
30
30
 
31
+ it 'should be able to reset a counter' do
32
+ album_attrs = {:artist_name => 'CSNY', :name => 'Deja vu'}
33
+ album_sales = Test::AlbumSales.new(album_attrs)
34
+ album_sales.sales.incr(4)
35
+ expect(album_sales.has_counter_diffs?).to eql true
36
+ album_sales.save
37
+
38
+ album_sales = Test::AlbumSales.where(album_attrs).first
39
+ expect(album_sales.sales.value).to eql 4
40
+ album_sales.sales.reset!
41
+ album_sales.save
42
+
43
+ album_sales = Test::AlbumSales.where(album_attrs).first
44
+ expect(album_sales.sales.value).to eql 0
45
+ end
46
+
47
+ it 'should be able to persist a counter value' do
48
+ album_sales = Test::AlbumSales.new(:artist_name => 'CSNY', :name => 'Deja vu')
49
+ album_sales.sales.incr
50
+ album_sales.save
51
+ expect(album_sales.sales.value).to eql 1
52
+
53
+ album_sales = Test::AlbumSales.where(:artist_name => 'CSNY', :name => 'Deja vu').first
54
+ album_sales.sales.incr
55
+ album_sales.save
56
+
57
+ album_sales = Test::AlbumSales.where(:artist_name => 'CSNY', :name => 'Deja vu').first
58
+ expect(album_sales.sales.value).to eql 2
59
+ end
60
+
31
61
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: believer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.13
4
+ version: 0.2.14
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-11-12 00:00:00.000000000 Z
12
+ date: 2013-11-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -130,6 +130,7 @@ executables: []
130
130
  extensions: []
131
131
  extra_rdoc_files: []
132
132
  files:
133
+ - lib/believer/act_as_enumerable.rb
133
134
  - lib/believer/base.rb
134
135
  - lib/believer/batch.rb
135
136
  - lib/believer/batch_delete.rb
@@ -154,6 +155,7 @@ files:
154
155
  - lib/believer/filter_command.rb
155
156
  - lib/believer/finder_methods.rb
156
157
  - lib/believer/insert.rb
158
+ - lib/believer/key_space.rb
157
159
  - lib/believer/limit.rb
158
160
  - lib/believer/log_subscriber.rb
159
161
  - lib/believer/model_schema.rb