gotime-cassandra_object 4.10.1 → 4.10.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a094a6b0709d5666b254585cf07f2e8889abf318
4
- data.tar.gz: f13ccf6c6431c0f576110ef956b18eb809367bd5
3
+ metadata.gz: dc64be77dc2226fed23bcf8c42c485481c9ce89c
4
+ data.tar.gz: 01fe5ae6645ce36caf4b5825b31ac985be16a55d
5
5
  SHA512:
6
- metadata.gz: b6fa0ffb1d1c554e906be4adf20ae4c1a0dac7148fa7bff4d15f9574750946f2f41d4833b86058f0d62825529a4a5d6f1e24dc19c8912afd86d2185ac48bbcd5
7
- data.tar.gz: 5060c16d39315b8cc1c98855582450715ac3e30162ab9328a148e2fac2bdc6b401f44fd29fbbc2df2b66864fb5b08187c08079a48042f477dea4f9ea9793be94
6
+ metadata.gz: 115a5fb6f7a9286afcec5c80f6bca49c63c86ad6be6a879f4d06446d41bb8c66f251d6ad9400e7c3d5392ed5a2e6070e67450d690339d76e140468984b007516
7
+ data.tar.gz: 9cab61d72f194b8a66fb4251b107cc3862e07cf580d54c9d5094b71dbe54d041a2a32d8719cc2dc5b38222284c96ab787e9a2449cce4c54f79a46577e9346fbd
data/.travis.yml CHANGED
@@ -1,7 +1,8 @@
1
1
  language: ruby
2
- services:
3
- - cassandra
4
2
  rvm:
5
- - 1.9.2
6
3
  - 1.9.3
4
+ - 2.0.0
7
5
  env: CQLSH=/usr/local/cassandra/bin/cqlsh
6
+ before_install:
7
+ - echo "127.0.0.1 "hostname | sudo tee /etc/hosts
8
+ - sudo service cassandra start
data/Gemfile CHANGED
@@ -3,6 +3,7 @@ gemspec
3
3
 
4
4
  gem 'rake'
5
5
  gem 'mocha'
6
+ gem 'thin'
6
7
 
7
8
  # group :development, :test do
8
9
  # gem 'pry'
@@ -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.1'
5
+ s.version = '4.10.2'
6
6
  s.description = 'Cassandra ActiveModel'
7
7
  s.summary = 'Cassandra ActiveModel'
8
8
  s.authors = ["Michael Koziarski", "gotime"]
@@ -7,12 +7,12 @@ module CassandraObject
7
7
  class_attribute :attribute_definitions
8
8
  self.attribute_definitions = {}
9
9
 
10
- %w(array boolean date float integer json string time).each do |type|
10
+ %w(array boolean date float hash_t integer json string time).each do |type|
11
11
  instance_eval <<-EOV, __FILE__, __LINE__ + 1
12
12
  def #{type}(*args)
13
13
  options = args.extract_options!
14
14
  args.each do |name|
15
- attribute(name, options.merge(:type => :#{type}))
15
+ attribute(name, options.merge(:type => :#{type == 'hash_t' ? 'hash' : type}))
16
16
  end
17
17
  end
18
18
  EOV
@@ -9,11 +9,8 @@ module CassandraObject
9
9
  module ClassMethods
10
10
  def remove(id)
11
11
  statement = "DELETE FROM #{column_family}#{write_option_string} WHERE "
12
- if id.is_a?(Array)
13
- statement += "KEY IN (?)"
14
- else
15
- statement += "KEY = ?"
16
- end
12
+ statement += id.is_a?(Array) ? "KEY IN (?)" : "KEY = ?"
13
+
17
14
  execute_batchable_cql statement, id
18
15
  end
19
16
 
@@ -18,9 +18,17 @@ module CassandraObject
18
18
  end
19
19
 
20
20
  private
21
+
22
+ def scoping
23
+ previous, klass.current_scope = klass.current_scope, self
24
+ yield
25
+ ensure
26
+ klass.current_scope = previous
27
+ end
28
+
21
29
  def method_missing(method_name, *args, &block)
22
30
  if klass.respond_to?(method_name)
23
- klass.send(method_name, *args, &block)
31
+ scoping { klass.send(method_name, *args, &block) }
24
32
  elsif Array.method_defined?(method_name)
25
33
  to_a.send(method_name, *args, &block)
26
34
  else
@@ -12,7 +12,15 @@ module CassandraObject
12
12
 
13
13
  module ClassMethods
14
14
  def scope
15
- Scope.new(self)
15
+ self.current_scope ||= Scope.new(self)
16
+ end
17
+
18
+ def current_scope
19
+ Thread.current["#{self}_current_scope"]
20
+ end
21
+
22
+ def current_scope=(new_scope)
23
+ Thread.current["#{self}_current_scope"] = new_scope
16
24
  end
17
25
  end
18
26
  end
@@ -2,6 +2,7 @@ CassandraObject::Type.register(:array, CassandraObject::Types::ArrayTyp
2
2
  CassandraObject::Type.register(:boolean, CassandraObject::Types::BooleanType)
3
3
  CassandraObject::Type.register(:date, CassandraObject::Types::DateType)
4
4
  CassandraObject::Type.register(:float, CassandraObject::Types::FloatType)
5
+ CassandraObject::Type.register(:hash, CassandraObject::Types::HashType)
5
6
  CassandraObject::Type.register(:integer, CassandraObject::Types::IntegerType)
6
7
  CassandraObject::Type.register(:json, CassandraObject::Types::JsonType)
7
8
  CassandraObject::Type.register(:time, CassandraObject::Types::TimeType)
@@ -0,0 +1,52 @@
1
+ module CassandraObject
2
+ module Types
3
+ class HashType < BaseType
4
+ class DirtyHash < Hash
5
+ attr_accessor :record, :name, :options
6
+ def initialize(record, name, hash, options)
7
+ @record = record
8
+ @name = name.to_s
9
+ @options = options
10
+
11
+ self.merge!(hash)
12
+ @init_hash = self.hash
13
+ @init_value = hash
14
+ end
15
+
16
+ def []=(obj, val)
17
+ modifying { super }
18
+ end
19
+
20
+ def delete(obj)
21
+ modifying { super }
22
+ end
23
+
24
+ private
25
+ def modifying
26
+ result = yield
27
+
28
+ if !record.changed_attributes.key?(name) && @init_hash != self.hash
29
+ record.changed_attributes[name] = @init_value
30
+ end
31
+
32
+ record.send("#{name}=", self)
33
+
34
+ result
35
+ end
36
+ end
37
+
38
+ def encode(hash)
39
+ ActiveSupport::JSON.encode(hash)
40
+ end
41
+
42
+ def decode(str)
43
+ ActiveSupport::JSON.decode(str)
44
+ end
45
+
46
+ def wrap(record, name, value)
47
+ DirtyHash.new(record, name, Hash(value), options)
48
+ end
49
+
50
+ end
51
+ end
52
+ end
@@ -1,40 +1,6 @@
1
1
  module CassandraObject
2
2
  module Types
3
3
  class JsonType < BaseType
4
- class DirtyHash < Hash
5
- attr_accessor :record, :name, :options
6
- def initialize(record, name, hash, options)
7
- @record = record
8
- @name = name.to_s
9
- @options = options
10
-
11
- self.merge!(hash)
12
- @init_hash = self.hash
13
- @init_value = hash
14
- end
15
-
16
- def []=(obj, val)
17
- modifying do super end
18
- end
19
-
20
- def delete(obj)
21
- modifying do super end
22
- end
23
-
24
- private
25
- def modifying
26
- result = yield
27
-
28
- if !record.changed_attributes.key?(name) && @init_hash != self.hash
29
- record.changed_attributes[name] = @init_value
30
- end
31
-
32
- record.send("#{name}=", self)
33
-
34
- result
35
- end
36
- end
37
-
38
4
  def encode(hash)
39
5
  ActiveSupport::JSON.encode(hash)
40
6
  end
@@ -42,11 +8,6 @@ module CassandraObject
42
8
  def decode(str)
43
9
  ActiveSupport::JSON.decode(str)
44
10
  end
45
-
46
- def wrap(record, name, value)
47
- DirtyHash.new(record, name, Hash(value), options)
48
- end
49
-
50
11
  end
51
12
  end
52
13
  end
@@ -53,6 +53,7 @@ module CassandraObject
53
53
  autoload :BooleanType
54
54
  autoload :DateType
55
55
  autoload :FloatType
56
+ autoload :HashType
56
57
  autoload :IntegerType
57
58
  autoload :JsonType
58
59
  autoload :StringType
@@ -2,4 +2,8 @@ class Issue < CassandraObject::Base
2
2
  string :description
3
3
  string :title
4
4
  before_create { self.description ||= 'funny' }
5
+
6
+ def self.for_key key
7
+ where('KEY' => key)
8
+ end
5
9
  end
@@ -30,11 +30,14 @@ class CassandraObject::AttributeMethodsTest < CassandraObject::TestCase
30
30
  assert_equal 'foo', issue.description
31
31
  end
32
32
 
33
- # class ChildIssue < Issue
34
- # end
33
+ class ChildIssue < Issue
34
+ def title=(val)
35
+ self[:title] = val + ' lol'
36
+ end
37
+ end
35
38
 
36
- test 'inheritence' do
37
- issue = Issue.new(title: 'hey')
39
+ test 'override' do
40
+ issue = ChildIssue.new(title: 'hey')
38
41
 
39
42
  assert_equal 'hey lol', issue.title
40
43
  end
@@ -23,4 +23,10 @@ class CassandraObject::Scope::QueryMethodsTest < CassandraObject::TestCase
23
23
  Issue.all
24
24
  end
25
25
  end
26
+
27
+ test "chaining where with scope" do
28
+ issue = Issue.create title: 'abc', description: 'def'
29
+ query = Issue.select(:title).for_key(issue.id)
30
+ assert_match /^SELECT KEY,title/, query.to_cql
31
+ end
26
32
  end
@@ -0,0 +1,77 @@
1
+ require 'test_helper'
2
+
3
+ class CassandraObject::Types::HashTypeTest < CassandraObject::Types::TestCase
4
+ test 'encode' do
5
+ assert_equal({a: 'b'}.to_json, coder.encode(a: 'b'))
6
+ assert_equal '-3', coder.encode(-3)
7
+ end
8
+
9
+ test 'decode' do
10
+ assert_equal({'a' => 'b'}, coder.decode({'a' => 'b'}.to_json))
11
+ end
12
+
13
+ test 'setting marks dirty' do
14
+ record = temp_object do
15
+ string :name
16
+ hash_t :stuff
17
+
18
+ end.new name: 'abcd', stuff: Hash[a: 1, b: 2]
19
+
20
+ record.save!
21
+ assert !record.stuff_changed?
22
+
23
+ record.stuff[:c] = 3
24
+
25
+ assert record.stuff_changed?
26
+ assert_equal Hash[a: 1, b: 2, c: 3], record.stuff
27
+ end
28
+
29
+ test 'hash change marks dirty' do
30
+ record = temp_object do
31
+ string :name
32
+ hash_t :stuff
33
+
34
+ end.new name: 'abcd', stuff: Hash[a: 1, b: 2, v: {}]
35
+
36
+ record.save!
37
+ assert !record.stuff_changed?
38
+
39
+ record.stuff[:v][:data] = 69
40
+ record.stuff[:v] = record.stuff[:v]
41
+
42
+ assert record.stuff_changed?
43
+ assert_equal Hash[a: 1, b: 2, v: Hash[data: 69]], record.stuff
44
+ end
45
+
46
+ test 'hash no change does not dirty' do
47
+ record = temp_object do
48
+ string :name
49
+ hash_t :stuff
50
+
51
+ end.new name: 'abcd', stuff: Hash[a: 1, b: 2, v: {data: 69}]
52
+
53
+ record.save!
54
+ assert !record.stuff_changed?
55
+
56
+ record.stuff[:v][:data] = 69
57
+ record.stuff[:v] = record.stuff[:v]
58
+
59
+ assert !record.stuff_changed?
60
+ assert_equal Hash[a: 1, b: 2, v: Hash[data: 69]], record.stuff
61
+ end
62
+
63
+ test 'delete marks dirty' do
64
+ record = temp_object do
65
+ string :name
66
+ hash_t :stuff
67
+
68
+ end.new name: 'abcd', stuff: Hash[a: 1, b: 2]
69
+
70
+ record.stuff.delete :b
71
+
72
+ assert record.stuff_changed?
73
+ assert_equal Hash[a: 1], record.stuff
74
+ end
75
+
76
+
77
+ end
@@ -10,67 +10,13 @@ class CassandraObject::Types::JsonTypeTest < CassandraObject::Types::TestCase
10
10
  assert_equal({'a' => 'b'}, coder.decode({'a' => 'b'}.to_json))
11
11
  end
12
12
 
13
- test 'setting marks dirty' do
14
- record = temp_object do
15
- string :name
16
- json :stuff
17
-
18
- end.new name: 'abcd', stuff: Hash[a: 1, b: 2]
19
-
20
- record.save!
21
- assert !record.stuff_changed?
22
-
23
- record.stuff[:c] = 3
24
-
25
- assert record.stuff_changed?
26
- assert_equal Hash[a: 1, b: 2, c: 3], record.stuff
27
- end
28
-
29
- test 'hash change marks dirty' do
30
- record = temp_object do
31
- string :name
32
- json :stuff
33
-
34
- end.new name: 'abcd', stuff: Hash[a: 1, b: 2, v: {}]
35
-
36
- record.save!
37
- assert !record.stuff_changed?
38
-
39
- record.stuff[:v][:data] = 69
40
- record.stuff[:v] = record.stuff[:v]
41
-
42
- assert record.stuff_changed?
43
- assert_equal Hash[a: 1, b: 2, v: Hash[data: 69]], record.stuff
44
- end
45
-
46
- test 'hash no change does not dirty' do
47
- record = temp_object do
48
- string :name
49
- json :stuff
50
-
51
- end.new name: 'abcd', stuff: Hash[a: 1, b: 2, v: {data: 69}]
52
-
53
- record.save!
54
- assert !record.stuff_changed?
55
-
56
- record.stuff[:v][:data] = 69
57
- record.stuff[:v] = record.stuff[:v]
58
-
59
- assert !record.stuff_changed?
60
- assert_equal Hash[a: 1, b: 2, v: Hash[data: 69]], record.stuff
13
+ test 'encode array' do
14
+ assert_equal(['a', 'b'].to_json, coder.encode(['a', 'b']))
15
+ assert_equal '-3', coder.encode(-3)
61
16
  end
62
17
 
63
- test 'delete marks dirty' do
64
- record = temp_object do
65
- string :name
66
- json :stuff
67
-
68
- end.new name: 'abcd', stuff: Hash[a: 1, b: 2]
69
-
70
- record.stuff.delete :b
71
-
72
- assert record.stuff_changed?
73
- assert_equal Hash[a: 1], record.stuff
18
+ test 'decode array' do
19
+ assert_equal(['a', 'b'], coder.decode(['a', 'b'].to_json))
74
20
  end
75
21
 
76
22
 
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.1
4
+ version: 4.10.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Koziarski
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-09 00:00:00.000000000 Z
12
+ date: 2013-10-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -122,6 +122,7 @@ files:
122
122
  - lib/cassandra_object/types/boolean_type.rb
123
123
  - lib/cassandra_object/types/date_type.rb
124
124
  - lib/cassandra_object/types/float_type.rb
125
+ - lib/cassandra_object/types/hash_type.rb
125
126
  - lib/cassandra_object/types/integer_type.rb
126
127
  - lib/cassandra_object/types/json_type.rb
127
128
  - lib/cassandra_object/types/string_type.rb
@@ -163,6 +164,7 @@ files:
163
164
  - test/unit/types/boolean_type_test.rb
164
165
  - test/unit/types/date_type_test.rb
165
166
  - test/unit/types/float_type_test.rb
167
+ - test/unit/types/hash_type_test.rb
166
168
  - test/unit/types/integer_type_test.rb
167
169
  - test/unit/types/json_type_test.rb
168
170
  - test/unit/types/string_type_test.rb