gotime-cassandra_object 2.10.2 → 2.10.3

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.10.2'
5
+ s.version = '2.10.3'
6
6
  s.description = 'Cassandra ActiveModel'
7
7
  s.summary = 'Cassandra ActiveModel'
8
8
 
@@ -18,8 +18,8 @@ Gem::Specification.new do |s|
18
18
  s.test_files = `git ls-files -- {test}/*`.split("\n")
19
19
  s.require_paths = ['lib']
20
20
 
21
- s.add_runtime_dependency('activemodel', "~> 3.0")
22
- s.add_runtime_dependency('cassandra', "~> 0.12.0")
21
+ s.add_runtime_dependency('activemodel', ">= 3.0")
22
+ s.add_runtime_dependency('cassandra', ">= 0.12.0")
23
23
 
24
- s.add_development_dependency('bundler', "~> 1.0.0")
24
+ s.add_development_dependency('bundler')
25
25
  end
@@ -12,7 +12,7 @@ module CassandraObject
12
12
  end
13
13
 
14
14
  def column_family
15
- @column_family || name.pluralize
15
+ @column_family ||= base_class.name.pluralize
16
16
  end
17
17
 
18
18
  def base_class
@@ -37,6 +37,7 @@ module CassandraObject
37
37
  include AttributeMethods
38
38
  include AttributeMethods::Dirty
39
39
  include AttributeMethods::Typecasting
40
+ include BelongsTo
40
41
  include Callbacks
41
42
  include Validations
42
43
  include Associations
@@ -0,0 +1,42 @@
1
+ module CassandraObject
2
+ module BelongsTo
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ # === Options
7
+ # [:class_name]
8
+ # Use if the class cannot be inferred from the association
9
+ # [:polymorphic]
10
+ # Specify if the association is polymorphic
11
+ # Example:
12
+ # class Driver < CassandraObject::Base
13
+ # end
14
+ # class Truck < CassandraObject::Base
15
+ # end
16
+ def belongs_to(name, options = {})
17
+ instance_variable_name = "@#{name}"
18
+
19
+ define_method("#{name}=") do |record|
20
+ instance_variable_set(instance_variable_name, record)
21
+ send("#{name}_id=", record.try(:id))
22
+ if options[:polymorphic]
23
+ send("#{name}_type=", record.class.name)
24
+ end
25
+ end
26
+
27
+ define_method(name) do
28
+ unless instance_variable_defined?(instance_variable_name)
29
+ if record_id = send("#{name}_id").presence
30
+ model_name = options[:polymorphic] ? send("#{name}_type") : (options[:class_name] || name.to_s.classify)
31
+ instance_variable_set(instance_variable_name, model_name.constantize.find_by_id(record_id))
32
+ else
33
+ instance_variable_set(instance_variable_name, nil)
34
+ end
35
+ end
36
+
37
+ instance_variable_get(instance_variable_name)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -10,6 +10,10 @@ module CassandraObject
10
10
  autoload :HashedNaturalKeyFactory
11
11
  autoload :CustomKeyFactory
12
12
 
13
+ included do
14
+ class_attribute :key_factory
15
+ end
16
+
13
17
  module ClassMethods
14
18
  # Indicate what kind of key the model will have: uuid or natural
15
19
  #
@@ -17,26 +21,26 @@ module CassandraObject
17
21
  # @param the options you want to pass along to the key factory (like :attributes => :name, for a natural key).
18
22
  #
19
23
  def key(name_or_factory = :uuid, *options)
20
- @key_factory = case name_or_factory
21
- when :uuid
22
- UUIDKeyFactory.new
23
- when :natural
24
- NaturalKeyFactory.new(*options)
25
- when :custom
26
- CustomKeyFactory.new(*options)
27
- else
28
- name_or_factory
29
- end
24
+ self.key_factory = case name_or_factory
25
+ when :uuid
26
+ UUIDKeyFactory.new
27
+ when :natural
28
+ NaturalKeyFactory.new(*options)
29
+ when :custom
30
+ CustomKeyFactory.new(*options)
31
+ else
32
+ name_or_factory
33
+ end
30
34
  end
31
35
 
32
36
  def next_key(object = nil)
33
- @key_factory.next_key(object).tap do |key|
37
+ key_factory.next_key(object).tap do |key|
34
38
  raise "Keys may not be nil" if key.nil?
35
39
  end
36
40
  end
37
41
 
38
42
  def parse_key(string)
39
- @key_factory.parse(string)
43
+ key_factory.parse(string)
40
44
  end
41
45
  end
42
46
 
@@ -7,6 +7,7 @@ module CassandraObject
7
7
  autoload :Base
8
8
  autoload :Connection
9
9
  autoload :AttributeMethods
10
+ autoload :BelongsTo
10
11
  autoload :Consistency
11
12
  autoload :Persistence
12
13
  autoload :Callbacks
@@ -14,6 +14,7 @@ class CassandraObject::BaseTest < CassandraObject::TestCase
14
14
 
15
15
  test 'column family' do
16
16
  assert_equal 'CassandraObject::BaseTest::Sons', Son.column_family
17
+ assert_equal 'CassandraObject::BaseTest::Sons', Grandson.column_family
17
18
  end
18
19
 
19
20
  test 'initialiaze' do
@@ -0,0 +1,52 @@
1
+ require 'test_helper'
2
+
3
+ class CassandraObject::BelongsToTest < CassandraObject::TestCase
4
+ class TestObject < Issue
5
+ string :issue_id
6
+ belongs_to :issue
7
+
8
+ string :widget_id
9
+ belongs_to :widget, class_name: 'Issue'
10
+
11
+ string :target_id
12
+ string :target_type
13
+ belongs_to :target, polymorphic: true
14
+ end
15
+
16
+ test 'belongs_to' do
17
+ issue = Issue.create
18
+
19
+ record = TestObject.create(issue: issue)
20
+
21
+ assert_equal issue, record.issue
22
+ assert_equal issue.id, record.issue_id
23
+
24
+ record = TestObject.find(record.id)
25
+ assert_equal issue, record.issue
26
+ end
27
+
28
+ test 'belongs_to with class_name' do
29
+ issue = Issue.create
30
+
31
+ record = TestObject.create(widget: issue)
32
+
33
+ assert_equal issue, record.widget
34
+ assert_equal issue.id, record.widget_id
35
+
36
+ record = TestObject.find(record.id)
37
+ assert_equal issue, record.widget
38
+ end
39
+
40
+ test 'belongs_to with polymorphic' do
41
+ issue = Issue.create
42
+
43
+ record = TestObject.create(target: issue)
44
+
45
+ assert_equal issue, record.target
46
+ assert_equal issue.id, record.target_id
47
+ assert_equal 'Issue', record.target_type
48
+
49
+ record = TestObject.find(record.id)
50
+ assert_equal issue, record.target
51
+ end
52
+ 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.10.2
4
+ version: 2.10.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,37 +14,37 @@ date: 2012-01-10 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activemodel
17
- requirement: &70228572053300 !ruby/object:Gem::Requirement
17
+ requirement: &70254537627400 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
- - - ~>
20
+ - - ! '>='
21
21
  - !ruby/object:Gem::Version
22
22
  version: '3.0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *70228572053300
25
+ version_requirements: *70254537627400
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: cassandra
28
- requirement: &70228572052320 !ruby/object:Gem::Requirement
28
+ requirement: &70254537638320 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
- - - ~>
31
+ - - ! '>='
32
32
  - !ruby/object:Gem::Version
33
33
  version: 0.12.0
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *70228572052320
36
+ version_requirements: *70254537638320
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: bundler
39
- requirement: &70228572051560 !ruby/object:Gem::Requirement
39
+ requirement: &70254537637500 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
- - - ~>
42
+ - - ! '>='
43
43
  - !ruby/object:Gem::Version
44
- version: 1.0.0
44
+ version: '0'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *70228572051560
47
+ version_requirements: *70254537637500
48
48
  description: Cassandra ActiveModel
49
49
  email: gems@gotime.com
50
50
  executables: []
@@ -69,6 +69,7 @@ files:
69
69
  - lib/cassandra_object/attribute_methods/typecasting.rb
70
70
  - lib/cassandra_object/base.rb
71
71
  - lib/cassandra_object/batches.rb
72
+ - lib/cassandra_object/belongs_to.rb
72
73
  - lib/cassandra_object/callbacks.rb
73
74
  - lib/cassandra_object/collection.rb
74
75
  - lib/cassandra_object/connection.rb
@@ -124,6 +125,7 @@ files:
124
125
  - test/unit/attribute_methods_test.rb
125
126
  - test/unit/base_test.rb
126
127
  - test/unit/batches_test.rb
128
+ - test/unit/belongs_to_test.rb
127
129
  - test/unit/callbacks_test.rb
128
130
  - test/unit/connection_test.rb
129
131
  - test/unit/consistency_test.rb