gotime-cassandra_object 2.12.5 → 2.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'gotime-cassandra_object'
5
- s.version = '2.12.5'
5
+ s.version = '2.13.0'
6
6
  s.description = 'Cassandra ActiveModel'
7
7
  s.summary = 'Cassandra ActiveModel'
8
8
  s.authors = ["Michael Koziarski", "gotime"]
@@ -2,6 +2,11 @@ module CassandraObject
2
2
  module BelongsTo
3
3
  extend ActiveSupport::Concern
4
4
 
5
+ included do
6
+ class_attribute :belongs_to_reflections
7
+ self.belongs_to_reflections = {}
8
+ end
9
+
5
10
  module ClassMethods
6
11
  # === Options
7
12
  # [:class_name]
@@ -14,29 +19,41 @@ module CassandraObject
14
19
  # class Truck < CassandraObject::Base
15
20
  # end
16
21
  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
22
+ CassandraObject::BelongsTo::Builder.build(self, name, options)
23
+ end
26
24
 
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)
25
+ def generated_belongs_to_methods
26
+ @generated_belongs_to_methods ||= begin
27
+ mod = const_set(:GeneratedSearchesManyMethods, Module.new)
28
+ include mod
29
+ mod
38
30
  end
39
31
  end
40
32
  end
33
+
34
+ # Returns the belongs_to instance for the given name, instantiating it if it doesn't already exist
35
+ def belongs_to_association(name)
36
+ association = belongs_to_instance_get(name)
37
+
38
+ if association.nil?
39
+ association = CassandraObject::BelongsTo::Association.new(self, belongs_to_reflections[name])
40
+ belongs_to_instance_set(name, association)
41
+ end
42
+
43
+ association
44
+ end
45
+
46
+ private
47
+ def belongs_to_cache
48
+ @belongs_to_cache ||= {}
49
+ end
50
+
51
+ def belongs_to_instance_get(name)
52
+ belongs_to_cache[name.to_sym]
53
+ end
54
+
55
+ def belongs_to_instance_set(name, association)
56
+ belongs_to_cache[name.to_sym] = association
57
+ end
41
58
  end
42
59
  end
@@ -0,0 +1,53 @@
1
+ module CassandraObject
2
+ module BelongsTo
3
+ class Association
4
+ attr_reader :owner, :reflection
5
+ delegate :options, to: :reflection
6
+
7
+ def initialize(owner, reflection)
8
+ @owner = owner
9
+ @reflection = reflection
10
+ end
11
+
12
+ def reader
13
+ unless loaded?
14
+ if record_id = owner.send(reflection.foreign_key).presence
15
+ self.record_variable = association_class.find_by_id(record_id)
16
+ else
17
+ self.record_variable = nil
18
+ end
19
+ end
20
+
21
+ record_variable
22
+ end
23
+
24
+ def writer(record)
25
+ self.record_variable = record
26
+ owner.send("#{reflection.foreign_key}=", record.try(:id))
27
+ if reflection.polymorphic?
28
+ owner.send("#{reflection.polymorphic_column}=", record.class.name)
29
+ end
30
+ end
31
+
32
+ def association_class
33
+ association_class_name.constantize
34
+ end
35
+
36
+ def association_class_name
37
+ reflection.polymorphic? ? owner.send(reflection.polymorphic_column) : reflection.class_name
38
+ end
39
+
40
+ def record_variable
41
+ owner.instance_variable_get(reflection.instance_variable_name)
42
+ end
43
+
44
+ def record_variable=(record)
45
+ owner.instance_variable_set(reflection.instance_variable_name, record)
46
+ end
47
+
48
+ def loaded?
49
+ owner.instance_variable_defined?(reflection.instance_variable_name)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,40 @@
1
+ module CassandraObject
2
+ module BelongsTo
3
+ class Builder
4
+ def self.build(model, name, options)
5
+ new(model, name, options).build
6
+ end
7
+
8
+ attr_reader :model, :name, :options
9
+ def initialize(model, name, options)
10
+ @model, @name, @options = model, name, options
11
+ end
12
+
13
+ def build
14
+ define_writer
15
+ define_reader
16
+
17
+ reflection = CassandraObject::BelongsTo::Reflection.new(model, name, options)
18
+ model.belongs_to_reflections = model.belongs_to_reflections.merge(name => reflection)
19
+ end
20
+
21
+ def mixin
22
+ model.generated_belongs_to_methods
23
+ end
24
+
25
+ def define_writer
26
+ name = self.name
27
+ mixin.redefine_method("#{name}=") do |records|
28
+ belongs_to_association(name).writer(records)
29
+ end
30
+ end
31
+
32
+ def define_reader
33
+ name = self.name
34
+ mixin.redefine_method(name) do
35
+ belongs_to_association(name).reader
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,30 @@
1
+ module CassandraObject
2
+ module BelongsTo
3
+ class Reflection
4
+ attr_reader :model, :name, :options
5
+ def initialize(model, name, options)
6
+ @model, @name, @options = model, name, options
7
+ end
8
+
9
+ def instance_variable_name
10
+ "@#{name}"
11
+ end
12
+
13
+ def foreign_key
14
+ "#{name}_id"
15
+ end
16
+
17
+ def polymorphic_column
18
+ "#{name}_type"
19
+ end
20
+
21
+ def polymorphic?
22
+ options[:polymorphic]
23
+ end
24
+
25
+ def class_name
26
+ options[:class_name] || name.to_s.classify
27
+ end
28
+ end
29
+ end
30
+ end
@@ -3,9 +3,9 @@ module CassandraObject
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  module ClassMethods
6
- def remove(key)
7
- ActiveSupport::Notifications.instrument("remove.cassandra_object", column_family: column_family, key: key) do
8
- connection.remove(column_family, key.to_s, consistency: thrift_write_consistency)
6
+ def remove(id)
7
+ ActiveSupport::Notifications.instrument("remove.cassandra_object", column_family: column_family, key: id) do
8
+ connection.remove(column_family, id, consistency: thrift_write_consistency)
9
9
  end
10
10
  end
11
11
 
@@ -21,19 +21,19 @@ module CassandraObject
21
21
  end
22
22
  end
23
23
 
24
- def write(key, attributes)
24
+ def write(id, attributes)
25
25
  attributes = encode_attributes(attributes)
26
- ActiveSupport::Notifications.instrument("insert.cassandra_object", column_family: column_family, key: key, attributes: attributes) do
27
- connection.insert(column_family, key.to_s, attributes, consistency: thrift_write_consistency)
26
+ ActiveSupport::Notifications.instrument("insert.cassandra_object", column_family: column_family, key: id, attributes: attributes) do
27
+ connection.insert(column_family, id, attributes, consistency: thrift_write_consistency)
28
28
  # if nil_attributes.any?
29
29
  # connection.remove(connection, key.to_s, *nil_attributes)
30
30
  # end
31
31
  end
32
32
  end
33
33
 
34
- def instantiate(key, attributes)
34
+ def instantiate(id, attributes)
35
35
  allocate.tap do |object|
36
- object.instance_variable_set("@id", key) if key
36
+ object.instance_variable_set("@id", id) if id
37
37
  object.instance_variable_set("@new_record", false)
38
38
  object.instance_variable_set("@destroyed", false)
39
39
  object.instance_variable_set("@attributes", typecast_attributes(object, attributes))
@@ -124,7 +124,7 @@ module CassandraObject
124
124
  end
125
125
 
126
126
  def write
127
- changed_attributes = changed.inject({}) { |h, n| h[n] = read_attribute(n); h }
127
+ changed_attributes = Hash[changed.map { |attr| [attr, read_attribute(attr)] }]
128
128
  self.class.write(id, changed_attributes)
129
129
  end
130
130
  end
@@ -24,6 +24,14 @@ module CassandraObject
24
24
  autoload :Type
25
25
  autoload :Schema
26
26
 
27
+ module BelongsTo
28
+ extend ActiveSupport::Autoload
29
+
30
+ autoload :Association
31
+ autoload :Builder
32
+ autoload :Reflection
33
+ end
34
+
27
35
  module AttributeMethods
28
36
  extend ActiveSupport::Autoload
29
37
 
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.12.5
4
+ version: 2.13.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: 2012-06-05 00:00:00.000000000 Z
13
+ date: 2012-07-05 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activemodel
17
- requirement: &70337376492520 !ruby/object:Gem::Requirement
17
+ requirement: &70153896808960 !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: *70337376492520
25
+ version_requirements: *70153896808960
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: mcmire-cassandra
28
- requirement: &70337376492060 !ruby/object:Gem::Requirement
28
+ requirement: &70153896808500 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: 0.12.3
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *70337376492060
36
+ version_requirements: *70153896808500
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: thrift_client
39
- requirement: &70337376491600 !ruby/object:Gem::Requirement
39
+ requirement: &70153896808040 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ~>
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: 0.8.0
45
45
  type: :runtime
46
46
  prerelease: false
47
- version_requirements: *70337376491600
47
+ version_requirements: *70153896808040
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: bundler
50
- requirement: &70337376491220 !ruby/object:Gem::Requirement
50
+ requirement: &70153896807660 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,7 +55,7 @@ dependencies:
55
55
  version: '0'
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *70337376491220
58
+ version_requirements: *70153896807660
59
59
  description: Cassandra ActiveModel
60
60
  email: gems@gotime.com
61
61
  executables: []
@@ -80,6 +80,9 @@ files:
80
80
  - lib/cassandra_object/base.rb
81
81
  - lib/cassandra_object/batches.rb
82
82
  - lib/cassandra_object/belongs_to.rb
83
+ - lib/cassandra_object/belongs_to/association.rb
84
+ - lib/cassandra_object/belongs_to/builder.rb
85
+ - lib/cassandra_object/belongs_to/reflection.rb
83
86
  - lib/cassandra_object/callbacks.rb
84
87
  - lib/cassandra_object/collection.rb
85
88
  - lib/cassandra_object/connection.rb