gotime-cassandra_object 2.11.9 → 2.12.0
Sign up to get free protection for your applications and to get access to all the features.
- data/gotime-cassandra_object.gemspec +1 -1
- data/lib/cassandra_object/attribute_methods/primary_key.rb +3 -4
- data/lib/cassandra_object/finder_methods.rb +8 -10
- data/lib/cassandra_object/generators/templates/migration.rb.erb +1 -1
- data/lib/cassandra_object/identity.rb +10 -38
- data/lib/cassandra_object/persistence.rb +5 -5
- data/test/unit/attribute_methods/primary_key_test.rb +10 -6
- data/test/unit/finder_methods_test.rb +2 -3
- data/test/unit/identity_test.rb +12 -8
- metadata +2 -8
- data/lib/cassandra_object/identity/abstract_key_factory.rb +0 -26
- data/lib/cassandra_object/identity/custom_key_factory.rb +0 -36
- data/lib/cassandra_object/identity/hashed_natural_key_factory.rb +0 -10
- data/lib/cassandra_object/identity/natural_key_factory.rb +0 -37
- data/lib/cassandra_object/identity/uuid_key_factory.rb +0 -23
- data/test/unit/identity/uuid_key_factory_test.rb +0 -14
@@ -3,20 +3,18 @@ module CassandraObject
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
module ClassMethods
|
6
|
-
def find(
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
elsif attributes = connection.get(column_family, key_string, {:count => 500}).presence
|
12
|
-
instantiate(key_string, attributes)
|
6
|
+
def find(id)
|
7
|
+
if id.blank?
|
8
|
+
raise CassandraObject::RecordNotFound, "Couldn't find #{self.name} with key #{id.inspect}"
|
9
|
+
elsif attributes = connection.get(column_family, id, {:count => 500}).presence
|
10
|
+
instantiate(id, attributes)
|
13
11
|
else
|
14
12
|
raise CassandraObject::RecordNotFound
|
15
13
|
end
|
16
14
|
end
|
17
15
|
|
18
|
-
def find_by_id(
|
19
|
-
find(
|
16
|
+
def find_by_id(id)
|
17
|
+
find(id)
|
20
18
|
rescue CassandraObject::RecordNotFound
|
21
19
|
nil
|
22
20
|
end
|
@@ -55,7 +53,7 @@ module CassandraObject
|
|
55
53
|
end
|
56
54
|
|
57
55
|
Hash[attribute_results.map do |key, attributes|
|
58
|
-
[
|
56
|
+
[key, attributes.present? ? instantiate(key, attributes) : nil]
|
59
57
|
end]
|
60
58
|
end
|
61
59
|
end
|
@@ -1,52 +1,24 @@
|
|
1
1
|
module CassandraObject
|
2
2
|
module Identity
|
3
3
|
extend ActiveSupport::Concern
|
4
|
-
extend ActiveSupport::Autoload
|
5
|
-
|
6
|
-
autoload :Key
|
7
|
-
autoload :AbstractKeyFactory
|
8
|
-
autoload :UUIDKeyFactory
|
9
|
-
autoload :NaturalKeyFactory
|
10
|
-
autoload :HashedNaturalKeyFactory
|
11
|
-
autoload :CustomKeyFactory
|
12
4
|
|
13
5
|
included do
|
14
|
-
class_attribute :
|
15
|
-
key :uuid
|
16
|
-
end
|
6
|
+
class_attribute :key_generator
|
17
7
|
|
18
|
-
|
19
|
-
|
20
|
-
#
|
21
|
-
# @param [:uuid, :natural] the type of key
|
22
|
-
# @param the options you want to pass along to the key factory (like :attributes => :name, for a natural key).
|
23
|
-
#
|
24
|
-
def key(name_or_factory = :uuid, *options)
|
25
|
-
self.key_factory = case name_or_factory
|
26
|
-
when :uuid
|
27
|
-
UUIDKeyFactory.new
|
28
|
-
when :natural
|
29
|
-
NaturalKeyFactory.new(*options)
|
30
|
-
when :custom
|
31
|
-
CustomKeyFactory.new(*options)
|
32
|
-
else
|
33
|
-
name_or_factory
|
34
|
-
end
|
8
|
+
key do
|
9
|
+
SimpleUUID::UUID.new.to_guid
|
35
10
|
end
|
11
|
+
end
|
36
12
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
13
|
+
module ClassMethods
|
14
|
+
# Define a key generator. Default is UUID.
|
15
|
+
def key(&block)
|
16
|
+
self.key_generator = block
|
41
17
|
end
|
42
18
|
|
43
|
-
def
|
44
|
-
|
19
|
+
def _generate_key(object)
|
20
|
+
object.instance_eval(&key_generator)
|
45
21
|
end
|
46
22
|
end
|
47
|
-
|
48
|
-
def key
|
49
|
-
@key ||= self.class.next_key(self)
|
50
|
-
end
|
51
23
|
end
|
52
24
|
end
|
@@ -33,7 +33,7 @@ module CassandraObject
|
|
33
33
|
|
34
34
|
def instantiate(key, attributes)
|
35
35
|
allocate.tap do |object|
|
36
|
-
object.instance_variable_set("@
|
36
|
+
object.instance_variable_set("@id", key) if key
|
37
37
|
object.instance_variable_set("@new_record", false)
|
38
38
|
object.instance_variable_set("@destroyed", false)
|
39
39
|
object.instance_variable_set("@attributes", typecast_attributes(object, attributes))
|
@@ -82,7 +82,7 @@ module CassandraObject
|
|
82
82
|
end
|
83
83
|
|
84
84
|
def destroy
|
85
|
-
self.class.remove(
|
85
|
+
self.class.remove(id)
|
86
86
|
@destroyed = true
|
87
87
|
freeze
|
88
88
|
end
|
@@ -104,7 +104,7 @@ module CassandraObject
|
|
104
104
|
end
|
105
105
|
|
106
106
|
def reload
|
107
|
-
@attributes.update(self.class.find(
|
107
|
+
@attributes.update(self.class.find(id).instance_variable_get('@attributes'))
|
108
108
|
end
|
109
109
|
|
110
110
|
private
|
@@ -116,7 +116,7 @@ module CassandraObject
|
|
116
116
|
def create
|
117
117
|
write
|
118
118
|
@new_record = false
|
119
|
-
|
119
|
+
id
|
120
120
|
end
|
121
121
|
|
122
122
|
def update
|
@@ -125,7 +125,7 @@ module CassandraObject
|
|
125
125
|
|
126
126
|
def write
|
127
127
|
changed_attributes = changed.inject({}) { |h, n| h[n] = read_attribute(n); h }
|
128
|
-
self.class.write(
|
128
|
+
self.class.write(id, changed_attributes)
|
129
129
|
end
|
130
130
|
end
|
131
131
|
end
|
@@ -1,17 +1,21 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
class CassandraObject::AttributeMethods::
|
3
|
+
class CassandraObject::AttributeMethods::PrimaryKeyTest < CassandraObject::TestCase
|
4
4
|
test 'get id' do
|
5
|
-
|
5
|
+
model = temp_object do
|
6
|
+
key do
|
7
|
+
"foo"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
record = model.new
|
6
11
|
|
7
|
-
assert_equal
|
12
|
+
assert_equal 'foo', record.id
|
8
13
|
end
|
9
14
|
|
10
15
|
test 'set id' do
|
11
|
-
|
12
|
-
issue = Issue.new id: uuid
|
16
|
+
issue = Issue.new id: 'foo'
|
13
17
|
|
14
|
-
assert_equal issue.
|
18
|
+
assert_equal 'foo', issue.id
|
15
19
|
end
|
16
20
|
|
17
21
|
test 'attributes' do
|
@@ -4,7 +4,6 @@ class CassandraObject::FinderMethodsTest < CassandraObject::TestCase
|
|
4
4
|
test 'find' do
|
5
5
|
Issue.create.tap do |issue|
|
6
6
|
assert_equal issue, Issue.find(issue.id)
|
7
|
-
assert_equal issue, Issue.find(issue.key)
|
8
7
|
end
|
9
8
|
|
10
9
|
begin
|
@@ -47,7 +46,7 @@ class CassandraObject::FinderMethodsTest < CassandraObject::TestCase
|
|
47
46
|
third_issue = Issue.create
|
48
47
|
|
49
48
|
assert_equal [], Issue.find_with_ids([])
|
50
|
-
assert_equal [first_issue, second_issue].to_set, Issue.find_with_ids(first_issue.
|
51
|
-
assert_equal [first_issue, second_issue].to_set, Issue.find_with_ids([first_issue.
|
49
|
+
assert_equal [first_issue, second_issue].to_set, Issue.find_with_ids(first_issue.id, second_issue.id).to_set
|
50
|
+
assert_equal [first_issue, second_issue].to_set, Issue.find_with_ids([first_issue.id, second_issue.id]).to_set
|
52
51
|
end
|
53
52
|
end
|
data/test/unit/identity_test.rb
CHANGED
@@ -5,18 +5,22 @@ class CassandraObject::IdentityTest < CassandraObject::TestCase
|
|
5
5
|
assert_equal 'id', Issue.primary_key
|
6
6
|
end
|
7
7
|
|
8
|
-
test '
|
8
|
+
test 'default _generate_key' do
|
9
9
|
issue = Issue.new
|
10
10
|
|
11
|
-
assert_not_nil issue
|
11
|
+
assert_not_nil Issue._generate_key(issue)
|
12
12
|
end
|
13
13
|
|
14
|
-
test '
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
14
|
+
test 'custom key' do
|
15
|
+
model = temp_object do
|
16
|
+
key do
|
17
|
+
"name:#{name}"
|
18
|
+
end
|
19
|
+
attr_accessor :name
|
20
|
+
end
|
21
|
+
record = model.new
|
22
|
+
record.name = 'bar'
|
19
23
|
|
20
|
-
|
24
|
+
assert_equal 'name:bar', model._generate_key(record)
|
21
25
|
end
|
22
26
|
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: 2.
|
4
|
+
version: 2.12.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-05-
|
13
|
+
date: 2012-05-19 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activemodel
|
@@ -108,11 +108,6 @@ files:
|
|
108
108
|
- lib/cassandra_object/generators/migration_generator.rb
|
109
109
|
- lib/cassandra_object/generators/templates/migration.rb.erb
|
110
110
|
- lib/cassandra_object/identity.rb
|
111
|
-
- lib/cassandra_object/identity/abstract_key_factory.rb
|
112
|
-
- lib/cassandra_object/identity/custom_key_factory.rb
|
113
|
-
- lib/cassandra_object/identity/hashed_natural_key_factory.rb
|
114
|
-
- lib/cassandra_object/identity/natural_key_factory.rb
|
115
|
-
- lib/cassandra_object/identity/uuid_key_factory.rb
|
116
111
|
- lib/cassandra_object/inspect.rb
|
117
112
|
- lib/cassandra_object/log_subscriber.rb
|
118
113
|
- lib/cassandra_object/migrations.rb
|
@@ -160,7 +155,6 @@ files:
|
|
160
155
|
- test/unit/connection_test.rb
|
161
156
|
- test/unit/consistency_test.rb
|
162
157
|
- test/unit/finder_methods_test.rb
|
163
|
-
- test/unit/identity/uuid_key_factory_test.rb
|
164
158
|
- test/unit/identity_test.rb
|
165
159
|
- test/unit/inspect_test.rb
|
166
160
|
- test/unit/persistence_test.rb
|
@@ -1,26 +0,0 @@
|
|
1
|
-
module CassandraObject
|
2
|
-
module Identity
|
3
|
-
# Key factories need to support 3 operations
|
4
|
-
class AbstractKeyFactory
|
5
|
-
# Next key takes an object and returns the key object it should use.
|
6
|
-
# object will be ignored with synthetic keys but could be useful with natural ones
|
7
|
-
#
|
8
|
-
# @param [CassandraObject::Base] the object that needs a new key
|
9
|
-
# @return [CassandraObject::Identity::Key] the key
|
10
|
-
#
|
11
|
-
def next_key(object)
|
12
|
-
raise NotImplementedError, "#{self.class.name}#next_key isn't implemented."
|
13
|
-
end
|
14
|
-
|
15
|
-
# Parse should create a new key object from the 'to_param' format
|
16
|
-
#
|
17
|
-
# @param [String] the result of calling key.to_param
|
18
|
-
# @return [CassandraObject::Identity::Key] the parsed key
|
19
|
-
#
|
20
|
-
def parse(string)
|
21
|
-
raise NotImplementedError, "#{self.class.name}#parse isn't implemented."
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
@@ -1,36 +0,0 @@
|
|
1
|
-
module CassandraObject
|
2
|
-
module Identity
|
3
|
-
class CustomKeyFactory < AbstractKeyFactory
|
4
|
-
class CustomKey
|
5
|
-
attr_reader :value
|
6
|
-
|
7
|
-
def initialize(value)
|
8
|
-
@value = value
|
9
|
-
end
|
10
|
-
|
11
|
-
def to_s
|
12
|
-
value
|
13
|
-
end
|
14
|
-
|
15
|
-
def ==(other)
|
16
|
-
other.to_s == value
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
attr_reader :method
|
21
|
-
|
22
|
-
def initialize(options)
|
23
|
-
@method = options[:method]
|
24
|
-
end
|
25
|
-
|
26
|
-
def next_key(object)
|
27
|
-
CustomKey.new(object.send(@method))
|
28
|
-
end
|
29
|
-
|
30
|
-
def parse(value)
|
31
|
-
value
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
@@ -1,10 +0,0 @@
|
|
1
|
-
require 'digest/sha1'
|
2
|
-
module CassandraObject
|
3
|
-
module Identity
|
4
|
-
class HashedNaturalKeyFactory < NaturalKeyFactory
|
5
|
-
def next_key(object)
|
6
|
-
NaturalKey.new(Digest::SHA1.hexdigest(attributes.map { |attribute| object[attribute] }.join(separator)))
|
7
|
-
end
|
8
|
-
end
|
9
|
-
end
|
10
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
module CassandraObject
|
2
|
-
module Identity
|
3
|
-
class NaturalKeyFactory < AbstractKeyFactory
|
4
|
-
class NaturalKey
|
5
|
-
attr_reader :value
|
6
|
-
|
7
|
-
def initialize(value)
|
8
|
-
@value = value
|
9
|
-
end
|
10
|
-
|
11
|
-
def to_s
|
12
|
-
value
|
13
|
-
end
|
14
|
-
|
15
|
-
def ==(other)
|
16
|
-
other.to_s == to_s
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
attr_reader :attributes, :separator
|
21
|
-
|
22
|
-
def initialize(options)
|
23
|
-
@attributes = [*options[:attributes]]
|
24
|
-
@separator = options[:separator] || "-"
|
25
|
-
end
|
26
|
-
|
27
|
-
def next_key(object)
|
28
|
-
NaturalKey.new(attributes.map { |a| object.attributes[a.to_s] }.join(separator))
|
29
|
-
end
|
30
|
-
|
31
|
-
def parse(paramized_key)
|
32
|
-
NaturalKey.new(paramized_key)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
@@ -1,23 +0,0 @@
|
|
1
|
-
module CassandraObject
|
2
|
-
module Identity
|
3
|
-
# Key factories need to support 3 operations
|
4
|
-
class UUIDKeyFactory < AbstractKeyFactory
|
5
|
-
class UUID < SimpleUUID::UUID
|
6
|
-
def to_s
|
7
|
-
to_guid
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
def next_key(object)
|
12
|
-
UUID.new
|
13
|
-
end
|
14
|
-
|
15
|
-
def parse(string)
|
16
|
-
UUID.new(string) if string
|
17
|
-
rescue
|
18
|
-
nil
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
@@ -1,14 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class CassandraObject::Identity::UuidKeyFactoryTest < CassandraObject::TestCase
|
4
|
-
test 'parse' do
|
5
|
-
assert_not_nil key_factory.parse('95b2cf70-c318-11e0-962b-0800200c9a66')
|
6
|
-
assert_nil key_factory.parse('xyz')
|
7
|
-
assert_nil key_factory.parse(nil)
|
8
|
-
end
|
9
|
-
|
10
|
-
private
|
11
|
-
def key_factory
|
12
|
-
@key_factory ||= CassandraObject::Identity::UUIDKeyFactory.new
|
13
|
-
end
|
14
|
-
end
|