believer 0.2.3 → 0.2.4
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 +33 -0
- data/lib/believer.rb +5 -3
- data/lib/believer/base.rb +4 -0
- data/lib/believer/command.rb +13 -6
- data/lib/believer/connection.rb +6 -0
- data/lib/believer/ddl.rb +11 -10
- data/lib/believer/empty_result.rb +57 -26
- data/lib/believer/environment.rb +7 -84
- data/lib/believer/environment/base_env.rb +113 -0
- data/lib/believer/environment/merb_env.rb +4 -0
- data/lib/believer/environment/rails_env.rb +13 -2
- data/lib/believer/log_subscriber.rb +65 -0
- data/lib/believer/query.rb +10 -6
- data/lib/believer/relation.rb +1 -1
- data/lib/believer/test/test_run_life_cycle.rb +0 -1
- data/lib/believer/version.rb +1 -1
- data/spec/believer/empty_result_spec.rb +38 -0
- data/spec/believer/environment_spec.rb +12 -8
- data/spec/believer/test_run_life_cycle_spec.rb +6 -8
- data/spec/support/test_classes.rb +7 -1
- metadata +101 -112
data/README.md
CHANGED
@@ -170,6 +170,39 @@ development:
|
|
170
170
|
size: 10
|
171
171
|
timeout: 5
|
172
172
|
```
|
173
|
+
### Believer configuration
|
174
|
+
The Believer exposes some configuration options. These can added in a 'believer' node of the configuration hash.
|
175
|
+
For now only the logging is configurable.
|
176
|
+
|
177
|
+
Some examples:
|
178
|
+
|
179
|
+
``` yaml
|
180
|
+
development:
|
181
|
+
host: 127.0.0.1
|
182
|
+
port: 9042
|
183
|
+
keyspace: my_keyspace
|
184
|
+
believer:
|
185
|
+
logger:
|
186
|
+
# Use the application log (f.e. /log/development.log)
|
187
|
+
use_environment: true
|
188
|
+
|
189
|
+
development:
|
190
|
+
host: 127.0.0.1
|
191
|
+
port: 9042
|
192
|
+
keyspace: my_keyspace
|
193
|
+
believer:
|
194
|
+
logger:
|
195
|
+
# Use STDOUT to log messages
|
196
|
+
use_environment: false
|
197
|
+
|
198
|
+
development:
|
199
|
+
host: 127.0.0.1
|
200
|
+
port: 9042
|
201
|
+
keyspace: my_keyspace
|
202
|
+
believer:
|
203
|
+
# No Believer logging at all
|
204
|
+
logger: false
|
205
|
+
```
|
173
206
|
|
174
207
|
## Callbacks
|
175
208
|
The Believer::Base supports several callbacks to hook into the lifecycle of the models.
|
data/lib/believer.rb
CHANGED
@@ -14,9 +14,10 @@ require 'cql/client'
|
|
14
14
|
require 'yaml'
|
15
15
|
|
16
16
|
require 'believer/cql_helper'
|
17
|
-
require 'believer/environment'
|
17
|
+
require 'believer/environment/base_env'
|
18
18
|
require 'believer/environment/rails_env'
|
19
19
|
require 'believer/environment/merb_env'
|
20
|
+
require 'believer/environment'
|
20
21
|
require 'believer/connection'
|
21
22
|
require 'believer/values'
|
22
23
|
require 'believer/column'
|
@@ -24,21 +25,22 @@ require 'believer/columns'
|
|
24
25
|
require 'believer/model_schema'
|
25
26
|
require 'believer/persistence'
|
26
27
|
require 'believer/command'
|
27
|
-
require 'believer/querying'
|
28
28
|
require 'believer/where_clause'
|
29
|
-
require 'believer/empty_result'
|
30
29
|
require 'believer/limit'
|
31
30
|
require 'believer/order_by'
|
32
31
|
require 'believer/filter_command'
|
33
32
|
require 'believer/query'
|
33
|
+
require 'believer/empty_result'
|
34
34
|
require 'believer/delete'
|
35
35
|
require 'believer/insert'
|
36
|
+
require 'believer/querying'
|
36
37
|
require 'believer/scoping'
|
37
38
|
require 'believer/batch'
|
38
39
|
require 'believer/batch_delete'
|
39
40
|
require 'believer/callbacks'
|
40
41
|
require 'believer/finder_methods'
|
41
42
|
|
43
|
+
require 'believer/log_subscriber'
|
42
44
|
require 'believer/observer'
|
43
45
|
require 'believer/relation'
|
44
46
|
|
data/lib/believer/base.rb
CHANGED
data/lib/believer/command.rb
CHANGED
@@ -18,15 +18,22 @@ module Believer
|
|
18
18
|
{:record_class => @record_class}
|
19
19
|
end
|
20
20
|
|
21
|
-
def
|
21
|
+
def command_name
|
22
|
+
self.class.name.split('::').last.underscore
|
23
|
+
end
|
24
|
+
|
25
|
+
def execute(name = nil)
|
26
|
+
name = "#{record_class.name} #{command_name}" if name.nil?
|
22
27
|
@record_class.connection_pool.with do |connection|
|
23
28
|
cql = to_cql
|
24
29
|
begin
|
25
|
-
start = Time.now
|
26
|
-
#
|
27
|
-
|
28
|
-
|
29
|
-
return
|
30
|
+
#start = Time.now
|
31
|
+
#res = connection.execute(cql)
|
32
|
+
#puts "#{name} #{sprintf "%.3f", (Time.now - start)*1000.0} ms: #{cql}"
|
33
|
+
#return res
|
34
|
+
return ActiveSupport::Notifications.instrument('cql.believer', :cql => cql, :name => name) do
|
35
|
+
return connection.execute(cql)
|
36
|
+
end
|
30
37
|
rescue Cql::Protocol::DecodingError => e
|
31
38
|
# Decoding errors tend to #$%# up the connection, resulting in no more activity, so a reconnect is performed here.
|
32
39
|
# This is a known issue in cql-rb, and will be fixed in version 1.10
|
data/lib/believer/connection.rb
CHANGED
@@ -27,6 +27,12 @@ module Believer
|
|
27
27
|
def connection(environment)
|
28
28
|
unless @connection_pool
|
29
29
|
pool_config = environment.connection_pool_configuration
|
30
|
+
if pool_config.nil?
|
31
|
+
pool_config = {
|
32
|
+
:size => 1,
|
33
|
+
:timeout => 10
|
34
|
+
}
|
35
|
+
end
|
30
36
|
@connection_pool ||= ::ConnectionPool.new(pool_config) do
|
31
37
|
environment.create_connection(:connect_to_keyspace => true)
|
32
38
|
end
|
data/lib/believer/ddl.rb
CHANGED
@@ -7,26 +7,23 @@ module Believer
|
|
7
7
|
|
8
8
|
def drop_table
|
9
9
|
connection_pool.with do |connection|
|
10
|
-
cql = "
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
cql = "DROP TABLE #{table_name}"
|
11
|
+
ActiveSupport::Notifications.instrument('deserialize.believer', :class => self, :cql => cql, :method => :drop) do
|
12
|
+
connection.execute(cql)
|
13
|
+
end
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
17
|
def create_table
|
18
18
|
connection_pool.with do |connection|
|
19
19
|
cql = create_table_cql
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
ActiveSupport::Notifications.instrument('ddl.believer', :class => self, :cql => cql, :method => :create) do
|
21
|
+
connection.execute(cql)
|
22
|
+
end
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
26
|
def create_table_cql
|
27
|
-
s = "CREATE TABLE #{table_name} (\n"
|
28
|
-
col_statement_parts = columns.keys.map {|col| "#{col} #{columns[col].cql_type}"}
|
29
|
-
s << col_statement_parts.join(",\n")
|
30
27
|
|
31
28
|
keys = []
|
32
29
|
get_primary_key.each do |key_part|
|
@@ -36,6 +33,10 @@ module Believer
|
|
36
33
|
keys << key_part
|
37
34
|
end
|
38
35
|
end
|
36
|
+
|
37
|
+
s = "CREATE TABLE #{table_name} (\n"
|
38
|
+
col_statement_parts = columns.keys.map {|col| "#{col} #{columns[col].cql_type}"}
|
39
|
+
s << col_statement_parts.join(",\n")
|
39
40
|
s << ",\n"
|
40
41
|
s << "PRIMARY KEY (#{keys.join(',')})"
|
41
42
|
s << "\n)"
|
@@ -1,34 +1,65 @@
|
|
1
1
|
module Believer
|
2
2
|
|
3
|
-
class EmptyResult
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
3
|
+
#class EmptyResult
|
4
|
+
# DATA_METHODS = {
|
5
|
+
# :execute => [],
|
6
|
+
# :destroy_all => nil,
|
7
|
+
# :delete_all => 0,
|
8
|
+
# :to_a => [],
|
9
|
+
# :size => 0,
|
10
|
+
# :count => 0,
|
11
|
+
# :each => nil,
|
12
|
+
# :first => nil,
|
13
|
+
# :last => nil,
|
14
|
+
# :any? => false,
|
15
|
+
# :sort_by => nil,
|
16
|
+
# :loaded_objects => []
|
17
|
+
# }
|
18
|
+
# QUERY_METHODS = [:select, :where, :order, :limit]
|
19
|
+
#
|
20
|
+
# DATA_METHODS.each do |method_name, return_val|
|
21
|
+
# define_method(method_name) do |*|
|
22
|
+
# return_val
|
23
|
+
# end
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# QUERY_METHODS.each do |method_name|
|
27
|
+
# define_method(method_name) do |*|
|
28
|
+
# self
|
29
|
+
# end
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
#end
|
33
|
+
class EmptyResult < ::Believer::Query
|
34
|
+
|
35
|
+
def clone
|
36
|
+
self
|
24
37
|
end
|
25
38
|
|
26
|
-
|
27
|
-
|
28
|
-
self
|
29
|
-
end
|
39
|
+
def to_cql
|
40
|
+
nil
|
30
41
|
end
|
31
42
|
|
32
|
-
|
43
|
+
def to_a
|
44
|
+
[]
|
45
|
+
end
|
46
|
+
|
47
|
+
def exists?(*args)
|
48
|
+
false
|
49
|
+
end
|
50
|
+
|
51
|
+
def count
|
52
|
+
0
|
53
|
+
end
|
54
|
+
|
55
|
+
def execute(name = nil)
|
56
|
+
[]
|
57
|
+
end
|
33
58
|
|
59
|
+
protected
|
60
|
+
def loaded_objects
|
61
|
+
[]
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
34
65
|
end
|
data/lib/believer/environment.rb
CHANGED
@@ -1,20 +1,19 @@
|
|
1
|
-
#require "believer/environment/rails_env"
|
2
|
-
#require "believer/environment/merb_env"
|
3
|
-
|
4
1
|
module Believer
|
5
2
|
module Environment
|
6
3
|
extend ::ActiveSupport::Concern
|
4
|
+
ENVIRONMENTS = [
|
5
|
+
::Believer::Environment::RailsEnv,
|
6
|
+
::Believer::Environment::MerbEnv
|
7
|
+
]
|
7
8
|
|
8
9
|
module ClassMethods
|
9
|
-
|
10
10
|
def environment
|
11
11
|
if @environment.nil?
|
12
12
|
if self.superclass.respond_to?(:environment)
|
13
13
|
@environment = self.superclass.environment
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
@environment = ::Believer::Environment::MerbEnv.new
|
14
|
+
else
|
15
|
+
env_class = ENVIRONMENTS.find { |env| env.respond_to?(:applies?) && env.applies? }
|
16
|
+
@environment = env_class.new if env_class
|
18
17
|
end
|
19
18
|
end
|
20
19
|
@environment
|
@@ -26,81 +25,5 @@ module Believer
|
|
26
25
|
|
27
26
|
end
|
28
27
|
|
29
|
-
class BaseEnv
|
30
|
-
# Default pool configuration
|
31
|
-
DEFAULT_POOL_CONFIG = {
|
32
|
-
:size => 1,
|
33
|
-
:timeout => 10
|
34
|
-
}
|
35
|
-
|
36
|
-
# Creates a new environment using the provided configuration
|
37
|
-
# @param config [Hash] the configuration
|
38
|
-
def initialize(config = nil)
|
39
|
-
@configuration = config.dup unless config.nil?
|
40
|
-
end
|
41
|
-
|
42
|
-
# Returns the configuration. This configuration hash should contain the cql-rb client connection parameters.
|
43
|
-
# Optionally the connection_pool configuraton can be included in a :pool node.
|
44
|
-
def configuration
|
45
|
-
@configuration ||= load_configuration
|
46
|
-
end
|
47
|
-
|
48
|
-
# Sets the configuration
|
49
|
-
def configuration=(config)
|
50
|
-
@configuration = config
|
51
|
-
end
|
52
|
-
|
53
|
-
def connection_configuration
|
54
|
-
configuration.reject {|k, v| k == :pool}
|
55
|
-
end
|
56
|
-
|
57
|
-
# The connection_pool configuration, which should be a :pool node in the configuration.
|
58
|
-
def connection_pool_configuration
|
59
|
-
DEFAULT_POOL_CONFIG.merge(configuration[:pool].is_a?(Hash) ? configuration[:pool].symbolize_keys! : {})
|
60
|
-
end
|
61
|
-
|
62
|
-
# Creates a new connection
|
63
|
-
def create_connection(options = {})
|
64
|
-
cc = connection_configuration
|
65
|
-
if options[:connect_to_keyspace] && cc[:keyspace]
|
66
|
-
connection = Cql::Client.connect(cc)
|
67
|
-
connection.use(cc[:keyspace])
|
68
|
-
else
|
69
|
-
connection = Cql::Client.connect(cc.delete_if {|k,v|k == :keyspace})
|
70
|
-
end
|
71
|
-
connection
|
72
|
-
end
|
73
|
-
|
74
|
-
def create_keyspace(properties = {}, connection = nil)
|
75
|
-
conn = connection || create_connection(:connect_to_keyspace => false)
|
76
|
-
|
77
|
-
default_properties = {:replication => {:class => 'SimpleStrategy', :replication_factor => 1}}
|
78
|
-
ks_props = default_properties.merge(properties)
|
79
|
-
|
80
|
-
ks_props_s = ks_props.keys.map {|k|
|
81
|
-
v = ks_props[k]
|
82
|
-
v_s = nil
|
83
|
-
if v.is_a?(Hash)
|
84
|
-
v_s = v.to_json.gsub(/\"/) {|m| "'"}
|
85
|
-
elsif v.is_a?(String)
|
86
|
-
v_s = "'#{v}'"
|
87
|
-
else
|
88
|
-
v_s = v.to_s
|
89
|
-
end
|
90
|
-
"#{k} = #{v_s}"
|
91
|
-
}.join("\nAND ")
|
92
|
-
|
93
|
-
ks_def = <<-KS_DEF
|
94
|
-
CREATE KEYSPACE #{connection_configuration[:keyspace]}
|
95
|
-
WITH #{ks_props_s}
|
96
|
-
KS_DEF
|
97
|
-
|
98
|
-
puts ks_def
|
99
|
-
|
100
|
-
conn.execute(ks_def)
|
101
|
-
end
|
102
|
-
|
103
|
-
end
|
104
|
-
|
105
28
|
end
|
106
29
|
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
module Believer
|
2
|
+
module Environment
|
3
|
+
class BaseEnv
|
4
|
+
DEFAULT_CONFIG = {
|
5
|
+
:pool => {
|
6
|
+
:size => 1,
|
7
|
+
:timeout => 10
|
8
|
+
},
|
9
|
+
:believer => {
|
10
|
+
:logger => {
|
11
|
+
:use_environment => true,
|
12
|
+
:level => ::Logger::DEBUG
|
13
|
+
}
|
14
|
+
}
|
15
|
+
}
|
16
|
+
|
17
|
+
# Creates a new environment using the provided configuration
|
18
|
+
# @param config [Hash] the configuration
|
19
|
+
def initialize(config = nil)
|
20
|
+
@configuration = config.dup unless config.nil?
|
21
|
+
end
|
22
|
+
|
23
|
+
def logger
|
24
|
+
return nil unless believer_configuration[:logger]
|
25
|
+
return environment_logger if believer_configuration[:logger][:use_environment] && respond_to?(:environment_logger)
|
26
|
+
unless @std_logger
|
27
|
+
puts believer_configuration[:logger]
|
28
|
+
@std_logger = ::Logger.new(STDOUT)
|
29
|
+
if believer_configuration[:logger][:level] && believer_configuration[:logger][:level].is_a?(Numeric)
|
30
|
+
@std_logger.level = believer_configuration[:logger][:level].to_i
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
@std_logger
|
35
|
+
end
|
36
|
+
|
37
|
+
# Returns the configuration. This configuration hash should contain the cql-rb client connection parameters.
|
38
|
+
# Optionally the connection_pool configuraton can be included in a :pool node.
|
39
|
+
def configuration
|
40
|
+
unless @configuration
|
41
|
+
loaded = load_configuration
|
42
|
+
config = HashWithIndifferentAccess.new(DEFAULT_CONFIG.merge(loaded))
|
43
|
+
@configuration = config
|
44
|
+
end
|
45
|
+
@configuration
|
46
|
+
end
|
47
|
+
|
48
|
+
# Sets the configuration
|
49
|
+
def configuration=(config)
|
50
|
+
@configuration = config
|
51
|
+
end
|
52
|
+
|
53
|
+
def connection_configuration
|
54
|
+
configuration.reject { |k, v| k == :pool || k == :believer }
|
55
|
+
end
|
56
|
+
|
57
|
+
# The connection_pool configuration, which should be a :pool node in the configuration.
|
58
|
+
def connection_pool_configuration
|
59
|
+
pc = configuration[:pool]
|
60
|
+
return DEFAULT_CONFIG[:pool] unless pc
|
61
|
+
pc
|
62
|
+
end
|
63
|
+
|
64
|
+
# The connection_pool configuration, which should be a :pool node in the configuration.
|
65
|
+
def believer_configuration
|
66
|
+
configuration[:believer]
|
67
|
+
end
|
68
|
+
|
69
|
+
# Creates a new connection
|
70
|
+
def create_connection(options = {})
|
71
|
+
cc = connection_configuration
|
72
|
+
if options[:connect_to_keyspace] && cc[:keyspace]
|
73
|
+
connection = Cql::Client.connect(cc)
|
74
|
+
connection.use(cc[:keyspace])
|
75
|
+
else
|
76
|
+
connection = Cql::Client.connect(cc.delete_if { |k, v| k == :keyspace })
|
77
|
+
end
|
78
|
+
connection
|
79
|
+
end
|
80
|
+
|
81
|
+
def create_keyspace(properties = {}, connection = nil)
|
82
|
+
conn = connection || create_connection(:connect_to_keyspace => false)
|
83
|
+
|
84
|
+
default_properties = {:replication => {:class => 'SimpleStrategy', :replication_factor => 1}}
|
85
|
+
ks_props = default_properties.merge(properties)
|
86
|
+
|
87
|
+
ks_props_s = ks_props.keys.map { |k|
|
88
|
+
v = ks_props[k]
|
89
|
+
v_s = nil
|
90
|
+
if v.is_a?(Hash)
|
91
|
+
v_s = v.to_json.gsub(/\"/) { |m| "'" }
|
92
|
+
elsif v.is_a?(String)
|
93
|
+
v_s = "'#{v}'"
|
94
|
+
else
|
95
|
+
v_s = v.to_s
|
96
|
+
end
|
97
|
+
"#{k} = #{v_s}"
|
98
|
+
}.join("\nAND ")
|
99
|
+
|
100
|
+
ks_def = <<-KS_DEF
|
101
|
+
CREATE KEYSPACE #{connection_configuration[:keyspace]}
|
102
|
+
WITH #{ks_props_s}
|
103
|
+
KS_DEF
|
104
|
+
|
105
|
+
puts ks_def
|
106
|
+
|
107
|
+
conn.execute(ks_def)
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
end
|
@@ -3,6 +3,10 @@ module Believer
|
|
3
3
|
module Environment
|
4
4
|
class MerbEnv < Believer::Environment::BaseEnv
|
5
5
|
|
6
|
+
def self.applies?
|
7
|
+
defined?(::Merb)
|
8
|
+
end
|
9
|
+
|
6
10
|
def load_configuration
|
7
11
|
config_file = File.join(Merb.root, 'config', 'believer.yml')
|
8
12
|
config = HashWithIndifferentAccess.new(YAML::load(File.open(config_file.to_s)))
|
@@ -1,15 +1,26 @@
|
|
1
1
|
|
2
2
|
module Believer
|
3
3
|
module Environment
|
4
|
-
class RailsEnv < Believer::Environment::BaseEnv
|
4
|
+
class RailsEnv < ::Believer::Environment::BaseEnv
|
5
|
+
|
6
|
+
def self.applies?
|
7
|
+
defined?(::Rails) && ::Rails.respond_to?(:env) && ::Rails.respond_to?(:root)
|
8
|
+
end
|
5
9
|
|
6
10
|
def load_configuration
|
7
11
|
config_file = File.join(Rails.root, 'config', 'believer.yml')
|
8
12
|
config = HashWithIndifferentAccess.new(YAML::load(File.open(config_file.to_s)))
|
9
13
|
env_config = config[Rails.env]
|
10
|
-
env_config[:logger] = Rails.logger unless Rails.logger.nil?
|
11
14
|
env_config
|
12
15
|
end
|
16
|
+
|
17
|
+
def environment_logger
|
18
|
+
if defined?(Rails) && !Rails.nil? && Rails.logger && Rails.logger.respond_to?(:debug)
|
19
|
+
return Rails.logger
|
20
|
+
end
|
21
|
+
super
|
22
|
+
end
|
23
|
+
|
13
24
|
end
|
14
25
|
end
|
15
26
|
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module Believer
|
2
|
+
|
3
|
+
class LogSubscriber < ActiveSupport::LogSubscriber
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
super
|
7
|
+
@odd_or_even = false
|
8
|
+
end
|
9
|
+
|
10
|
+
def ddl(event)
|
11
|
+
return unless logger.debug?
|
12
|
+
|
13
|
+
payload = event.payload
|
14
|
+
|
15
|
+
name = '%s (%.1fms) [CQL]' % [payload[:class].to_s, event.duration]
|
16
|
+
name = color(name, MAGENTA, true)
|
17
|
+
cql = color(payload[:cql].squeeze(' '), BLACK, true)
|
18
|
+
|
19
|
+
debug " #{name} #{payload[:method]} table:\n#{cql}"
|
20
|
+
end
|
21
|
+
|
22
|
+
def deserialize(event)
|
23
|
+
return unless logger.debug?
|
24
|
+
|
25
|
+
payload = event.payload
|
26
|
+
|
27
|
+
name = '%s (%.1fms)' % [payload[:class].to_s, event.duration]
|
28
|
+
name = color(name, RED, true)
|
29
|
+
|
30
|
+
debug " #{name} deserialized #{payload[:count]} objects"
|
31
|
+
end
|
32
|
+
|
33
|
+
def cql(event)
|
34
|
+
return unless logger.debug?
|
35
|
+
|
36
|
+
payload = event.payload
|
37
|
+
|
38
|
+
return if 'SCHEMA' == payload[:name]
|
39
|
+
|
40
|
+
name = '%s (%.1fms) [CQL]' % [payload[:name], event.duration]
|
41
|
+
cql = payload[:cql].squeeze(' ')
|
42
|
+
|
43
|
+
if odd?
|
44
|
+
name = color(name, GREEN, true)
|
45
|
+
cql = color(cql, nil, true)
|
46
|
+
else
|
47
|
+
name = color(name, CYAN, true)
|
48
|
+
end
|
49
|
+
|
50
|
+
debug " #{name} #{cql}"
|
51
|
+
end
|
52
|
+
|
53
|
+
def logger
|
54
|
+
Believer::Base.logger
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
def odd?
|
59
|
+
@odd_or_even = !@odd_or_even
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
Believer::LogSubscriber.attach_to :believer
|
data/lib/believer/query.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
module Believer
|
2
2
|
class Query < FilterCommand
|
3
|
-
|
4
3
|
attr_accessor :record_class, :selects, :order_by, :limit_to
|
5
4
|
|
6
5
|
delegate *(Enumerable.instance_methods(false).map {|enum_method| enum_method.to_sym}), :to => :to_a
|
@@ -89,12 +88,17 @@ module Believer
|
|
89
88
|
def to_a
|
90
89
|
if @loaded_objects.nil?
|
91
90
|
result = execute
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
91
|
+
notify_payload = {:class => @record_class}
|
92
|
+
@loaded_objects = ActiveSupport::Notifications.instrument('deserialize.believer', notify_payload) do
|
93
|
+
start = Time.now
|
94
|
+
objects = []
|
95
|
+
result.each do |row|
|
96
|
+
objects << record_class.instantiate_from_result_rows(row)
|
97
|
+
end
|
98
|
+
notify_payload[:count] = objects.count
|
99
|
+
objects
|
96
100
|
end
|
97
|
-
puts "Took #{sprintf "%.3f", (Time.now - start)*1000.0} ms to deserialize #{@loaded_objects.size} object(s)"
|
101
|
+
#puts "Took #{sprintf "%.3f", (Time.now - start)*1000.0} ms to deserialize #{@loaded_objects.size} object(s)"
|
98
102
|
end
|
99
103
|
@loaded_objects
|
100
104
|
end
|
data/lib/believer/relation.rb
CHANGED
@@ -76,7 +76,7 @@ module Believer
|
|
76
76
|
if options[:filter]
|
77
77
|
q = self.instance_exec(q, &(options[:filter]))
|
78
78
|
unless q
|
79
|
-
er = EmptyResult.new
|
79
|
+
er = EmptyResult.new(:record_class => relation_class)
|
80
80
|
er.extend(CollectionMethods)
|
81
81
|
er.extend(::Believer::FinderMethods)
|
82
82
|
return er
|
data/lib/believer/version.rb
CHANGED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Believer::EmptyResult do
|
4
|
+
|
5
|
+
let :empty_result do
|
6
|
+
Believer::EmptyResult.new(:record_class => Test::Artist)
|
7
|
+
end
|
8
|
+
let :artist do
|
9
|
+
Test::Artist.create(:name => 'Colplay')
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should not return data" do
|
13
|
+
expect(empty_result.size).to eq(0)
|
14
|
+
expect(empty_result.to_a).to eq([])
|
15
|
+
end
|
16
|
+
|
17
|
+
{
|
18
|
+
:select => [:name],
|
19
|
+
:where => {:name => 'Colplay'},
|
20
|
+
:order => :name,
|
21
|
+
:limit => 10
|
22
|
+
}.each do |method, args|
|
23
|
+
it "should remain empty after #{method} query call" do
|
24
|
+
empty_new = empty_result.send(method, args)
|
25
|
+
expect(empty_new.size).to eq(0)
|
26
|
+
expect(empty_new.to_a).to eq([])
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should return false for exists?" do
|
31
|
+
expect(empty_result.exists?).to eq(false)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should return 0 for count" do
|
35
|
+
expect(empty_result.count).to eq(0)
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -11,18 +11,22 @@ describe Believer::Environment do
|
|
11
11
|
Believer::Base.environment = @original_env
|
12
12
|
end
|
13
13
|
|
14
|
-
it 'load the Merb configuration' do
|
15
|
-
Merb = Struct.new(:root, :environment, :logger).new(File.join(RSpec.configuration.test_files_dir, 'merb'), :development, nil)
|
16
|
-
env = Believer::Base.environment
|
17
|
-
env.class.should == Believer::Environment::MerbEnv
|
18
|
-
env.configuration[:host].should == 'merb.local'
|
19
|
-
end
|
20
|
-
|
21
14
|
it 'load the rails configuration' do
|
22
|
-
Rails = Struct.new(:root, :env, :logger).new(File.join(RSpec.configuration.test_files_dir, 'rails'), :
|
15
|
+
Rails = Struct.new(:root, :env, :logger).new(File.join(RSpec.configuration.test_files_dir, 'rails'), :test, nil)
|
23
16
|
env = Believer::Base.environment
|
24
17
|
env.class.should == Believer::Environment::RailsEnv
|
25
18
|
env.configuration[:host].should == '123.456.789.0'
|
19
|
+
#remove_const(Rails)
|
20
|
+
Rails = nil
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'load the Merb configuration' do
|
24
|
+
Merb = Struct.new(:root, :environment, :logger).new(File.join(RSpec.configuration.test_files_dir, 'merb'), :test, nil)
|
25
|
+
env = Believer::Base.environment
|
26
|
+
env.class.should == Believer::Environment::MerbEnv
|
27
|
+
env.configuration[:host].should == 'merb.test.local'
|
28
|
+
#remove_const(Merb)
|
29
|
+
Merb = nil
|
26
30
|
end
|
27
31
|
|
28
32
|
end
|
@@ -8,34 +8,32 @@ describe Believer::Test::TestRunLifeCycle do
|
|
8
8
|
@destroyed_count = 0
|
9
9
|
@destroy_monitor = lambda do |obj|
|
10
10
|
@destroyed_count += 1
|
11
|
-
puts "Destroyed"
|
12
11
|
end
|
13
12
|
|
14
13
|
begin
|
15
14
|
XXX.drop_table
|
16
|
-
|
15
|
+
|
17
16
|
rescue
|
17
|
+
ensure
|
18
|
+
XXX.create_table
|
18
19
|
end
|
19
|
-
|
20
20
|
end
|
21
21
|
|
22
22
|
after :all do
|
23
|
-
puts "
|
23
|
+
puts "DROP TABLE XXXX"
|
24
24
|
@destroyed_count.should == @created.size
|
25
25
|
XXX.drop_table
|
26
26
|
end
|
27
27
|
|
28
28
|
before :each do
|
29
|
-
|
30
29
|
@created = []
|
31
30
|
10.times do |i|
|
32
31
|
@created << XXX.create(:name => "artist_#{i}", :destroy_monitor => @destroy_monitor)
|
33
|
-
puts "Created"
|
34
32
|
end
|
35
33
|
end
|
36
34
|
|
37
|
-
it "should clean all created objects" do
|
38
|
-
|
35
|
+
it "should clean all created objects, even after a fail" do
|
36
|
+
#fail
|
39
37
|
end
|
40
38
|
|
41
39
|
class XXX < Believer::Base
|
@@ -61,7 +61,13 @@ module Test
|
|
61
61
|
|
62
62
|
class Environment < Believer::Environment::BaseEnv
|
63
63
|
def configuration
|
64
|
-
{
|
64
|
+
{
|
65
|
+
:host => '127.0.0.1',
|
66
|
+
:keyspace => 'believer_test_space',
|
67
|
+
:believer => {
|
68
|
+
:logger => {:level => ::Logger::DEBUG}
|
69
|
+
}
|
70
|
+
}
|
65
71
|
end
|
66
72
|
end
|
67
73
|
|
metadata
CHANGED
@@ -1,124 +1,119 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: believer
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.4
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 2
|
9
|
-
- 3
|
10
|
-
version: 0.2.3
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Jerphaes van Blijenburgh
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2013-10-25 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
21
15
|
name: activemodel
|
22
|
-
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
24
17
|
none: false
|
25
|
-
requirements:
|
26
|
-
- -
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
|
29
|
-
segments:
|
30
|
-
- 3
|
31
|
-
- 2
|
32
|
-
- 13
|
33
|
-
version: 3.2.13
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3'
|
34
22
|
type: :runtime
|
35
|
-
version_requirements: *id001
|
36
|
-
- !ruby/object:Gem::Dependency
|
37
|
-
name: cql-rb
|
38
23
|
prerelease: false
|
39
|
-
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
25
|
none: false
|
41
|
-
requirements:
|
42
|
-
- -
|
43
|
-
- !ruby/object:Gem::Version
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: cql-rb
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
51
37
|
version: 1.1.0.pre6
|
52
38
|
type: :runtime
|
53
|
-
version_requirements: *id002
|
54
|
-
- !ruby/object:Gem::Dependency
|
55
|
-
name: connection_pool
|
56
39
|
prerelease: false
|
57
|
-
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
41
|
none: false
|
59
|
-
requirements:
|
60
|
-
- -
|
61
|
-
- !ruby/object:Gem::Version
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.1.0.pre6
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: connection_pool
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
66
54
|
type: :runtime
|
67
|
-
version_requirements: *id003
|
68
|
-
- !ruby/object:Gem::Dependency
|
69
|
-
name: rake
|
70
55
|
prerelease: false
|
71
|
-
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rake
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
72
65
|
none: false
|
73
|
-
requirements:
|
74
|
-
- -
|
75
|
-
- !ruby/object:Gem::Version
|
76
|
-
|
77
|
-
segments:
|
78
|
-
- 0
|
79
|
-
version: "0"
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
80
70
|
type: :development
|
81
|
-
version_requirements: *id004
|
82
|
-
- !ruby/object:Gem::Dependency
|
83
|
-
name: rspec
|
84
71
|
prerelease: false
|
85
|
-
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
86
73
|
none: false
|
87
|
-
requirements:
|
88
|
-
- -
|
89
|
-
- !ruby/object:Gem::Version
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: rspec
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
94
86
|
type: :development
|
95
|
-
version_requirements: *id005
|
96
|
-
- !ruby/object:Gem::Dependency
|
97
|
-
name: simplecov
|
98
87
|
prerelease: false
|
99
|
-
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
100
89
|
none: false
|
101
|
-
requirements:
|
102
|
-
- -
|
103
|
-
- !ruby/object:Gem::Version
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: simplecov
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - '='
|
100
|
+
- !ruby/object:Gem::Version
|
109
101
|
version: 0.7.1
|
110
102
|
type: :development
|
111
|
-
|
112
|
-
|
113
|
-
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - '='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 0.7.1
|
110
|
+
description: ! 'An Object Relational Mapping library for CQL3 '
|
111
|
+
email:
|
114
112
|
- jerphaes@gmail.com
|
115
113
|
executables: []
|
116
|
-
|
117
114
|
extensions: []
|
118
|
-
|
119
115
|
extra_rdoc_files: []
|
120
|
-
|
121
|
-
files:
|
116
|
+
files:
|
122
117
|
- lib/believer/base.rb
|
123
118
|
- lib/believer/batch.rb
|
124
119
|
- lib/believer/batch_delete.rb
|
@@ -131,6 +126,7 @@ files:
|
|
131
126
|
- lib/believer/ddl.rb
|
132
127
|
- lib/believer/delete.rb
|
133
128
|
- lib/believer/empty_result.rb
|
129
|
+
- lib/believer/environment/base_env.rb
|
134
130
|
- lib/believer/environment/merb_env.rb
|
135
131
|
- lib/believer/environment/rails_env.rb
|
136
132
|
- lib/believer/environment.rb
|
@@ -138,6 +134,7 @@ files:
|
|
138
134
|
- lib/believer/finder_methods.rb
|
139
135
|
- lib/believer/insert.rb
|
140
136
|
- lib/believer/limit.rb
|
137
|
+
- lib/believer/log_subscriber.rb
|
141
138
|
- lib/believer/model_schema.rb
|
142
139
|
- lib/believer/observer.rb
|
143
140
|
- lib/believer/order_by.rb
|
@@ -155,6 +152,7 @@ files:
|
|
155
152
|
- spec/believer/base_spec.rb
|
156
153
|
- spec/believer/callback_spec.rb
|
157
154
|
- spec/believer/delete_spec.rb
|
155
|
+
- spec/believer/empty_result_spec.rb
|
158
156
|
- spec/believer/environment_spec.rb
|
159
157
|
- spec/believer/finder_methods_spec.rb
|
160
158
|
- spec/believer/insert_spec.rb
|
@@ -170,44 +168,35 @@ files:
|
|
170
168
|
- spec/support/setup_database.rb
|
171
169
|
- spec/support/test_classes.rb
|
172
170
|
homepage: http://github.com/jerphaes/believer
|
173
|
-
licenses:
|
171
|
+
licenses:
|
174
172
|
- Apache License 2.0
|
175
173
|
post_install_message:
|
176
174
|
rdoc_options: []
|
177
|
-
|
178
|
-
require_paths:
|
175
|
+
require_paths:
|
179
176
|
- lib
|
180
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
177
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
181
178
|
none: false
|
182
|
-
requirements:
|
183
|
-
- -
|
184
|
-
- !ruby/object:Gem::Version
|
185
|
-
hash: 55
|
186
|
-
segments:
|
187
|
-
- 1
|
188
|
-
- 9
|
189
|
-
- 2
|
179
|
+
requirements:
|
180
|
+
- - ! '>='
|
181
|
+
- !ruby/object:Gem::Version
|
190
182
|
version: 1.9.2
|
191
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
183
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
192
184
|
none: false
|
193
|
-
requirements:
|
194
|
-
- -
|
195
|
-
- !ruby/object:Gem::Version
|
196
|
-
|
197
|
-
segments:
|
198
|
-
- 0
|
199
|
-
version: "0"
|
185
|
+
requirements:
|
186
|
+
- - ! '>='
|
187
|
+
- !ruby/object:Gem::Version
|
188
|
+
version: '0'
|
200
189
|
requirements: []
|
201
|
-
|
202
190
|
rubyforge_project:
|
203
191
|
rubygems_version: 1.8.25
|
204
192
|
signing_key:
|
205
193
|
specification_version: 3
|
206
194
|
summary: CQL3 ORM
|
207
|
-
test_files:
|
195
|
+
test_files:
|
208
196
|
- spec/believer/base_spec.rb
|
209
197
|
- spec/believer/callback_spec.rb
|
210
198
|
- spec/believer/delete_spec.rb
|
199
|
+
- spec/believer/empty_result_spec.rb
|
211
200
|
- spec/believer/environment_spec.rb
|
212
201
|
- spec/believer/finder_methods_spec.rb
|
213
202
|
- spec/believer/insert_spec.rb
|