datastax_rails 2.0.12 → 2.0.15
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.
- checksums.yaml +4 -4
- data/Rakefile +5 -5
- data/lib/blankslate.rb +8 -11
- data/lib/cql-rb_extensions.rb +5 -3
- data/lib/datastax_rails/associations/association.rb +93 -101
- data/lib/datastax_rails/associations/association_scope.rb +7 -7
- data/lib/datastax_rails/associations/belongs_to_association.rb +46 -48
- data/lib/datastax_rails/associations/builder/association.rb +32 -31
- data/lib/datastax_rails/associations/builder/belongs_to.rb +19 -20
- data/lib/datastax_rails/associations/builder/collection_association.rb +32 -32
- data/lib/datastax_rails/associations/builder/has_and_belongs_to_many.rb +21 -21
- data/lib/datastax_rails/associations/builder/has_many.rb +39 -40
- data/lib/datastax_rails/associations/builder/has_one.rb +30 -31
- data/lib/datastax_rails/associations/builder/singular_association.rb +31 -33
- data/lib/datastax_rails/associations/collection_association.rb +129 -135
- data/lib/datastax_rails/associations/collection_proxy.rb +21 -21
- data/lib/datastax_rails/associations/has_and_belongs_to_many_association.rb +26 -26
- data/lib/datastax_rails/associations/has_many_association.rb +38 -38
- data/lib/datastax_rails/associations/has_one_association.rb +31 -32
- data/lib/datastax_rails/associations/singular_association.rb +31 -30
- data/lib/datastax_rails/associations.rb +27 -24
- data/lib/datastax_rails/attribute_assignment.rb +17 -17
- data/lib/datastax_rails/attribute_methods/definition.rb +4 -4
- data/lib/datastax_rails/attribute_methods/dirty.rb +34 -33
- data/lib/datastax_rails/attribute_methods/primary_key.rb +3 -8
- data/lib/datastax_rails/attribute_methods/read.rb +10 -12
- data/lib/datastax_rails/attribute_methods/typecasting.rb +36 -35
- data/lib/datastax_rails/attribute_methods/write.rb +5 -6
- data/lib/datastax_rails/attribute_methods.rb +52 -56
- data/lib/datastax_rails/base.rb +122 -125
- data/lib/datastax_rails/callbacks.rb +15 -9
- data/lib/datastax_rails/cassandra_only_model.rb +6 -6
- data/lib/datastax_rails/collection.rb +5 -7
- data/lib/datastax_rails/column.rb +130 -118
- data/lib/datastax_rails/connection/statement_cache.rb +3 -3
- data/lib/datastax_rails/connection.rb +42 -33
- data/lib/datastax_rails/cql/alter_column_family.rb +19 -21
- data/lib/datastax_rails/cql/base.rb +8 -11
- data/lib/datastax_rails/cql/column_family.rb +11 -10
- data/lib/datastax_rails/cql/consistency.rb +2 -2
- data/lib/datastax_rails/cql/create_column_family.rb +15 -15
- data/lib/datastax_rails/cql/create_index.rb +5 -5
- data/lib/datastax_rails/cql/create_keyspace.rb +7 -7
- data/lib/datastax_rails/cql/delete.rb +16 -29
- data/lib/datastax_rails/cql/drop_column_family.rb +2 -2
- data/lib/datastax_rails/cql/drop_index.rb +2 -2
- data/lib/datastax_rails/cql/drop_keyspace.rb +2 -2
- data/lib/datastax_rails/cql/insert.rb +10 -16
- data/lib/datastax_rails/cql/select.rb +21 -33
- data/lib/datastax_rails/cql/truncate.rb +2 -2
- data/lib/datastax_rails/cql/update.rb +16 -24
- data/lib/datastax_rails/cql/use_keyspace.rb +2 -2
- data/lib/datastax_rails/cql.rb +2 -2
- data/lib/datastax_rails/dynamic_model.rb +32 -29
- data/lib/datastax_rails/errors.rb +6 -6
- data/lib/datastax_rails/grouped_collection.rb +3 -3
- data/lib/datastax_rails/inheritance.rb +9 -9
- data/lib/datastax_rails/payload_model.rb +24 -20
- data/lib/datastax_rails/persistence.rb +116 -110
- data/lib/datastax_rails/railtie.rb +7 -7
- data/lib/datastax_rails/reflection.rb +61 -59
- data/lib/datastax_rails/relation/batches.rb +12 -13
- data/lib/datastax_rails/relation/facet_methods.rb +44 -33
- data/lib/datastax_rails/relation/finder_methods.rb +95 -91
- data/lib/datastax_rails/relation/modification_methods.rb +5 -5
- data/lib/datastax_rails/relation/search_methods.rb +102 -102
- data/lib/datastax_rails/relation/spawn_methods.rb +25 -24
- data/lib/datastax_rails/relation/stats_methods.rb +9 -8
- data/lib/datastax_rails/relation.rb +165 -170
- data/lib/datastax_rails/rsolr_client_wrapper.rb +3 -3
- data/lib/datastax_rails/schema/cassandra.rb +44 -43
- data/lib/datastax_rails/schema/migrator.rb +52 -52
- data/lib/datastax_rails/schema/solr.rb +55 -47
- data/lib/datastax_rails/schema_cache.rb +1 -3
- data/lib/datastax_rails/scoping/default.rb +2 -3
- data/lib/datastax_rails/scoping/named.rb +3 -5
- data/lib/datastax_rails/scoping.rb +11 -12
- data/lib/datastax_rails/serialization.rb +34 -31
- data/lib/datastax_rails/serializers/xml_serializer.rb +178 -175
- data/lib/datastax_rails/timestamps.rb +4 -4
- data/lib/datastax_rails/types/dirty_collection.rb +57 -57
- data/lib/datastax_rails/types/dynamic_list.rb +1 -1
- data/lib/datastax_rails/types/dynamic_map.rb +5 -7
- data/lib/datastax_rails/types/dynamic_set.rb +2 -2
- data/lib/datastax_rails/util/solr_repair.rb +3 -3
- data/lib/datastax_rails/validations/associated.rb +8 -6
- data/lib/datastax_rails/validations/uniqueness.rb +8 -8
- data/lib/datastax_rails/validations.rb +9 -10
- data/lib/datastax_rails/version.rb +2 -1
- data/lib/datastax_rails/wide_storage_model.rb +6 -6
- data/lib/datastax_rails.rb +13 -9
- data/lib/schema_migration.rb +3 -3
- data/spec/datastax_rails/associations/belongs_to_association_spec.rb +2 -2
- data/spec/datastax_rails/associations/collection_association_spec.rb +14 -14
- data/spec/datastax_rails/associations/has_many_association_spec.rb +20 -20
- data/spec/datastax_rails/associations_spec.rb +11 -11
- data/spec/datastax_rails/attribute_methods_spec.rb +25 -25
- data/spec/datastax_rails/base_spec.rb +24 -24
- data/spec/datastax_rails/callbacks_spec.rb +21 -21
- data/spec/datastax_rails/column_spec.rb +133 -132
- data/spec/datastax_rails/connection/statement_cache_spec.rb +2 -2
- data/spec/datastax_rails/cql/base_spec.rb +4 -4
- data/spec/datastax_rails/cql/delete_spec.rb +19 -0
- data/spec/datastax_rails/cql/select_spec.rb +8 -8
- data/spec/datastax_rails/cql/update_spec.rb +8 -10
- data/spec/datastax_rails/dynamic_model_spec.rb +36 -22
- data/spec/datastax_rails/inheritance_spec.rb +11 -14
- data/spec/datastax_rails/persistence_spec.rb +73 -74
- data/spec/datastax_rails/relation/batches_spec.rb +13 -13
- data/spec/datastax_rails/relation/facet_methods_spec.rb +43 -35
- data/spec/datastax_rails/relation/finder_methods_spec.rb +77 -78
- data/spec/datastax_rails/relation/modification_methods_spec.rb +19 -19
- data/spec/datastax_rails/relation/search_methods_spec.rb +160 -160
- data/spec/datastax_rails/relation/spawn_methods_spec.rb +18 -18
- data/spec/datastax_rails/relation_spec.rb +119 -116
- data/spec/datastax_rails/schema/migrator_spec.rb +30 -30
- data/spec/datastax_rails/schema/solr_spec.rb +15 -15
- data/spec/datastax_rails/scoping/default_spec.rb +9 -9
- data/spec/datastax_rails/types/dynamic_list_spec.rb +12 -12
- data/spec/datastax_rails/types/dynamic_map_spec.rb +10 -10
- data/spec/datastax_rails/types/dynamic_set_spec.rb +22 -10
- data/spec/datastax_rails/validations/uniqueness_spec.rb +25 -25
- data/spec/datastax_rails/wide_storage_model_spec.rb +11 -0
- data/spec/datastax_rails_spec.rb +2 -2
- data/spec/dummy/config/application.rb +2 -3
- data/spec/dummy/config/boot.rb +1 -1
- data/spec/dummy/config/environments/development.rb +3 -3
- data/spec/dummy/config/environments/test.rb +1 -1
- data/spec/dummy/config/initializers/session_store.rb +1 -1
- data/spec/dummy/config/initializers/wrap_parameters.rb +1 -1
- data/spec/factories/audit_logs.rb +6 -0
- data/spec/factories/hobbies.rb +6 -0
- data/spec/factories/people.rb +5 -0
- data/spec/feature/dynamic_fields_spec.rb +4 -4
- data/spec/feature/overloaded_tables_spec.rb +11 -12
- data/spec/spec_helper.rb +17 -14
- data/spec/support/datastax_test_hook.rb +2 -2
- data/spec/support/default_consistency_shared_examples.rb +11 -11
- data/spec/support/models.rb +31 -32
- metadata +40 -6
- data/lib/datastax_rails/attribute_methods/before_type_cast.rb +0 -71
- data/lib/datastax_rails/log_subscriber.rb +0 -0
- data/spec/dummy/ks/migrate/20111117224534_models.rb +0 -20
@@ -21,7 +21,7 @@ module DatastaxRails
|
|
21
21
|
|
22
22
|
attributes.each do |k, v|
|
23
23
|
if v.is_a?(Hash)
|
24
|
-
nested_parameter_attributes << [
|
24
|
+
nested_parameter_attributes << [k, v]
|
25
25
|
else
|
26
26
|
_assign_attribute(k, v)
|
27
27
|
end
|
@@ -29,23 +29,23 @@ module DatastaxRails
|
|
29
29
|
|
30
30
|
assign_nested_parameter_attributes(nested_parameter_attributes) unless nested_parameter_attributes.empty?
|
31
31
|
end
|
32
|
-
|
33
|
-
|
32
|
+
alias_method :attributes=, :assign_attributes
|
33
|
+
|
34
34
|
private
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
# Assign any deferred nested attributes after the base attributes have been set.
|
47
|
-
def assign_nested_parameter_attributes(pairs)
|
48
|
-
pairs.each { |k, v| _assign_attribute(k, v) }
|
35
|
+
|
36
|
+
def _assign_attribute(k, v)
|
37
|
+
public_send("#{k}=", v)
|
38
|
+
rescue NoMethodError
|
39
|
+
if respond_to?("#{k}=")
|
40
|
+
raise
|
41
|
+
else
|
42
|
+
raise UnknownAttributeError, "unknown attribute: #{k}"
|
49
43
|
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Assign any deferred nested attributes after the base attributes have been set.
|
47
|
+
def assign_nested_parameter_attributes(pairs)
|
48
|
+
pairs.each { |k, v| _assign_attribute(k, v) }
|
49
|
+
end
|
50
50
|
end
|
51
51
|
end
|
@@ -12,19 +12,19 @@ module DatastaxRails
|
|
12
12
|
def instantiate(record, value)
|
13
13
|
value = coder.default if value.nil?
|
14
14
|
return if value.nil?
|
15
|
-
|
15
|
+
|
16
16
|
value = coder.decode(value)
|
17
17
|
coder.wrap(record, name, value)
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
# Returns :solr, :cassandra, :both, or +false+
|
21
21
|
def indexed
|
22
22
|
coder.options[:indexed]
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
def type
|
26
26
|
coder.type
|
27
27
|
end
|
28
28
|
end
|
29
29
|
end
|
30
|
-
end
|
30
|
+
end
|
@@ -3,16 +3,16 @@ module DatastaxRails
|
|
3
3
|
module Dirty
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
include ActiveModel::Dirty
|
6
|
-
|
6
|
+
|
7
7
|
included do
|
8
8
|
if self < ::DatastaxRails::Timestamps
|
9
|
-
|
9
|
+
fail 'You cannot include Dirty after Timestamps'
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
13
|
# Attempts to +save+ the record and clears changed attributes if successful.
|
14
14
|
def save(*) #:nodoc:
|
15
|
-
if status = super
|
15
|
+
if (status = super)
|
16
16
|
@previously_changed = changes
|
17
17
|
@changed_attributes.clear
|
18
18
|
end
|
@@ -38,7 +38,7 @@ module DatastaxRails
|
|
38
38
|
def write_attribute(attr, value)
|
39
39
|
attr = attr.to_s
|
40
40
|
loaded_attributes[attr] = true
|
41
|
-
|
41
|
+
|
42
42
|
# The attribute already has an unsaved change.
|
43
43
|
if attribute_changed?(attr)
|
44
44
|
old = @changed_attributes[attr]
|
@@ -47,39 +47,40 @@ module DatastaxRails
|
|
47
47
|
old = clone_attribute_value(:read_attribute, attr)
|
48
48
|
@changed_attributes[attr] = old if _field_changed?(attr, old, value)
|
49
49
|
end
|
50
|
-
|
50
|
+
|
51
51
|
super
|
52
52
|
end
|
53
|
-
|
53
|
+
|
54
54
|
private
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
55
|
+
|
56
|
+
def _field_changed?(attr, old, value)
|
57
|
+
if (column = column_for_attribute(attr))
|
58
|
+
if column.number? && (changes_from_nil_to_empty_string?(column, old, value) ||
|
59
|
+
changes_from_zero_to_string?(old, value))
|
60
|
+
value = nil
|
61
|
+
else
|
62
|
+
value = column.type_cast(value, self)
|
63
63
|
end
|
64
|
-
|
65
|
-
old != value
|
66
|
-
end
|
67
|
-
|
68
|
-
def changes_from_nil_to_empty_string?(column, old, value)
|
69
|
-
# We don't record it as a change if the value changes from nil to ''.
|
70
|
-
# If an old value of 0 is set to '' we want this to get changed to nil as otherwise it'll
|
71
|
-
# be typecast back to 0 (''.to_i => 0)
|
72
|
-
(old.nil? || old == 0) && value.blank?
|
73
|
-
end
|
74
|
-
|
75
|
-
def changes_from_zero_to_string?(old, value)
|
76
|
-
# For columns with old 0 and value non-empty string
|
77
|
-
old == 0 && value.is_a?(String) && value.present? && non_zero?(value)
|
78
|
-
end
|
79
|
-
|
80
|
-
def non_zero?(value)
|
81
|
-
value !~ /\A0+(\.0+)?\z/
|
82
64
|
end
|
65
|
+
|
66
|
+
old != value
|
67
|
+
end
|
68
|
+
|
69
|
+
def changes_from_nil_to_empty_string?(_column, old, value)
|
70
|
+
# We don't record it as a change if the value changes from nil to ''.
|
71
|
+
# If an old value of 0 is set to '' we want this to get changed to nil as otherwise it'll
|
72
|
+
# be typecast back to 0 (''.to_i => 0)
|
73
|
+
(old.nil? || old == 0) && value.blank?
|
74
|
+
end
|
75
|
+
|
76
|
+
def changes_from_zero_to_string?(old, value)
|
77
|
+
# For columns with old 0 and value non-empty string
|
78
|
+
old == 0 && value.is_a?(String) && value.present? && non_zero?(value)
|
79
|
+
end
|
80
|
+
|
81
|
+
def non_zero?(value)
|
82
|
+
value !~ /\A0+(\.0+)?\z/
|
83
|
+
end
|
83
84
|
end
|
84
85
|
end
|
85
|
-
end
|
86
|
+
end
|
@@ -8,13 +8,13 @@ module DatastaxRails
|
|
8
8
|
# Returns this record's primary key value wrapped in an Array if one is
|
9
9
|
# available.
|
10
10
|
def to_key
|
11
|
-
key =
|
11
|
+
key = id
|
12
12
|
[key] if key
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
# Returns a primary key hash for updates. Wide models override this.
|
16
16
|
def id_for_update
|
17
|
-
{self.class.primary_key.to_s =>
|
17
|
+
{ self.class.primary_key.to_s => id }
|
18
18
|
end
|
19
19
|
|
20
20
|
# Returns the primary key value.
|
@@ -32,11 +32,6 @@ module DatastaxRails
|
|
32
32
|
query_attribute(self.class.primary_key)
|
33
33
|
end
|
34
34
|
|
35
|
-
# Returns the primary key value before type cast.
|
36
|
-
def id_before_type_cast
|
37
|
-
read_attribute_before_type_cast(self.class.primary_key)
|
38
|
-
end
|
39
|
-
|
40
35
|
protected
|
41
36
|
|
42
37
|
def attribute_method?(attr_name)
|
@@ -67,7 +67,7 @@ module DatastaxRails
|
|
67
67
|
|
68
68
|
def cacheable_column?(column)
|
69
69
|
if attribute_types_cached_by_default == ATTRIBUTE_TYPES_CACHED_BY_DEFAULT
|
70
|
-
!
|
70
|
+
!serialized_attributes.include? column.name
|
71
71
|
else
|
72
72
|
attribute_types_cached_by_default.include?(column.type)
|
73
73
|
end
|
@@ -79,35 +79,33 @@ module DatastaxRails
|
|
79
79
|
# to a date object, like Date.new(2004, 12, 12)).
|
80
80
|
def read_attribute(attr_name)
|
81
81
|
name = attr_name.to_s
|
82
|
-
|
82
|
+
|
83
83
|
# If it's a lazily loaded attribute and hasn't been loaded yet, we need to do that now.
|
84
|
-
if
|
85
|
-
@attributes[name.to_s] = self.class.select(name).with_cassandra.find(
|
84
|
+
if !loaded_attributes[name] && persisted? && !key.blank?
|
85
|
+
@attributes[name.to_s] = self.class.select(name).with_cassandra.find(id).read_attribute(name)
|
86
86
|
loaded_attributes[name] = true
|
87
87
|
end
|
88
|
-
|
88
|
+
|
89
89
|
# If it's cached, just return it
|
90
90
|
# We use #[] first as a perf optimization for non-nil values. See https://gist.github.com/jonleighton/3552829.
|
91
|
-
@attributes_cache[name] || @attributes_cache.fetch(name)
|
91
|
+
@attributes_cache[name] || @attributes_cache.fetch(name) do
|
92
92
|
column = @column_types_override[name] if @column_types_override
|
93
93
|
column ||= self.class.attribute_definitions[name]
|
94
94
|
|
95
|
-
return @attributes.fetch(name)
|
95
|
+
return @attributes.fetch(name) do
|
96
96
|
if name == 'id' && self.class.primary_key != name
|
97
97
|
read_attribute(self.class.primary_key)
|
98
98
|
end
|
99
|
-
|
99
|
+
end unless column
|
100
100
|
|
101
|
-
|
102
|
-
nil
|
103
|
-
}
|
101
|
+
value = @attributes.fetch(name) { nil }
|
104
102
|
|
105
103
|
if self.class.cache_attribute?(name)
|
106
104
|
@attributes_cache[name] = column.type_cast(value, self)
|
107
105
|
else
|
108
106
|
column.type_cast value, self
|
109
107
|
end
|
110
|
-
|
108
|
+
end
|
111
109
|
end
|
112
110
|
|
113
111
|
private
|
@@ -1,5 +1,7 @@
|
|
1
1
|
module DatastaxRails
|
2
2
|
module AttributeMethods
|
3
|
+
# Handles the mapping of attributes to their appropriate DatastaxRails::Column so that they can
|
4
|
+
# be typecasted.
|
3
5
|
module Typecasting
|
4
6
|
extend ActiveSupport::Concern
|
5
7
|
|
@@ -11,27 +13,25 @@ module DatastaxRails
|
|
11
13
|
self.lazy_attributes = []
|
12
14
|
self.readonly_attributes = []
|
13
15
|
end
|
14
|
-
|
16
|
+
|
15
17
|
# This is a hook for use by modules that need to do extra stuff to
|
16
18
|
# attributes when they are initialized. (e.g. attribute
|
17
19
|
# serialization)
|
18
20
|
def initialize_attributes(attributes) #:nodoc:
|
19
21
|
attrs = {}
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
end
|
30
|
-
end
|
31
|
-
else
|
32
|
-
attrs[k.to_s] = col.collection? ? col.wrap_collection(v, self) : v
|
22
|
+
attributes.each do |k, v|
|
23
|
+
col = column_for_attribute(k)
|
24
|
+
next unless col
|
25
|
+
if col.type == :map && k.to_s != col.name.to_s
|
26
|
+
# See if we have a matching dynamic attribute column
|
27
|
+
self.class.map_columns.each do |mcol|
|
28
|
+
if k.to_s.starts_with?(mcol.name.to_s)
|
29
|
+
attrs[mcol.name.to_s] ||= mcol.wrap_collection({}, self)
|
30
|
+
attrs[mcol.name.to_s][k.to_s] = v
|
33
31
|
end
|
34
32
|
end
|
33
|
+
else
|
34
|
+
attrs[k.to_s] = col.collection? ? col.wrap_collection(v, self) : v
|
35
35
|
end
|
36
36
|
end
|
37
37
|
attrs
|
@@ -40,47 +40,47 @@ module DatastaxRails
|
|
40
40
|
module ClassMethods
|
41
41
|
# Provide some measure of compatibility with things that expect this from ActiveRecord.
|
42
42
|
def columns_hash
|
43
|
-
|
43
|
+
attribute_definitions
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
# Gives you all of the map columns (useful for detecting dynamic columns)
|
47
47
|
def map_columns
|
48
|
-
@map_columns ||=
|
48
|
+
@map_columns ||= attribute_definitions.values.select { |c| c.type == :map }
|
49
49
|
end
|
50
|
-
|
50
|
+
|
51
51
|
# Returns a hash where the keys are column names and the values are
|
52
52
|
# default values when instantiating the DSR object for this table.
|
53
53
|
def column_defaults
|
54
54
|
@column_defaults ||= Hash[columns.map { |c| [c.name.to_s, c.default] }]
|
55
55
|
end
|
56
|
-
|
56
|
+
|
57
57
|
# We need to ensure that inherited classes get their own attribute definitions.
|
58
58
|
def inherited(child)
|
59
59
|
super
|
60
60
|
child.attribute_definitions = attribute_definitions.dup
|
61
61
|
end
|
62
|
-
|
62
|
+
|
63
63
|
# @!group Attribute Types
|
64
|
-
|
64
|
+
|
65
65
|
# @!macro [new] attr_doc
|
66
66
|
# Declare an attribute of the given type
|
67
|
-
#
|
67
|
+
#
|
68
68
|
# @param [Symbol] name the name of the attribute to create
|
69
69
|
# @param [Hash] options the options to use in setting up the attribute
|
70
70
|
def binary(name, options = {})
|
71
|
-
options.reverse_merge!(:
|
72
|
-
attribute(name, options.update(:
|
71
|
+
options.reverse_merge!(lazy: true)
|
72
|
+
attribute(name, options.update(type: :binary))
|
73
73
|
end
|
74
|
-
|
74
|
+
|
75
75
|
# Declare the timestamps attribute type method.
|
76
76
|
# Creates both the created_at and updated_at attributes with type +time+.
|
77
|
-
#
|
77
|
+
#
|
78
78
|
# @param [Hash] options the options to use in setting up the attribute
|
79
79
|
def timestamps(options = {})
|
80
|
-
attribute(:created_at, options.update(:
|
81
|
-
attribute(:updated_at, options.update(:
|
80
|
+
attribute(:created_at, options.update(type: :timestamp))
|
81
|
+
attribute(:updated_at, options.update(type: :timestamp))
|
82
82
|
end
|
83
|
-
|
83
|
+
|
84
84
|
# @!method array(name, options = {})
|
85
85
|
# @macro attr_doc
|
86
86
|
# @!method boolean(name, options = {})
|
@@ -113,16 +113,17 @@ module DatastaxRails
|
|
113
113
|
# @macro attr_doc
|
114
114
|
# @!method set(name, options = {})
|
115
115
|
# @macro attr_doc
|
116
|
-
|
116
|
+
|
117
117
|
# The following sets up a bunch of nearly identical attribute methods
|
118
|
-
%w(array boolean date datetime float integer json string text time timestamp time_with_zone
|
118
|
+
%w(array boolean date datetime float integer json string text time timestamp time_with_zone
|
119
|
+
uuid map set list).each do |type|
|
119
120
|
class_eval <<-EOV, __FILE__, __LINE__ + 1
|
120
|
-
def #{type}(name, options = {})
|
121
|
-
attribute(name, options.update(:type => :#{type}))
|
122
|
-
end
|
121
|
+
def #{type}(name, options = {}) # def string(name, options = {})
|
122
|
+
attribute(name, options.update(:type => :#{type})) # attribute(name, options.update(type: :string))
|
123
|
+
end # end
|
123
124
|
EOV
|
124
125
|
end
|
125
|
-
|
126
|
+
|
126
127
|
# @!endgroup
|
127
128
|
end
|
128
129
|
end
|
@@ -4,7 +4,7 @@ module DatastaxRails
|
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
|
6
6
|
included do
|
7
|
-
attribute_method_suffix
|
7
|
+
attribute_method_suffix '='
|
8
8
|
end
|
9
9
|
|
10
10
|
module ClassMethods
|
@@ -37,19 +37,18 @@ module DatastaxRails
|
|
37
37
|
|
38
38
|
# If we're dealing with a binary column, write the data to the cache
|
39
39
|
# so we don't attempt to typecast multiple times.
|
40
|
-
if column && column.binary?
|
41
|
-
@attributes_cache[attr_name] = value
|
42
|
-
end
|
40
|
+
@attributes_cache[attr_name] = value if column && column.binary?
|
43
41
|
|
44
|
-
if column || @attributes.
|
42
|
+
if column || @attributes.key?(attr_name)
|
45
43
|
@attributes[attr_name] = value
|
46
44
|
else
|
47
|
-
|
45
|
+
fail ActiveModel::MissingAttributeError, "can't write unknown attribute `#{attr_name}'"
|
48
46
|
end
|
49
47
|
end
|
50
48
|
alias_method :raw_write_attribute, :write_attribute
|
51
49
|
|
52
50
|
private
|
51
|
+
|
53
52
|
# Handle *= for method_missing.
|
54
53
|
def attribute=(attribute_name, value)
|
55
54
|
write_attribute(attribute_name, value)
|
@@ -4,7 +4,7 @@ module DatastaxRails
|
|
4
4
|
module AttributeMethods
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
include ActiveModel::AttributeMethods
|
7
|
-
|
7
|
+
|
8
8
|
included do
|
9
9
|
initialize_generated_modules
|
10
10
|
|
@@ -13,20 +13,20 @@ module DatastaxRails
|
|
13
13
|
include Read
|
14
14
|
include PrimaryKey
|
15
15
|
include Typecasting
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
16
|
+
|
17
|
+
alias_method :[], :read_attribute
|
18
|
+
alias_method :[]=, :write_attribute
|
19
|
+
alias_method :has_attribute?, :attribute_exists?
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
module ClassMethods
|
23
23
|
def inherited(child_class)
|
24
24
|
child_class.initialize_generated_modules
|
25
25
|
super
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
def initialize_generated_modules
|
29
|
-
@generated_attribute_methods = Module.new
|
29
|
+
@generated_attribute_methods = Module.new do
|
30
30
|
extend Mutex_m
|
31
31
|
|
32
32
|
const_set :AttrNames, Module.new {
|
@@ -37,11 +37,11 @@ module DatastaxRails
|
|
37
37
|
end
|
38
38
|
end
|
39
39
|
}
|
40
|
-
|
40
|
+
end
|
41
41
|
@attribute_methods_generated = false
|
42
42
|
include @generated_attribute_methods
|
43
43
|
end
|
44
|
-
|
44
|
+
|
45
45
|
def define_attribute_methods
|
46
46
|
# Use a mutex; we don't want two thread simultaneously trying to define
|
47
47
|
# attribute methods.
|
@@ -56,7 +56,7 @@ module DatastaxRails
|
|
56
56
|
end
|
57
57
|
true
|
58
58
|
end
|
59
|
-
|
59
|
+
|
60
60
|
def undefine_attribute_methods
|
61
61
|
generated_attribute_methods.synchronize do
|
62
62
|
super if attribute_methods_generated?
|
@@ -67,37 +67,32 @@ module DatastaxRails
|
|
67
67
|
def attribute_methods_generated?
|
68
68
|
@attribute_methods_generated ||= false
|
69
69
|
end
|
70
|
-
|
71
|
-
#
|
70
|
+
|
71
|
+
#
|
72
72
|
# attribute :name, :type => :string
|
73
73
|
# attribute :ammo, :type => Ammo, :coder => AmmoCodec
|
74
|
-
#
|
74
|
+
#
|
75
75
|
def attribute(name, options)
|
76
76
|
type = options.delete :type
|
77
77
|
coder = options.delete :coder
|
78
78
|
default = options.delete :default
|
79
79
|
|
80
|
-
if
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
if(options[:readonly])
|
85
|
-
readonly_attributes << name.to_sym
|
86
|
-
end
|
87
|
-
|
80
|
+
lazy_attributes << name.to_sym if options[:lazy]
|
81
|
+
readonly_attributes << name.to_sym if options[:readonly]
|
82
|
+
|
88
83
|
column = Column.new(name, default, type, options)
|
89
84
|
column.primary = (name.to_s == primary_key.to_s)
|
90
85
|
if coder
|
91
|
-
coder = coder.constantize rescue nil
|
86
|
+
coder = coder.constantize rescue nil # rubocop:disable Style/RescueModifier
|
92
87
|
if coder.class == Class && (coder.instance_methods & [:dump, :load]).size == 2
|
93
88
|
column.coder = coder.new(self)
|
94
89
|
else
|
95
|
-
|
90
|
+
fail ArgumentError, 'Coder must be a class that responds the dump and load instance variables'
|
96
91
|
end
|
97
92
|
end
|
98
93
|
attribute_definitions[name.to_sym] = column
|
99
94
|
end
|
100
|
-
|
95
|
+
|
101
96
|
# Returns the column object for the named attribute. Returns +nil+ if the
|
102
97
|
# named attribute not exists.
|
103
98
|
#
|
@@ -112,20 +107,19 @@ module DatastaxRails
|
|
112
107
|
# # => nil
|
113
108
|
def column_for_attribute(name)
|
114
109
|
# FIXME: should this return a null object for columns that don't exist?
|
115
|
-
column =
|
110
|
+
column = columns_hash[name.to_s]
|
116
111
|
unless column
|
117
112
|
# Check for a dynamic column
|
118
113
|
columns_hash.values.each do |col|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
end
|
114
|
+
next unless col.type == :map && name.to_s.starts_with?("#{col.name}")
|
115
|
+
column = col
|
116
|
+
break
|
123
117
|
end
|
124
118
|
end
|
125
119
|
column
|
126
120
|
end
|
127
121
|
end
|
128
|
-
|
122
|
+
|
129
123
|
def attribute_exists?(name)
|
130
124
|
@attributes.key?(name.to_s)
|
131
125
|
end
|
@@ -143,37 +137,39 @@ module DatastaxRails
|
|
143
137
|
self.class.define_attribute_methods unless self.class.attribute_methods_generated?
|
144
138
|
super
|
145
139
|
end
|
146
|
-
|
140
|
+
|
147
141
|
def column_for_attribute(name)
|
148
142
|
self.class.column_for_attribute(name)
|
149
143
|
end
|
150
144
|
|
151
145
|
protected
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
attributes
|
161
|
-
end
|
162
|
-
|
163
|
-
def clone_attribute_value(reader_method, attribute_name) # :nodoc:
|
164
|
-
value = send(reader_method, attribute_name)
|
165
|
-
value.duplicable? ? value.clone : value
|
166
|
-
rescue TypeError, NoMethodError
|
167
|
-
value
|
146
|
+
|
147
|
+
def attribute_method?(name)
|
148
|
+
attribute_definitions.key?(name.to_sym)
|
149
|
+
end
|
150
|
+
|
151
|
+
def clone_attributes(reader_method = :read_attribute, attributes = {}) # :nodoc:
|
152
|
+
attribute_names.each do |name|
|
153
|
+
attributes[name] = clone_attribute_value(reader_method, name)
|
168
154
|
end
|
155
|
+
attributes
|
156
|
+
end
|
157
|
+
|
158
|
+
def clone_attribute_value(reader_method, attribute_name) # :nodoc:
|
159
|
+
value = send(reader_method, attribute_name)
|
160
|
+
value.duplicable? ? value.clone : value
|
161
|
+
rescue TypeError, NoMethodError
|
162
|
+
value
|
163
|
+
end
|
169
164
|
|
170
165
|
private
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
166
|
+
|
167
|
+
def attribute(name)
|
168
|
+
read_attribute(name)
|
169
|
+
end
|
170
|
+
|
171
|
+
def attribute=(name, value)
|
172
|
+
write_attribute(name, value)
|
173
|
+
end
|
178
174
|
end
|
179
|
-
end
|
175
|
+
end
|