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.
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'gotime-cassandra_object'
5
- s.version = '2.6.4'
5
+ s.version = '2.7.0'
6
6
  s.description = 'Cassandra ActiveModel'
7
7
  s.summary = 'Cassandra ActiveModel'
8
8
 
@@ -8,8 +8,9 @@ module CassandraObject
8
8
  end
9
9
 
10
10
  def instantiate(record, value)
11
- return if value.nil? && coder.ignore_nil?
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 = {}.with_indifferent_access
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 = {}.with_indifferent_access
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 = encode_columns_hash(attributes, schema_version)
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", decode_columns_hash(object, attributes))
39
+ object.instance_variable_set("@attributes", instantiate_attributes(object, attributes))
44
40
  end
45
41
  end
46
42
 
47
- def encode_columns_hash(attributes, schema_version)
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 decode_columns_hash(object, attributes)
58
- Hash[attributes.map { |k, v| [k.to_s, instantiate_attribute(object, k, v)] }]
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
 
@@ -44,8 +44,8 @@ module CassandraObject
44
44
  end
45
45
  end
46
46
 
47
- def ignore_nil?
48
- false
47
+ def default
48
+ []
49
49
  end
50
50
 
51
51
  def encode(array)
@@ -6,8 +6,8 @@ module CassandraObject
6
6
  @options = options
7
7
  end
8
8
 
9
- def ignore_nil?
10
- true
9
+ def default
10
+ options[:default].dup if options[:default]
11
11
  end
12
12
 
13
13
  def encode(value)
@@ -10,7 +10,6 @@ module CassandraObject
10
10
  end
11
11
 
12
12
  def decode(str)
13
- return nil if str.empty?
14
13
  Date.parse(str)
15
14
  end
16
15
  end
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(name: 'foo')
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,10 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class CassandraObject::Types::ArrayTypeTest < CassandraObject::Types::TestCase
4
- test 'ignore_nil' do
5
- assert_equal false, coder.ignore_nil?
6
- end
7
-
8
4
  test 'encode' do
9
5
  assert_equal ['1', '2'].to_json, coder.encode(['1', '2'])
10
6
 
@@ -1,8 +1,9 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class CassandraObject::Types::BaseTypeTest < CassandraObject::Types::TestCase
4
- test 'ignore_nil' do
5
- assert_equal true, coder.ignore_nil?
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.6.4
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: &70125957503400 !ruby/object:Gem::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: *70125957503400
25
+ version_requirements: *70185349554920
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: cassandra
28
- requirement: &70125957502800 !ruby/object:Gem::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: *70125957502800
36
+ version_requirements: *70185349554140
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: bundler
39
- requirement: &70125957502140 !ruby/object:Gem::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: *70125957502140
47
+ version_requirements: *70185349553500
48
48
  description: Cassandra ActiveModel
49
49
  email: gems@gotime.com
50
50
  executables: []