gotime-cassandra_object 2.6.4 → 2.7.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/attributes.rb +8 -7
- data/lib/cassandra_object/base.rb +7 -1
- data/lib/cassandra_object/persistence.rb +8 -11
- data/lib/cassandra_object/types/array_type.rb +2 -2
- data/lib/cassandra_object/types/base_type.rb +2 -2
- data/lib/cassandra_object/types/date_type.rb +0 -1
- data/test/base_test.rb +8 -1
- data/test/dirty_test.rb +12 -2
- data/test/types/array_type_test.rb +0 -4
- data/test/types/base_type_test.rb +3 -2
- metadata +7 -7
@@ -8,8 +8,9 @@ module CassandraObject
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def instantiate(record, value)
|
11
|
-
|
12
|
-
|
11
|
+
value ||= coder.default
|
12
|
+
return unless value
|
13
|
+
|
13
14
|
value = value.kind_of?(expected_type) ? value : coder.decode(value)
|
14
15
|
coder.wrap(record, name, value)
|
15
16
|
end
|
@@ -21,7 +22,7 @@ module CassandraObject
|
|
21
22
|
|
22
23
|
included do
|
23
24
|
class_attribute :model_attributes
|
24
|
-
self.model_attributes = {}
|
25
|
+
self.model_attributes = {}
|
25
26
|
|
26
27
|
attribute_method_suffix("", "=")
|
27
28
|
|
@@ -56,15 +57,15 @@ module CassandraObject
|
|
56
57
|
type_mapping = CassandraObject::Type::TypeMapping.new(expected_type, coder)
|
57
58
|
end
|
58
59
|
|
59
|
-
model_attributes[name] = Attribute.new(name, type_mapping, options)
|
60
|
+
model_attributes[name.to_sym] = Attribute.new(name, type_mapping, options)
|
60
61
|
end
|
61
62
|
|
62
|
-
def json(name)
|
63
|
-
attribute(name, type: :hash)
|
63
|
+
def json(name, options = {})
|
64
|
+
attribute(name, options.update(type: :hash))
|
64
65
|
end
|
65
66
|
|
66
67
|
def instantiate_attribute(record, name, value)
|
67
|
-
if model_attribute = model_attributes[name]
|
68
|
+
if model_attribute = model_attributes[name.to_sym]
|
68
69
|
model_attribute.instantiate(record, value)
|
69
70
|
else
|
70
71
|
raise NoMethodError, "Unknown attribute #{name.inspect}"
|
@@ -52,8 +52,14 @@ module CassandraObject
|
|
52
52
|
@key = attributes.delete(:key)
|
53
53
|
@new_record = true
|
54
54
|
@destroyed = false
|
55
|
-
@attributes = {}
|
55
|
+
@attributes = {}
|
56
56
|
self.attributes = attributes
|
57
|
+
model_attributes.each do |k, model_attribute|
|
58
|
+
unless read_attribute(k)
|
59
|
+
write_attribute(k, model_attribute.instantiate(self, nil))
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
57
63
|
@schema_version = self.class.current_schema_version
|
58
64
|
end
|
59
65
|
|
@@ -23,7 +23,7 @@ module CassandraObject
|
|
23
23
|
|
24
24
|
def write(key, attributes, schema_version)
|
25
25
|
key.tap do |key|
|
26
|
-
attributes =
|
26
|
+
attributes = encode_attributes(attributes, schema_version)
|
27
27
|
ActiveSupport::Notifications.instrument("insert.cassandra_object", column_family: column_family, key: key, attributes: attributes) do
|
28
28
|
connection.insert(column_family, key.to_s, attributes, consistency: thrift_write_consistency)
|
29
29
|
end
|
@@ -31,31 +31,28 @@ module CassandraObject
|
|
31
31
|
end
|
32
32
|
|
33
33
|
def instantiate(key, attributes)
|
34
|
-
# remove any attributes we don't know about. we would do this earlier, but we want to make such
|
35
|
-
# attributes available to migrations
|
36
|
-
attributes.delete_if { |k,_| model_attributes[k].nil? }
|
37
|
-
|
38
34
|
allocate.tap do |object|
|
39
35
|
object.instance_variable_set("@schema_version", attributes.delete('schema_version'))
|
40
|
-
object.instance_variable_set("@key", parse_key(key))
|
36
|
+
object.instance_variable_set("@key", parse_key(key)) if key
|
41
37
|
object.instance_variable_set("@new_record", false)
|
42
38
|
object.instance_variable_set("@destroyed", false)
|
43
|
-
object.instance_variable_set("@attributes",
|
39
|
+
object.instance_variable_set("@attributes", instantiate_attributes(object, attributes))
|
44
40
|
end
|
45
41
|
end
|
46
42
|
|
47
|
-
def
|
43
|
+
def encode_attributes(attributes, schema_version)
|
48
44
|
attributes.inject({}) do |memo, (column_name, value)|
|
49
45
|
# cassandra stores bytes, not strings, so it has no concept of encodings. The ruby thrift gem
|
50
46
|
# expects all strings to be encoded as ascii-8bit.
|
51
47
|
# don't attempt to encode columns that are nil
|
52
|
-
memo[column_name.to_s] = value.nil? ? '' : model_attributes[column_name].coder.encode(value).force_encoding('ASCII-8BIT')
|
48
|
+
memo[column_name.to_s] = value.nil? ? '' : model_attributes[column_name.to_sym].coder.encode(value).force_encoding('ASCII-8BIT')
|
53
49
|
memo
|
54
50
|
end.merge({"schema_version" => schema_version.to_s})
|
55
51
|
end
|
56
52
|
|
57
|
-
def
|
58
|
-
|
53
|
+
def instantiate_attributes(object, attributes)
|
54
|
+
attributes = attributes.symbolize_keys
|
55
|
+
Hash[model_attributes.map { |k, model_attribute| [k.to_s, model_attribute.instantiate(object, attributes[k])] }]
|
59
56
|
end
|
60
57
|
end
|
61
58
|
|
data/test/base_test.rb
CHANGED
@@ -6,7 +6,7 @@ class CassandraObject::BaseTest < CassandraObject::TestCase
|
|
6
6
|
|
7
7
|
class Grandson < Son
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
test 'base_class' do
|
11
11
|
assert_equal Son, Son.base_class
|
12
12
|
assert_equal Son, Grandson.base_class
|
@@ -16,6 +16,13 @@ class CassandraObject::BaseTest < CassandraObject::TestCase
|
|
16
16
|
assert_equal 'CassandraObject::BaseTest::Sons', Son.column_family
|
17
17
|
end
|
18
18
|
|
19
|
+
test 'initialiaze' do
|
20
|
+
issue = Issue.new
|
21
|
+
|
22
|
+
assert issue.new_record?
|
23
|
+
assert !issue.destroyed?
|
24
|
+
end
|
25
|
+
|
19
26
|
test 'to_param' do
|
20
27
|
issue = Issue.create
|
21
28
|
assert_equal issue.id, issue.to_param
|
data/test/dirty_test.rb
CHANGED
@@ -8,10 +8,20 @@ class CassandraObject::DirtyTest < CassandraObject::TestCase
|
|
8
8
|
end
|
9
9
|
|
10
10
|
test 'save clears dirty' do
|
11
|
-
record = TestRecord.new
|
11
|
+
record = TestRecord.new name: 'foo'
|
12
12
|
assert record.changed?
|
13
13
|
|
14
|
-
record.save
|
14
|
+
record.save!
|
15
|
+
|
16
|
+
assert !record.changed?
|
17
|
+
end
|
18
|
+
|
19
|
+
test 'reload clears dirty' do
|
20
|
+
record = TestRecord.create! name: 'foo'
|
21
|
+
record.name = 'bar'
|
22
|
+
assert record.changed?
|
23
|
+
|
24
|
+
record.reload
|
15
25
|
|
16
26
|
assert !record.changed?
|
17
27
|
end
|
@@ -1,8 +1,9 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class CassandraObject::Types::BaseTypeTest < CassandraObject::Types::TestCase
|
4
|
-
test '
|
5
|
-
assert_equal
|
4
|
+
test 'default' do
|
5
|
+
assert_equal nil, coder.default
|
6
|
+
assert_equal '5', CassandraObject::Types::BaseType.new(default: '5').default
|
6
7
|
end
|
7
8
|
|
8
9
|
test 'encode' do
|
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.7.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -14,7 +14,7 @@ date: 2011-08-30 00:00:00.000000000Z
|
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activemodel
|
17
|
-
requirement: &
|
17
|
+
requirement: &70185349554920 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
20
|
- - ~>
|
@@ -22,10 +22,10 @@ dependencies:
|
|
22
22
|
version: '3.0'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
|
-
version_requirements: *
|
25
|
+
version_requirements: *70185349554920
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: cassandra
|
28
|
-
requirement: &
|
28
|
+
requirement: &70185349554140 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ~>
|
@@ -33,10 +33,10 @@ dependencies:
|
|
33
33
|
version: 0.12.0
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *70185349554140
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
38
|
name: bundler
|
39
|
-
requirement: &
|
39
|
+
requirement: &70185349553500 !ruby/object:Gem::Requirement
|
40
40
|
none: false
|
41
41
|
requirements:
|
42
42
|
- - ~>
|
@@ -44,7 +44,7 @@ dependencies:
|
|
44
44
|
version: 1.0.0
|
45
45
|
type: :development
|
46
46
|
prerelease: false
|
47
|
-
version_requirements: *
|
47
|
+
version_requirements: *70185349553500
|
48
48
|
description: Cassandra ActiveModel
|
49
49
|
email: gems@gotime.com
|
50
50
|
executables: []
|