gotime-cassandra_object 2.2.4 → 2.3.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 -8
- data/lib/cassandra_object/persistence.rb +2 -2
- data/lib/cassandra_object/type.rb +3 -3
- data/lib/cassandra_object/types/array_type.rb +1 -3
- data/lib/cassandra_object/types/boolean_type.rb +1 -3
- data/lib/cassandra_object/types/date_type.rb +1 -3
- data/lib/cassandra_object/types/float_type.rb +1 -3
- data/lib/cassandra_object/types/hash_type.rb +1 -3
- data/lib/cassandra_object/types/integer_type.rb +1 -3
- data/lib/cassandra_object/types/set_type.rb +1 -3
- data/lib/cassandra_object/types/string_type.rb +1 -3
- data/lib/cassandra_object/types/time_type.rb +1 -3
- data/lib/cassandra_object/types/time_with_zone_type.rb +1 -3
- data/lib/cassandra_object/types/utf8_string_type.rb +1 -3
- data/test/test_helper.rb +9 -0
- data/test/types/array_type_test.rb +4 -4
- data/test/types/boolean_type_test.rb +12 -12
- data/test/types/date_type_test.rb +1 -1
- data/test/types/float_type_test.rb +1 -1
- data/test/types/hash_type_test.rb +1 -1
- data/test/types/integer_type_test.rb +7 -7
- data/test/types/set_type_test.rb +6 -6
- data/test/types/string_type_test.rb +1 -1
- data/test/types/time_type_test.rb +1 -1
- data/test/types/utf8_string_type_test.rb +1 -1
- metadata +8 -8
@@ -1,15 +1,15 @@
|
|
1
1
|
module CassandraObject
|
2
2
|
class Attribute
|
3
|
-
attr_reader :name, :
|
4
|
-
def initialize(name,
|
3
|
+
attr_reader :name, :coder, :expected_type
|
4
|
+
def initialize(name, coder, expected_type)
|
5
5
|
@name = name.to_s
|
6
|
-
@
|
6
|
+
@coder = coder
|
7
7
|
@expected_type = expected_type
|
8
8
|
end
|
9
9
|
|
10
10
|
def check_value!(value)
|
11
11
|
return value if value.nil?
|
12
|
-
value.kind_of?(expected_type) ? value :
|
12
|
+
value.kind_of?(expected_type) ? value : coder.decode(value)
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
@@ -24,16 +24,16 @@ module CassandraObject
|
|
24
24
|
end
|
25
25
|
|
26
26
|
if type_mapping = CassandraObject::Type.get_mapping(options[:type])
|
27
|
-
|
27
|
+
coder = type_mapping.coder
|
28
28
|
expected_type = type_mapping.expected_type
|
29
|
-
elsif options[:
|
30
|
-
|
29
|
+
elsif options[:coder]
|
30
|
+
coder = options[:coder]
|
31
31
|
expected_type = options[:type]
|
32
32
|
else
|
33
33
|
raise "Unknown type #{options[:type]}"
|
34
34
|
end
|
35
35
|
|
36
|
-
model_attributes[name] = Attribute.new(name,
|
36
|
+
model_attributes[name] = Attribute.new(name, coder, expected_type)
|
37
37
|
end
|
38
38
|
|
39
39
|
def define_attribute_methods
|
@@ -49,13 +49,13 @@ module CassandraObject
|
|
49
49
|
# cassandra stores bytes, not strings, so it has no concept of encodings. The ruby thrift gem
|
50
50
|
# expects all strings to be encoded as ascii-8bit.
|
51
51
|
# don't attempt to encode columns that are nil
|
52
|
-
memo[column_name.to_s] = value.nil? ? '' : model_attributes[column_name].
|
52
|
+
memo[column_name.to_s] = value.nil? ? '' : model_attributes[column_name].coder.encode(value).force_encoding('ASCII-8BIT')
|
53
53
|
memo
|
54
54
|
end.merge({"schema_version" => schema_version.to_s})
|
55
55
|
end
|
56
56
|
|
57
57
|
def decode_columns_hash(attributes)
|
58
|
-
Hash[attributes.map { |k, v| [k.to_s, model_attributes[k].
|
58
|
+
Hash[attributes.map { |k, v| [k.to_s, model_attributes[k].coder.decode(v)] }]
|
59
59
|
end
|
60
60
|
|
61
61
|
def column_family_configuration
|
@@ -1,14 +1,14 @@
|
|
1
1
|
module CassandraObject
|
2
2
|
class Type
|
3
|
-
class TypeMapping < Struct.new(:expected_type, :
|
3
|
+
class TypeMapping < Struct.new(:expected_type, :coder)
|
4
4
|
end
|
5
5
|
|
6
6
|
cattr_accessor :attribute_types
|
7
7
|
self.attribute_types = {}.with_indifferent_access
|
8
8
|
|
9
9
|
class << self
|
10
|
-
def register(name, expected_type,
|
11
|
-
attribute_types[name] = TypeMapping.new(expected_type,
|
10
|
+
def register(name, expected_type, coder)
|
11
|
+
attribute_types[name] = TypeMapping.new(expected_type, coder.new)
|
12
12
|
end
|
13
13
|
|
14
14
|
def get_mapping(name)
|
@@ -1,16 +1,14 @@
|
|
1
1
|
module CassandraObject
|
2
2
|
module Types
|
3
|
-
|
3
|
+
class ArrayType
|
4
4
|
def encode(array)
|
5
5
|
raise ArgumentError.new("#{self} requires an Array") unless array.kind_of?(Array)
|
6
6
|
array.to_json
|
7
7
|
end
|
8
|
-
module_function :encode
|
9
8
|
|
10
9
|
def decode(str)
|
11
10
|
ActiveSupport::JSON.decode(str)
|
12
11
|
end
|
13
|
-
module_function :decode
|
14
12
|
end
|
15
13
|
end
|
16
14
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module CassandraObject
|
2
2
|
module Types
|
3
|
-
|
3
|
+
class BooleanType
|
4
4
|
TRUE_VALS = [true, 'true', '1']
|
5
5
|
FALSE_VALS = [false, 'false', '0', '', nil]
|
6
6
|
VALID_VALS = TRUE_VALS + FALSE_VALS
|
@@ -11,13 +11,11 @@ module CassandraObject
|
|
11
11
|
end
|
12
12
|
TRUE_VALS.include?(bool) ? '1' : '0'
|
13
13
|
end
|
14
|
-
module_function :encode
|
15
14
|
|
16
15
|
def decode(str)
|
17
16
|
raise ArgumentError.new("Cannot convert #{str} into a boolean") unless VALID_VALS.include?(str)
|
18
17
|
TRUE_VALS.include?(str)
|
19
18
|
end
|
20
|
-
module_function :decode
|
21
19
|
end
|
22
20
|
end
|
23
21
|
end
|
@@ -1,20 +1,18 @@
|
|
1
1
|
module CassandraObject
|
2
2
|
module Types
|
3
|
-
|
3
|
+
class DateType
|
4
4
|
FORMAT = '%Y-%m-%d'
|
5
5
|
REGEX = /\A\d{4}-\d{2}-\d{2}\Z/
|
6
6
|
def encode(date)
|
7
7
|
raise ArgumentError.new("#{self} requires a Date") unless date.kind_of?(Date)
|
8
8
|
date.strftime(FORMAT)
|
9
9
|
end
|
10
|
-
module_function :encode
|
11
10
|
|
12
11
|
def decode(str)
|
13
12
|
return nil if str.empty?
|
14
13
|
raise ArgumentError.new("Cannot convert #{str} into a Date") unless str.kind_of?(String) && str.match(REGEX)
|
15
14
|
Date.strptime(str, FORMAT)
|
16
15
|
end
|
17
|
-
module_function :decode
|
18
16
|
end
|
19
17
|
end
|
20
18
|
end
|
@@ -1,19 +1,17 @@
|
|
1
1
|
module CassandraObject
|
2
2
|
module Types
|
3
|
-
|
3
|
+
class FloatType
|
4
4
|
REGEX = /\A[-+]?\d+(\.\d+)?\Z/
|
5
5
|
def encode(float)
|
6
6
|
raise ArgumentError.new("#{self} requires a Float") unless float.kind_of?(Float)
|
7
7
|
float.to_s
|
8
8
|
end
|
9
|
-
module_function :encode
|
10
9
|
|
11
10
|
def decode(str)
|
12
11
|
return nil if str.empty?
|
13
12
|
raise ArgumentError.new("Cannot convert #{str} into a Float") unless str.kind_of?(String) && str.match(REGEX)
|
14
13
|
str.to_f
|
15
14
|
end
|
16
|
-
module_function :decode
|
17
15
|
end
|
18
16
|
end
|
19
17
|
end
|
@@ -1,16 +1,14 @@
|
|
1
1
|
module CassandraObject
|
2
2
|
module Types
|
3
|
-
|
3
|
+
class HashType
|
4
4
|
def encode(hash)
|
5
5
|
raise ArgumentError.new("#{self} requires a Hash") unless hash.kind_of?(Hash)
|
6
6
|
ActiveSupport::JSON.encode(hash)
|
7
7
|
end
|
8
|
-
module_function :encode
|
9
8
|
|
10
9
|
def decode(str)
|
11
10
|
ActiveSupport::JSON.decode(str)
|
12
11
|
end
|
13
|
-
module_function :decode
|
14
12
|
end
|
15
13
|
end
|
16
14
|
end
|
@@ -1,19 +1,17 @@
|
|
1
1
|
module CassandraObject
|
2
2
|
module Types
|
3
|
-
|
3
|
+
class IntegerType
|
4
4
|
REGEX = /\A[-+]?\d+\Z/
|
5
5
|
def encode(int)
|
6
6
|
raise ArgumentError.new("#{self} requires an Integer. You passed #{int.inspect}") unless int.kind_of?(Integer)
|
7
7
|
int.to_s
|
8
8
|
end
|
9
|
-
module_function :encode
|
10
9
|
|
11
10
|
def decode(str)
|
12
11
|
return nil if str.empty?
|
13
12
|
raise ArgumentError.new("Cannot convert #{str} into an Integer") unless str.kind_of?(String) && str.match(REGEX)
|
14
13
|
str.to_i
|
15
14
|
end
|
16
|
-
module_function :decode
|
17
15
|
end
|
18
16
|
end
|
19
17
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module CassandraObject
|
2
2
|
module Types
|
3
|
-
|
3
|
+
class SetType
|
4
4
|
def encode(set)
|
5
5
|
if set.kind_of?(Set)
|
6
6
|
set.to_json
|
@@ -10,13 +10,11 @@ module CassandraObject
|
|
10
10
|
raise ArgumentError.new("#{self} requires an Array or Set")
|
11
11
|
end
|
12
12
|
end
|
13
|
-
module_function :encode
|
14
13
|
|
15
14
|
def decode(str)
|
16
15
|
return str.to_a if str.kind_of?(Set)
|
17
16
|
ActiveSupport::JSON.decode(str)
|
18
17
|
end
|
19
|
-
module_function :decode
|
20
18
|
end
|
21
19
|
end
|
22
20
|
end
|
@@ -1,16 +1,14 @@
|
|
1
1
|
module CassandraObject
|
2
2
|
module Types
|
3
|
-
|
3
|
+
class StringType
|
4
4
|
def encode(str)
|
5
5
|
raise ArgumentError.new("#{self} requires a String") unless str.kind_of?(String)
|
6
6
|
str.dup
|
7
7
|
end
|
8
|
-
module_function :encode
|
9
8
|
|
10
9
|
def decode(str)
|
11
10
|
str
|
12
11
|
end
|
13
|
-
module_function :decode
|
14
12
|
end
|
15
13
|
end
|
16
14
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module CassandraObject
|
2
2
|
module Types
|
3
|
-
|
3
|
+
class TimeType
|
4
4
|
# lifted from the implementation of Time.xmlschema and simplified
|
5
5
|
REGEX = /\A\s*
|
6
6
|
(-?\d+)-(\d\d)-(\d\d)
|
@@ -14,14 +14,12 @@ module CassandraObject
|
|
14
14
|
raise ArgumentError.new("#{self} requires a Time") unless time.kind_of?(Time)
|
15
15
|
time.xmlschema(6)
|
16
16
|
end
|
17
|
-
module_function :encode
|
18
17
|
|
19
18
|
def decode(str)
|
20
19
|
return nil if str.empty?
|
21
20
|
raise ArgumentError.new("Cannot convert #{str} into a Time") unless str.kind_of?(String) && str.match(REGEX)
|
22
21
|
Time.xmlschema(str)
|
23
22
|
end
|
24
|
-
module_function :decode
|
25
23
|
end
|
26
24
|
end
|
27
25
|
end
|
@@ -1,18 +1,16 @@
|
|
1
1
|
module CassandraObject
|
2
2
|
module Types
|
3
|
-
|
3
|
+
class TimeWithZoneType
|
4
4
|
def encode(time)
|
5
5
|
raise ArgumentError.new("#{self} requires a Time") unless time.kind_of?(Time)
|
6
6
|
time.utc.xmlschema(6)
|
7
7
|
end
|
8
|
-
module_function :encode
|
9
8
|
|
10
9
|
def decode(str)
|
11
10
|
return nil if str.empty?
|
12
11
|
raise ArgumentError.new("Cannot convert #{str} into a Time") unless str.kind_of?(String) && str.match(TimeType::REGEX)
|
13
12
|
Time.xmlschema(str).in_time_zone
|
14
13
|
end
|
15
|
-
module_function :decode
|
16
14
|
end
|
17
15
|
end
|
18
16
|
end
|
@@ -1,18 +1,16 @@
|
|
1
1
|
module CassandraObject
|
2
2
|
module Types
|
3
|
-
|
3
|
+
class UTF8StringType
|
4
4
|
def encode(str)
|
5
5
|
# This is technically the most correct, but it is a pain to require utf-8 encoding for all strings. Should revisit.
|
6
6
|
#raise ArgumentError.new("#{self} requires a UTF-8 encoded String") unless str.kind_of?(String) && str.encoding == Encoding::UTF_8
|
7
7
|
raise ArgumentError.new("#{self} requires a String") unless str.kind_of?(String)
|
8
8
|
str.dup
|
9
9
|
end
|
10
|
-
module_function :encode
|
11
10
|
|
12
11
|
def decode(str)
|
13
12
|
str.force_encoding('UTF-8')
|
14
13
|
end
|
15
|
-
module_function :decode
|
16
14
|
end
|
17
15
|
end
|
18
16
|
end
|
data/test/test_helper.rb
CHANGED
@@ -28,4 +28,13 @@ module CassandraObject
|
|
28
28
|
CassandraObject::Base.connection
|
29
29
|
end
|
30
30
|
end
|
31
|
+
|
32
|
+
module Types
|
33
|
+
class TestCase < ActiveSupport::TestCase
|
34
|
+
attr_accessor :coder
|
35
|
+
setup do
|
36
|
+
@coder = self.class.name.sub(/Test$/, '').constantize.new
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
31
40
|
end
|
@@ -1,15 +1,15 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
class CassandraObject::Types::ArrayTypeTest < CassandraObject::TestCase
|
3
|
+
class CassandraObject::Types::ArrayTypeTest < CassandraObject::Types::TestCase
|
4
4
|
test 'encode' do
|
5
|
-
assert_equal ['1', '2'].to_json,
|
5
|
+
assert_equal ['1', '2'].to_json, coder.encode(['1', '2'])
|
6
6
|
|
7
7
|
assert_raise ArgumentError do
|
8
|
-
|
8
|
+
coder.encode('wtf')
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
12
|
test 'decode' do
|
13
|
-
assert_equal ['1', '2'],
|
13
|
+
assert_equal ['1', '2'], coder.decode(['1', '2'].to_json)
|
14
14
|
end
|
15
15
|
end
|
@@ -1,23 +1,23 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
class CassandraObject::Types::BooleanTypeTest < CassandraObject::TestCase
|
3
|
+
class CassandraObject::Types::BooleanTypeTest < CassandraObject::Types::TestCase
|
4
4
|
test 'encode' do
|
5
|
-
assert_equal '1',
|
6
|
-
assert_equal '1',
|
7
|
-
assert_equal '1',
|
8
|
-
|
9
|
-
assert_equal '0',
|
10
|
-
assert_equal '0',
|
11
|
-
assert_equal '0',
|
12
|
-
assert_equal '0',
|
5
|
+
assert_equal '1', coder.encode(true)
|
6
|
+
assert_equal '1', coder.encode('true')
|
7
|
+
assert_equal '1', coder.encode('1')
|
8
|
+
coder
|
9
|
+
assert_equal '0', coder.encode(false)
|
10
|
+
assert_equal '0', coder.encode('false')
|
11
|
+
assert_equal '0', coder.encode('0')
|
12
|
+
assert_equal '0', coder.encode('')
|
13
13
|
|
14
14
|
assert_raise ArgumentError do
|
15
|
-
|
15
|
+
coder.encode('wtf')
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
19
|
test 'decode' do
|
20
|
-
assert_equal true,
|
21
|
-
assert_equal false,
|
20
|
+
assert_equal true, coder.decode('1')
|
21
|
+
assert_equal false, coder.decode('0')
|
22
22
|
end
|
23
23
|
end
|
@@ -1,18 +1,18 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
class CassandraObject::Types::IntegerTypeTest < CassandraObject::TestCase
|
3
|
+
class CassandraObject::Types::IntegerTypeTest < CassandraObject::Types::TestCase
|
4
4
|
test 'encode' do
|
5
|
-
assert_equal '3',
|
6
|
-
assert_equal '-3',
|
5
|
+
assert_equal '3', coder.encode(3)
|
6
|
+
assert_equal '-3', coder.encode(-3)
|
7
7
|
|
8
8
|
assert_raise ArgumentError do
|
9
|
-
|
9
|
+
coder.encode('xx')
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
13
|
test 'decode' do
|
14
|
-
assert_nil
|
15
|
-
assert_equal 3,
|
16
|
-
assert_equal -3,
|
14
|
+
assert_nil coder.decode('')
|
15
|
+
assert_equal 3, coder.decode('3')
|
16
|
+
assert_equal -3, coder.decode('-3')
|
17
17
|
end
|
18
18
|
end
|
data/test/types/set_type_test.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
class CassandraObject::Types::SetTypeTest < CassandraObject::TestCase
|
3
|
+
class CassandraObject::Types::SetTypeTest < CassandraObject::Types::TestCase
|
4
4
|
test 'encode' do
|
5
|
-
assert_equal ['1', '2'].to_json,
|
6
|
-
assert_equal ['1', '2'].to_json,
|
5
|
+
assert_equal ['1', '2'].to_json, coder.encode(['1', '2'].to_set)
|
6
|
+
assert_equal ['1', '2'].to_json, coder.encode(['1', '2', '2'])
|
7
7
|
|
8
8
|
assert_raise ArgumentError do
|
9
|
-
|
9
|
+
coder.encode('wtf')
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
13
|
test 'decode' do
|
14
|
-
assert_equal ['1', '2'],
|
15
|
-
assert_equal ['1', '2'],
|
14
|
+
assert_equal ['1', '2'], coder.decode(['1', '2'].to_set)
|
15
|
+
assert_equal ['1', '2'], coder.decode(['1', '2'].to_json)
|
16
16
|
end
|
17
17
|
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.3.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,11 +10,11 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-08-05 00:00:00.000000000Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rails
|
17
|
-
requirement: &
|
17
|
+
requirement: &2156966760 !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: *2156966760
|
26
26
|
- !ruby/object:Gem::Dependency
|
27
27
|
name: cassandra
|
28
|
-
requirement: &
|
28
|
+
requirement: &2156966180 !ruby/object:Gem::Requirement
|
29
29
|
none: false
|
30
30
|
requirements:
|
31
31
|
- - ~>
|
@@ -33,10 +33,10 @@ dependencies:
|
|
33
33
|
version: 0.11.3
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
|
-
version_requirements: *
|
36
|
+
version_requirements: *2156966180
|
37
37
|
- !ruby/object:Gem::Dependency
|
38
38
|
name: bundler
|
39
|
-
requirement: &
|
39
|
+
requirement: &2156965600 !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: *2156965600
|
48
48
|
description: Cassandra ActiveModel
|
49
49
|
email: gems@gotime.com
|
50
50
|
executables: []
|