quandl_cassandra 1.0.3 → 1.1.0

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: f83fe0075114dcd993b31f6b0a0f56799d66bb90
4
- data.tar.gz: daad90584e0adbf6edd4190ca50fb5f97b8d9eef
3
+ metadata.gz: a1b7c7cb00077c1c1d39ab0bc9a8141e2c7f7197
4
+ data.tar.gz: 3fd25aaa7cf8f6105425ecf0ae4d2c0c212afe13
5
5
  SHA512:
6
- metadata.gz: 5140c4b99eec70768e703a1f1f44305462e518c0e3af9712c17448c11dbf69c4b4736a19f30eb28914136e3d4c021edc69ab5afb56328d430a554274e6542195
7
- data.tar.gz: ad3c0b11d1657d9f78480dc2fcfd35af61080a4b2bf0e7721ae266dd5de0ae5a7c7f543ba2c2d71d06e52025385088cdef368d87ce997bf729546af06001e7ca
6
+ metadata.gz: 3c48d85c066b89c52c3e03b0f0086eb69a5b8d5f99da04208d0f898c4e54e5f5fd6043799eba5dd0dce576df357b4a0c3a27992fa7c306c24edee991a97f6746
7
+ data.tar.gz: 4a397f15caa8cf38f7c75d8c120f82f1547c408fd4f2c8ae9d74a9966c2c5a1fdea46fb68c6c374ee9b64768bff0c9a5cab55998e29778e88397d4e184b03741
@@ -42,10 +42,19 @@ module Attributes
42
42
  end
43
43
 
44
44
  def initialize(*args)
45
+ attrs = args.extract_options!
45
46
  @attributes = {}
46
- self.assign_attributes(args.extract_options!)
47
+ # result set attributes?
48
+ result_set = attrs.delete(:_result_set) if attrs.has_key?(:_result_set)
49
+ # assign
50
+ if result_set.present? && result_set.is_a?(Hash)
51
+ @attributes = result_set.symbolize_keys!
52
+ self.new_record = false
53
+ end
54
+ # assign attributes
55
+ self.assign_attributes(attrs)
47
56
  end
48
-
57
+
49
58
  def inspect
50
59
  "#{self.class.name} " + attributes.inspect
51
60
  end
@@ -4,6 +4,10 @@ module Quandl::Cassandra::Base::Connection
4
4
 
5
5
  module ClassMethods
6
6
 
7
+ def truncate
8
+ execute("TRUNCATE #{table_name}")
9
+ end
10
+
7
11
  def delete(*ids)
8
12
  statement = prepare("DELETE FROM #{table_name} WHERE id = ?")
9
13
  Array(ids).flatten.
@@ -22,10 +22,7 @@ module Quandl::Cassandra::Base::Persistence
22
22
 
23
23
  def new_from_query_result(result)
24
24
  return nil if result.blank?
25
- r = self.new
26
- r.new_record = false
27
- r.attributes = result
28
- r
25
+ self.new( _result_set: result )
29
26
  end
30
27
 
31
28
  end
@@ -1,5 +1,5 @@
1
1
  module Quandl
2
2
  module Cassandra
3
- VERSION = '1.0.3'
3
+ VERSION = '1.1.0'
4
4
  end
5
5
  end
@@ -4,22 +4,24 @@ Quandl::Cassandra.configure do |c|
4
4
  c.consistency = :one
5
5
  end
6
6
 
7
- # reset keyspace
7
+ # check for keyspace
8
8
  keyspace_exists = Quandl::Cassandra::Base.execute("SELECT * FROM system.schema_keyspaces WHERE keyspace_name = 'quandl_cassandra_test';")
9
- Quandl::Cassandra::Base.execute("DROP KEYSPACE quandl_cassandra_test;") if keyspace_exists.present?
10
9
 
11
- # create keyspace
12
- Quandl::Cassandra::Base.execute("CREATE KEYSPACE quandl_cassandra_test WITH REPLICATION = {'class' : 'SimpleStrategy', 'replication_factor': 3};")
10
+ # create keyspace if not exists
11
+ Quandl::Cassandra::Base.execute("CREATE KEYSPACE quandl_cassandra_test WITH REPLICATION = {'class' : 'SimpleStrategy', 'replication_factor': 1};") unless keyspace_exists
13
12
 
14
13
  # reconnect to keyspace
15
14
  Quandl::Cassandra.configuration.keyspace = 'quandl_cassandra_test'
16
15
  Quandl::Cassandra::Base.reset_connection
17
16
 
18
17
  # create posts table
19
- Quandl::Cassandra::Base.execute("CREATE TABLE posts (
18
+ begin
19
+ Quandl::Cassandra::Base.execute("CREATE TABLE posts (
20
20
  id uuid PRIMARY KEY,
21
21
  content text,
22
22
  author varchar,
23
23
  updated_at timestamp );")
24
-
24
+ rescue
25
+ end
26
+
25
27
  class Post < Quandl::Cassandra::Base; end
@@ -0,0 +1,13 @@
1
+ FactoryGirl.define do
2
+
3
+ sequence :content do |n|
4
+ "title#{n}"
5
+ end
6
+
7
+ factory :post do
8
+ id {Cql::Uuid.new(SecureRandom.uuid)}
9
+ content
10
+ author "quandl"
11
+ end
12
+
13
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ class PostWithCallbacks < Quandl::Cassandra::Base
4
+
5
+ table_name :posts
6
+
7
+ after_initialize :set_values
8
+
9
+ def set_values
10
+ self.content = 'always change'
11
+ self.author = 'only set as default' unless author.present?
12
+ end
13
+
14
+ end
15
+
16
+ describe Quandl::Cassandra::Base::Connection do
17
+ subject{ PostWithCallbacks.new( content: 'should be changed', author: 'should not change' ) }
18
+
19
+ its(:content){ should eq 'always change' }
20
+ its(:author){ should eq 'should not change' }
21
+ its(:changes){ should eq({"content"=>[nil, "always change"], "author"=>[nil, "should not change"]}) }
22
+
23
+ describe ".new_from_query_result" do
24
+ subject{ PostWithCallbacks.new_from_query_result({ content: 'should be changed', author: 'should not change' }) }
25
+
26
+ its(:content){ should eq 'always change' }
27
+ its(:author){ should eq 'should not change' }
28
+ its(:changes){ should eq({"content"=>["should be changed", "always change"]}) }
29
+
30
+ end
31
+
32
+ end
@@ -13,6 +13,15 @@ describe Quandl::Cassandra::Base::Persistence do
13
13
 
14
14
  its(:new_record?){ should be_true }
15
15
 
16
+ describe ".new_from_query_result" do
17
+ subject{ Post.new_from_query_result({ id: id, content: 'blah', author: 'someone' }) }
18
+
19
+ its(:changes){ should be_blank }
20
+ its(:id){ should eq id }
21
+ its(:content){ should eq 'blah' }
22
+ its(:author){ should eq 'someone' }
23
+ end
24
+
16
25
  describe ".create" do
17
26
 
18
27
  let(:post){ Post.create( id: id, content: 'test content', author: "quandl" ) }
@@ -27,6 +36,8 @@ describe Quandl::Cassandra::Base::Persistence do
27
36
  its(:content){ should eq 'test content' }
28
37
  its(:author){ should eq 'quandl' }
29
38
  its(:new_record?){ should be_false }
39
+ its(:changes){ should be_blank }
40
+ its(:changed?){ should be_false }
30
41
  end
31
42
 
32
43
  describe ".destroy" do
@@ -1,7 +1,13 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Quandl::Cassandra::Base::Scoping do
4
-
4
+
5
+ before(:all){
6
+ Post.truncate
7
+ 5.times{ create(:post) }
8
+ }
9
+ let(:posts){ Post.all }
10
+
5
11
  subject{ Post }
6
12
 
7
13
  [:where, :select, :limit, :order].each do |name|
@@ -12,6 +18,35 @@ describe Quandl::Cassandra::Base::Scoping do
12
18
 
13
19
  its(:scope_names){ should eq Post.scope.scope_names }
14
20
 
21
+ describe ".all" do
22
+
23
+ subject{ posts }
24
+ its(:count){ should eq 5 }
25
+
26
+ describe "#first" do
27
+ subject{ posts.first }
28
+
29
+ its(:changes){ should be_blank }
30
+ its(:id){ should be_a Cql::Uuid }
31
+ end
32
+ end
33
+
34
+ describe ".where" do
35
+
36
+ let(:ids){ posts.collect(&:id)[1..3] }
37
+ subject{ Post.where(id: ids ) }
38
+
39
+ its(:count){ should eq 3 }
40
+
41
+ describe "#first" do
42
+ subject{ posts.first }
43
+
44
+ its(:changes){ should be_blank }
45
+ its(:id){ should be_a Cql::Uuid }
46
+ end
47
+
48
+ end
49
+
15
50
  describe ".find_or_build" do
16
51
  it "given nil should be a new record" do
17
52
  Post.find_or_build(nil).new_record?.should be_true
data/spec/spec_helper.rb CHANGED
@@ -9,12 +9,12 @@ require 'securerandom'
9
9
 
10
10
  require "quandl/cassandra"
11
11
  require "quandl/fabricate"
12
+ require 'rspec/expectations'
12
13
 
13
14
  # require config
14
15
  Dir.glob( File.join( File.dirname(__FILE__), 'config/**/*.rb' ) ).each{|f| require(f) }
15
16
 
16
17
  # require expectations
17
- require 'rspec/expectations'
18
18
  Dir.glob( File.join( File.dirname(__FILE__), 'expectations/**/*.rb' ) ).each{|f| require(f) }
19
19
 
20
20
  # require factories
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quandl_cassandra
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blake Hilscher
@@ -239,6 +239,8 @@ files:
239
239
  - spec/config/logger.rb
240
240
  - spec/expectations/string.rb
241
241
  - spec/expectations/time.rb
242
+ - spec/factories/post.rb
243
+ - spec/lib/quandl/cassandra/base/callbacks_spec.rb
242
244
  - spec/lib/quandl/cassandra/base/connection_spec.rb
243
245
  - spec/lib/quandl/cassandra/base/persistence_spec.rb
244
246
  - spec/lib/quandl/cassandra/base/sanitization_spec.rb
@@ -274,6 +276,8 @@ test_files:
274
276
  - spec/config/logger.rb
275
277
  - spec/expectations/string.rb
276
278
  - spec/expectations/time.rb
279
+ - spec/factories/post.rb
280
+ - spec/lib/quandl/cassandra/base/callbacks_spec.rb
277
281
  - spec/lib/quandl/cassandra/base/connection_spec.rb
278
282
  - spec/lib/quandl/cassandra/base/persistence_spec.rb
279
283
  - spec/lib/quandl/cassandra/base/sanitization_spec.rb