sessionm-cassandra_object 2.2.8 → 2.2.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,15 +1,22 @@
1
1
  module CassandraObject
2
2
  class Attribute
3
- attr_reader :name, :converter, :expected_type
4
- def initialize(name, converter, expected_type)
3
+ attr_reader :name, :converter, :expected_type, :options
4
+ def initialize(name, converter, expected_type, options)
5
5
  @name = name.to_s
6
6
  @converter = converter
7
7
  @expected_type = expected_type
8
+ @options = options
8
9
  end
9
10
 
10
11
  def check_value!(value)
11
12
  return value if value.nil?
12
- value.kind_of?(expected_type) ? value : converter.decode(value)
13
+ if value.kind_of?(expected_type)
14
+ value
15
+ elsif converter.method(:decode).arity == 1
16
+ converter.decode(value)
17
+ else
18
+ converter.decode(value, @options)
19
+ end
13
20
  end
14
21
  end
15
22
 
@@ -32,8 +39,8 @@ module CassandraObject
32
39
  else
33
40
  raise "Unknown type #{options[:type]}"
34
41
  end
35
-
36
- model_attributes[name] = Attribute.new(name, converter, expected_type)
42
+
43
+ model_attributes[name] = Attribute.new(name, converter, expected_type, options.except(:type, :converter))
37
44
  end
38
45
 
39
46
  def define_attribute_methods
@@ -55,7 +55,15 @@ module CassandraObject
55
55
  end
56
56
 
57
57
  def decode_columns_hash(attributes)
58
- Hash[attributes.map { |k, v| [k.to_s, model_attributes[k].converter.decode(v)] }]
58
+ Hash[attributes.map do |k, v|
59
+ attribute = model_attributes[k]
60
+ decoded = if attribute.converter.method(:decode).arity == 1
61
+ attribute.converter.decode(v)
62
+ else
63
+ attribute.converter.decode(v, attribute.options)
64
+ end
65
+ [k.to_s, decoded]
66
+ end]
59
67
  end
60
68
 
61
69
  def column_family_configuration
@@ -7,10 +7,14 @@ module CassandraObject
7
7
  end
8
8
  module_function :encode
9
9
 
10
- def decode(str)
10
+ def decode(str, options={})
11
11
  return nil if str.empty?
12
12
  raise ArgumentError.new("Cannot convert #{str} into a BigDecimal") unless str.kind_of?(String)
13
- BigDecimal.new(str)
13
+ if options[:precision].present?
14
+ BigDecimal.new(str, options[:precision])
15
+ else
16
+ BigDecimal.new(str)
17
+ end
14
18
  end
15
19
  module_function :decode
16
20
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'sessionm-cassandra_object'
5
- s.version = '2.2.8'
5
+ s.version = '2.2.10'
6
6
  s.description = 'Cassandra ActiveModel'
7
7
  s.summary = 'Cassandra ActiveModel'
8
8
 
@@ -6,23 +6,23 @@ class CassandraObject::ConnectionTest < CassandraObject::TestCase
6
6
 
7
7
  test 'establish_connection' do
8
8
  TestObject.establish_connection(
9
- keyspace: 'place_directory_development',
9
+ keyspace: TEST_KEYSPACE,
10
10
  servers: '192.168.0.100:9160',
11
11
  thrift: {'timeout' => 10}
12
12
  )
13
13
 
14
14
  assert_not_equal CassandraObject::Base.connection, TestObject.connection
15
- assert_equal 'place_directory_development', TestObject.connection.keyspace
15
+ assert_equal TEST_KEYSPACE, TestObject.connection.keyspace
16
16
  assert_equal ["192.168.0.100:9160"], TestObject.connection.servers
17
17
  assert_equal 10, TestObject.connection.thrift_client_options[:timeout]
18
18
  end
19
19
 
20
20
  test 'establish_connection defaults' do
21
21
  TestObject.establish_connection(
22
- keyspace: 'place_directory_development'
22
+ keyspace: TEST_KEYSPACE
23
23
  )
24
24
 
25
- assert_equal 'place_directory_development', TestObject.connection.keyspace
25
+ assert_equal TEST_KEYSPACE, TestObject.connection.keyspace
26
26
  assert_equal ["127.0.0.1:9160"], TestObject.connection.servers
27
27
  end
28
- end
28
+ end
@@ -22,9 +22,13 @@ class CassandraObject::PersistenceTest < CassandraObject::TestCase
22
22
  begin
23
23
  Issue.validates(:description, presence: true)
24
24
 
25
- issue = Issue.new(description: 'bad')
25
+ issue = Issue.new(description: 'bad', worth: '9000.00311219')
26
26
  issue.save!
27
27
 
28
+ issue = Issue.find(issue.id)
29
+ assert_equal BigDecimal.new('9000.00311219'), issue.worth
30
+ assert_equal [18, 117], issue.worth.precs
31
+
28
32
  issue = Issue.new
29
33
  assert_raise(CassandraObject::RecordInvalid) { issue.save! }
30
34
  ensure
@@ -81,4 +85,4 @@ class CassandraObject::PersistenceTest < CassandraObject::TestCase
81
85
  persisted_issue.reload
82
86
  assert_equal 'say what', persisted_issue.description
83
87
  end
84
- end
88
+ end
data/test/test_helper.rb CHANGED
@@ -8,13 +8,16 @@ require 'rails/test_help'
8
8
  class Issue < CassandraObject::Base
9
9
  key :uuid
10
10
  attribute :description, :type => :string
11
+ attribute :worth, :type => :decimal, :precision => 100
11
12
  end
12
13
 
13
14
  module CassandraObject
14
15
  class TestCase < ActiveSupport::TestCase
16
+ TEST_KEYSPACE = 'place_directory_development'
17
+
15
18
  setup do
16
19
  CassandraObject::Base.establish_connection(
17
- :keyspace => 'place_directory_development',
20
+ :keyspace => TEST_KEYSPACE,
18
21
  # servers: '192.168.0.100:9160'
19
22
  :servers => '127.0.0.1:9160'
20
23
  )
@@ -7,5 +7,7 @@ class CassandraObject::Types::DecimalTypeTest < CassandraObject::TestCase
7
7
 
8
8
  test 'decode' do
9
9
  assert_equal BigDecimal.new('0.001'), CassandraObject::Types::DecimalType.decode('.001')
10
+ assert_equal [9, 18], CassandraObject::Types::DecimalType.decode('.001').precs
11
+ assert_equal [9, 27], CassandraObject::Types::DecimalType.decode('.001', :precision => 14).precs
10
12
  end
11
13
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sessionm-cassandra_object
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.8
4
+ version: 2.2.10
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -15,7 +15,7 @@ date: 2011-08-03 00:00:00.000000000Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rails
18
- requirement: &70341720486380 !ruby/object:Gem::Requirement
18
+ requirement: &70293952989880 !ruby/object:Gem::Requirement
19
19
  none: false
20
20
  requirements:
21
21
  - - ~>
@@ -23,10 +23,10 @@ dependencies:
23
23
  version: '3.0'
24
24
  type: :runtime
25
25
  prerelease: false
26
- version_requirements: *70341720486380
26
+ version_requirements: *70293952989880
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: cassandra
29
- requirement: &70341720484280 !ruby/object:Gem::Requirement
29
+ requirement: &70293952989420 !ruby/object:Gem::Requirement
30
30
  none: false
31
31
  requirements:
32
32
  - - ~>
@@ -34,10 +34,10 @@ dependencies:
34
34
  version: 0.11.3
35
35
  type: :runtime
36
36
  prerelease: false
37
- version_requirements: *70341720484280
37
+ version_requirements: *70293952989420
38
38
  - !ruby/object:Gem::Dependency
39
39
  name: bundler
40
- requirement: &70341720480680 !ruby/object:Gem::Requirement
40
+ requirement: &70293952988960 !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
43
  - - ~>
@@ -45,7 +45,7 @@ dependencies:
45
45
  version: 1.0.0
46
46
  type: :development
47
47
  prerelease: false
48
- version_requirements: *70341720480680
48
+ version_requirements: *70293952988960
49
49
  description: Cassandra ActiveModel
50
50
  email: klange@sessionm.com
51
51
  executables: []