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.
Files changed (143) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +5 -5
  3. data/lib/blankslate.rb +8 -11
  4. data/lib/cql-rb_extensions.rb +5 -3
  5. data/lib/datastax_rails/associations/association.rb +93 -101
  6. data/lib/datastax_rails/associations/association_scope.rb +7 -7
  7. data/lib/datastax_rails/associations/belongs_to_association.rb +46 -48
  8. data/lib/datastax_rails/associations/builder/association.rb +32 -31
  9. data/lib/datastax_rails/associations/builder/belongs_to.rb +19 -20
  10. data/lib/datastax_rails/associations/builder/collection_association.rb +32 -32
  11. data/lib/datastax_rails/associations/builder/has_and_belongs_to_many.rb +21 -21
  12. data/lib/datastax_rails/associations/builder/has_many.rb +39 -40
  13. data/lib/datastax_rails/associations/builder/has_one.rb +30 -31
  14. data/lib/datastax_rails/associations/builder/singular_association.rb +31 -33
  15. data/lib/datastax_rails/associations/collection_association.rb +129 -135
  16. data/lib/datastax_rails/associations/collection_proxy.rb +21 -21
  17. data/lib/datastax_rails/associations/has_and_belongs_to_many_association.rb +26 -26
  18. data/lib/datastax_rails/associations/has_many_association.rb +38 -38
  19. data/lib/datastax_rails/associations/has_one_association.rb +31 -32
  20. data/lib/datastax_rails/associations/singular_association.rb +31 -30
  21. data/lib/datastax_rails/associations.rb +27 -24
  22. data/lib/datastax_rails/attribute_assignment.rb +17 -17
  23. data/lib/datastax_rails/attribute_methods/definition.rb +4 -4
  24. data/lib/datastax_rails/attribute_methods/dirty.rb +34 -33
  25. data/lib/datastax_rails/attribute_methods/primary_key.rb +3 -8
  26. data/lib/datastax_rails/attribute_methods/read.rb +10 -12
  27. data/lib/datastax_rails/attribute_methods/typecasting.rb +36 -35
  28. data/lib/datastax_rails/attribute_methods/write.rb +5 -6
  29. data/lib/datastax_rails/attribute_methods.rb +52 -56
  30. data/lib/datastax_rails/base.rb +122 -125
  31. data/lib/datastax_rails/callbacks.rb +15 -9
  32. data/lib/datastax_rails/cassandra_only_model.rb +6 -6
  33. data/lib/datastax_rails/collection.rb +5 -7
  34. data/lib/datastax_rails/column.rb +130 -118
  35. data/lib/datastax_rails/connection/statement_cache.rb +3 -3
  36. data/lib/datastax_rails/connection.rb +42 -33
  37. data/lib/datastax_rails/cql/alter_column_family.rb +19 -21
  38. data/lib/datastax_rails/cql/base.rb +8 -11
  39. data/lib/datastax_rails/cql/column_family.rb +11 -10
  40. data/lib/datastax_rails/cql/consistency.rb +2 -2
  41. data/lib/datastax_rails/cql/create_column_family.rb +15 -15
  42. data/lib/datastax_rails/cql/create_index.rb +5 -5
  43. data/lib/datastax_rails/cql/create_keyspace.rb +7 -7
  44. data/lib/datastax_rails/cql/delete.rb +16 -29
  45. data/lib/datastax_rails/cql/drop_column_family.rb +2 -2
  46. data/lib/datastax_rails/cql/drop_index.rb +2 -2
  47. data/lib/datastax_rails/cql/drop_keyspace.rb +2 -2
  48. data/lib/datastax_rails/cql/insert.rb +10 -16
  49. data/lib/datastax_rails/cql/select.rb +21 -33
  50. data/lib/datastax_rails/cql/truncate.rb +2 -2
  51. data/lib/datastax_rails/cql/update.rb +16 -24
  52. data/lib/datastax_rails/cql/use_keyspace.rb +2 -2
  53. data/lib/datastax_rails/cql.rb +2 -2
  54. data/lib/datastax_rails/dynamic_model.rb +32 -29
  55. data/lib/datastax_rails/errors.rb +6 -6
  56. data/lib/datastax_rails/grouped_collection.rb +3 -3
  57. data/lib/datastax_rails/inheritance.rb +9 -9
  58. data/lib/datastax_rails/payload_model.rb +24 -20
  59. data/lib/datastax_rails/persistence.rb +116 -110
  60. data/lib/datastax_rails/railtie.rb +7 -7
  61. data/lib/datastax_rails/reflection.rb +61 -59
  62. data/lib/datastax_rails/relation/batches.rb +12 -13
  63. data/lib/datastax_rails/relation/facet_methods.rb +44 -33
  64. data/lib/datastax_rails/relation/finder_methods.rb +95 -91
  65. data/lib/datastax_rails/relation/modification_methods.rb +5 -5
  66. data/lib/datastax_rails/relation/search_methods.rb +102 -102
  67. data/lib/datastax_rails/relation/spawn_methods.rb +25 -24
  68. data/lib/datastax_rails/relation/stats_methods.rb +9 -8
  69. data/lib/datastax_rails/relation.rb +165 -170
  70. data/lib/datastax_rails/rsolr_client_wrapper.rb +3 -3
  71. data/lib/datastax_rails/schema/cassandra.rb +44 -43
  72. data/lib/datastax_rails/schema/migrator.rb +52 -52
  73. data/lib/datastax_rails/schema/solr.rb +55 -47
  74. data/lib/datastax_rails/schema_cache.rb +1 -3
  75. data/lib/datastax_rails/scoping/default.rb +2 -3
  76. data/lib/datastax_rails/scoping/named.rb +3 -5
  77. data/lib/datastax_rails/scoping.rb +11 -12
  78. data/lib/datastax_rails/serialization.rb +34 -31
  79. data/lib/datastax_rails/serializers/xml_serializer.rb +178 -175
  80. data/lib/datastax_rails/timestamps.rb +4 -4
  81. data/lib/datastax_rails/types/dirty_collection.rb +57 -57
  82. data/lib/datastax_rails/types/dynamic_list.rb +1 -1
  83. data/lib/datastax_rails/types/dynamic_map.rb +5 -7
  84. data/lib/datastax_rails/types/dynamic_set.rb +2 -2
  85. data/lib/datastax_rails/util/solr_repair.rb +3 -3
  86. data/lib/datastax_rails/validations/associated.rb +8 -6
  87. data/lib/datastax_rails/validations/uniqueness.rb +8 -8
  88. data/lib/datastax_rails/validations.rb +9 -10
  89. data/lib/datastax_rails/version.rb +2 -1
  90. data/lib/datastax_rails/wide_storage_model.rb +6 -6
  91. data/lib/datastax_rails.rb +13 -9
  92. data/lib/schema_migration.rb +3 -3
  93. data/spec/datastax_rails/associations/belongs_to_association_spec.rb +2 -2
  94. data/spec/datastax_rails/associations/collection_association_spec.rb +14 -14
  95. data/spec/datastax_rails/associations/has_many_association_spec.rb +20 -20
  96. data/spec/datastax_rails/associations_spec.rb +11 -11
  97. data/spec/datastax_rails/attribute_methods_spec.rb +25 -25
  98. data/spec/datastax_rails/base_spec.rb +24 -24
  99. data/spec/datastax_rails/callbacks_spec.rb +21 -21
  100. data/spec/datastax_rails/column_spec.rb +133 -132
  101. data/spec/datastax_rails/connection/statement_cache_spec.rb +2 -2
  102. data/spec/datastax_rails/cql/base_spec.rb +4 -4
  103. data/spec/datastax_rails/cql/delete_spec.rb +19 -0
  104. data/spec/datastax_rails/cql/select_spec.rb +8 -8
  105. data/spec/datastax_rails/cql/update_spec.rb +8 -10
  106. data/spec/datastax_rails/dynamic_model_spec.rb +36 -22
  107. data/spec/datastax_rails/inheritance_spec.rb +11 -14
  108. data/spec/datastax_rails/persistence_spec.rb +73 -74
  109. data/spec/datastax_rails/relation/batches_spec.rb +13 -13
  110. data/spec/datastax_rails/relation/facet_methods_spec.rb +43 -35
  111. data/spec/datastax_rails/relation/finder_methods_spec.rb +77 -78
  112. data/spec/datastax_rails/relation/modification_methods_spec.rb +19 -19
  113. data/spec/datastax_rails/relation/search_methods_spec.rb +160 -160
  114. data/spec/datastax_rails/relation/spawn_methods_spec.rb +18 -18
  115. data/spec/datastax_rails/relation_spec.rb +119 -116
  116. data/spec/datastax_rails/schema/migrator_spec.rb +30 -30
  117. data/spec/datastax_rails/schema/solr_spec.rb +15 -15
  118. data/spec/datastax_rails/scoping/default_spec.rb +9 -9
  119. data/spec/datastax_rails/types/dynamic_list_spec.rb +12 -12
  120. data/spec/datastax_rails/types/dynamic_map_spec.rb +10 -10
  121. data/spec/datastax_rails/types/dynamic_set_spec.rb +22 -10
  122. data/spec/datastax_rails/validations/uniqueness_spec.rb +25 -25
  123. data/spec/datastax_rails/wide_storage_model_spec.rb +11 -0
  124. data/spec/datastax_rails_spec.rb +2 -2
  125. data/spec/dummy/config/application.rb +2 -3
  126. data/spec/dummy/config/boot.rb +1 -1
  127. data/spec/dummy/config/environments/development.rb +3 -3
  128. data/spec/dummy/config/environments/test.rb +1 -1
  129. data/spec/dummy/config/initializers/session_store.rb +1 -1
  130. data/spec/dummy/config/initializers/wrap_parameters.rb +1 -1
  131. data/spec/factories/audit_logs.rb +6 -0
  132. data/spec/factories/hobbies.rb +6 -0
  133. data/spec/factories/people.rb +5 -0
  134. data/spec/feature/dynamic_fields_spec.rb +4 -4
  135. data/spec/feature/overloaded_tables_spec.rb +11 -12
  136. data/spec/spec_helper.rb +17 -14
  137. data/spec/support/datastax_test_hook.rb +2 -2
  138. data/spec/support/default_consistency_shared_examples.rb +11 -11
  139. data/spec/support/models.rb +31 -32
  140. metadata +40 -6
  141. data/lib/datastax_rails/attribute_methods/before_type_cast.rb +0 -71
  142. data/lib/datastax_rails/log_subscriber.rb +0 -0
  143. data/spec/dummy/ks/migrate/20111117224534_models.rb +0 -20
@@ -1,3 +1,5 @@
1
+ require 'date'
2
+
1
3
  module DatastaxRails
2
4
  # = Datastax Rails Callbacks
3
5
  #
@@ -202,7 +204,8 @@ module DatastaxRails
202
204
  # == Ordering callbacks
203
205
  #
204
206
  # Sometimes the code needs that the callbacks execute in a specific order. For example, a +before_destroy+
205
- # callback (+log_children+ in this case) should be executed before the children get destroyed by the +dependent: destroy+ option.
207
+ # callback (+log_children+ in this case) should be executed before the children get destroyed by the
208
+ # +dependent: destroy+ option.
206
209
  #
207
210
  # Let's look at the code below:
208
211
  #
@@ -218,7 +221,8 @@ module DatastaxRails
218
221
  # end
219
222
  #
220
223
  # In this case, the problem is that when the +before_destroy+ callback is executed, the children are not available
221
- # because the +destroy+ callback gets executed first. You can use the +prepend+ option on the +before_destroy+ callback to avoid this.
224
+ # because the +destroy+ callback gets executed first. You can use the +prepend+ option on the +before_destroy+
225
+ # callback to avoid this.
222
226
  #
223
227
  # class Topic < DatastaxRails::Base
224
228
  # has_many :children, dependent: destroy
@@ -231,13 +235,14 @@ module DatastaxRails
231
235
  # end
232
236
  # end
233
237
  #
234
- # This way, the +before_destroy+ gets executed before the <tt>dependent: destroy</tt> is called, and the data is still available.
238
+ # This way, the +before_destroy+ gets executed before the <tt>dependent: destroy</tt> is called, and the data is
239
+ # still available.
235
240
  #
236
241
  # == Debugging callbacks
237
242
  #
238
243
  # The callback chain is accessible via the <tt>_*_callbacks</tt> method on an object. ActiveModel Callbacks support
239
- # <tt>:before</tt>, <tt>:after</tt> and <tt>:around</tt> as values for the <tt>kind</tt> property. The <tt>kind</tt> property
240
- # defines what part of the chain the callback runs in.
244
+ # <tt>:before</tt>, <tt>:after</tt> and <tt>:around</tt> as values for the <tt>kind</tt> property. The <tt>kind</tt>
245
+ # property defines what part of the chain the callback runs in.
241
246
  #
242
247
  # To find all callbacks in the before_save callback chain:
243
248
  #
@@ -245,7 +250,8 @@ module DatastaxRails
245
250
  #
246
251
  # Returns an array of callback objects that form the before_save chain.
247
252
  #
248
- # To further check if the before_save chain contains a proc defined as <tt>rest_when_dead</tt> use the <tt>filter</tt> property of the callback object:
253
+ # To further check if the before_save chain contains a proc defined as <tt>rest_when_dead</tt> use the
254
+ # <tt>filter</tt> property of the callback object:
249
255
  #
250
256
  # Topic._save_callbacks.select { |cb| cb.kind.eql?(:before) }.collect(&:filter).include?(:rest_when_dead)
251
257
  #
@@ -268,7 +274,7 @@ module DatastaxRails
268
274
  included do
269
275
  include ActiveModel::Validations::Callbacks
270
276
 
271
- define_model_callbacks :initialize, :find, :touch, :only => :after
277
+ define_model_callbacks :initialize, :find, :touch, only: :after
272
278
  define_model_callbacks :save, :create, :update, :destroy
273
279
  end
274
280
 
@@ -277,10 +283,10 @@ module DatastaxRails
277
283
  end
278
284
 
279
285
  # def touch(*) #:nodoc:
280
- # run_callbacks(:touch) { super }
286
+ # run_callbacks(:touch) { super }
281
287
  # end
282
288
 
283
- private
289
+ private
284
290
 
285
291
  def _create_or_update(*) #:nodoc:
286
292
  run_callbacks(:save) { super }
@@ -17,17 +17,17 @@ module DatastaxRails
17
17
  extend ActiveSupport::Concern
18
18
 
19
19
  included do
20
- default_scope -> {with_cassandra}
20
+ default_scope -> { with_cassandra }
21
21
  self.storage_method = :cql
22
22
  end
23
-
23
+
24
24
  module ClassMethods
25
25
  def attribute(name, options)
26
- opts = options.update(:solr_index => false, :solr_store => false,
27
- :multi_valued => false, :sortable => false,
28
- :tokenized => false, :fulltext => false)
26
+ opts = options.update(solr_index: false, solr_store: false,
27
+ multi_valued: false, sortable: false,
28
+ tokenized: false, fulltext: false)
29
29
  super(name, opts)
30
30
  end
31
31
  end
32
32
  end
33
- end
33
+ end
@@ -2,23 +2,21 @@ module DatastaxRails
2
2
  class Collection < Array
3
3
  # @!attribute [r] total_entries
4
4
  # @return [Fixnum] the total number of entries that match the search
5
- # @!attribute [r] last_column_name
6
- # @return [Fixnum] the last column that was returned in the search in case you limited the number of columns (not supported)
7
5
  # @!attribute [r] per_page
8
6
  # @return [Fixnum] the per page value of the search that produced these results (used by will_paginate)
9
7
  # @!attribute [r] current_page
10
8
  # @return [Fixnum] the current page of the search that produced these results (used by will_paginate)
11
9
  # @!attribute [r] facets
12
- # @return [Hash] the facet(s) result (field and/or range) e.g. results.facets => {"author"=>["vonnegut", 2, "asimov", 4]}
13
- attr_accessor :last_column_name, :total_entries, :per_page, :current_page, :facets, :highlights
14
-
10
+ # @return [Hash] the facet results (field and/or range)
11
+ attr_accessor :total_entries, :per_page, :current_page, :facets, :highlights
12
+
15
13
  def inspect
16
14
  "<DatastaxRails::Collection##{object_id} contents: #{super} last_column_name: #{last_column_name.inspect}>"
17
15
  end
18
-
16
+
19
17
  def total_pages
20
18
  return 1 unless per_page
21
19
  (total_entries / per_page.to_f).ceil
22
20
  end
23
21
  end
24
- end
22
+ end
@@ -1,20 +1,22 @@
1
1
  require 'set'
2
2
 
3
+ # TODO: Break this up into manageable pieces
4
+ # rubocop:disable Style/RescueModifier
3
5
  module DatastaxRails
4
- class Column
6
+ class Column # rubocop:disable Style/ClassLength
5
7
  TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON'].to_set
6
8
  FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF'].to_set
7
-
9
+
8
10
  module Format
9
11
  ISO_DATE = /\A(\d{4})-(\d\d)-(\d\d)\z/
10
12
  ISO_DATETIME = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(\.\d+)?\z/
11
- SOLR_TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ".force_encoding('utf-8').freeze
13
+ SOLR_TIME_FORMAT = '%Y-%m-%dT%H:%M:%SZ'.force_encoding('utf-8').freeze
12
14
  end
13
15
 
14
16
  attr_reader :name, :type, :cql_type, :solr_type, :options
15
17
  attr_accessor :primary, :coder, :default
16
18
 
17
- alias :encoded? :coder
19
+ alias_method :encoded?, :coder
18
20
 
19
21
  # Instantiates a new column in the table.
20
22
  #
@@ -30,7 +32,7 @@ module DatastaxRails
30
32
  def initialize(name, default, type, options = {})
31
33
  @name = name
32
34
  @type = type.to_sym
33
- raise ArgumentError, "Unknown type #{type}" unless self.klass
35
+ fail ArgumentError, "Unknown type #{type}" unless klass
34
36
  options[:holds] = 'string' if collection? && options[:holds].blank?
35
37
  @options = configure_options(@type, options).with_indifferent_access
36
38
  @cql_type = compute_cql_type(@type, @options)
@@ -39,36 +41,36 @@ module DatastaxRails
39
41
  @primary = nil
40
42
  @coder = nil
41
43
  end
42
-
44
+
43
45
  def configure_options(type, options)
44
46
  case type.to_sym
45
47
  when :set, :list, :map then
46
- configure_options(options[:holds], options).merge(:multi_valued => true)
48
+ configure_options(options[:holds], options).merge(multi_valued: true)
47
49
  when :binary then
48
- {:solr_index => false, :solr_store => false,
49
- :multi_valued => false, :sortable => false,
50
- :tokenized => false, :fulltext => false,
51
- :cql_index => false}
50
+ { solr_index: false, solr_store: false,
51
+ multi_valued: false, sortable: false,
52
+ tokenized: false, fulltext: false,
53
+ cql_index: false }
52
54
  when :boolean, :date, :time, :timestamp, :datetime, :float, :integer, :uuid then
53
- {:solr_index => true, :solr_store => true,
54
- :multi_valued => false, :sortable => true,
55
- :tokenized => false, :fulltext => false,
56
- :cql_index => false}
55
+ { solr_index: true, solr_store: true,
56
+ multi_valued: false, sortable: true,
57
+ tokenized: false, fulltext: false,
58
+ cql_index: false }
57
59
  when :string then
58
- {:solr_index => true, :solr_store => true,
59
- :multi_valued => false, :sortable => true,
60
- :tokenized => false, :fulltext => true,
61
- :cql_index => false}
60
+ { solr_index: true, solr_store: true,
61
+ multi_valued: false, sortable: true,
62
+ tokenized: false, fulltext: true,
63
+ cql_index: false }
62
64
  when :text then
63
- {:solr_index => true, :solr_store => true,
64
- :multi_valued => false, :sortable => false,
65
- :tokenized => true, :fulltext => true,
66
- :cql_index => false}
65
+ { solr_index: true, solr_store: true,
66
+ multi_valued: false, sortable: false,
67
+ tokenized: true, fulltext: true,
68
+ cql_index: false }
67
69
  else
68
- raise ArgumentError, "Unknown Type: #{type.to_s}"
70
+ fail ArgumentError, "Unknown Type: #{type}"
69
71
  end.merge(options)
70
72
  end
71
-
73
+
72
74
  # Returns +true+ if the column is either of type ascii or text.
73
75
  def text?
74
76
  [:ascii, :text].include?(type)
@@ -78,18 +80,18 @@ module DatastaxRails
78
80
  def number?
79
81
  [:decimal, :double, :float, :integer].include?(type)
80
82
  end
81
-
83
+
82
84
  # Returns +true+ if the column is of type binary
83
85
  def binary?
84
86
  [:binary].include?(type)
85
87
  end
86
-
88
+
87
89
  def collection?
88
90
  [:set, :list, :map].include?(type)
89
91
  end
90
92
 
91
- def has_default?
92
- !default.nil?
93
+ def default?
94
+ default.present?
93
95
  end
94
96
 
95
97
  # Returns the Ruby class that corresponds to the abstract data type.
@@ -110,7 +112,7 @@ module DatastaxRails
110
112
  end
111
113
 
112
114
  # Casts value (which can be a String) to an appropriate instance.
113
- def type_cast(value, record = nil, dest_type = nil)
115
+ def type_cast(value, record = nil, dest_type = nil) # rubocop:disable Style/CyclomaticComplexity
114
116
  return nil if value.nil?
115
117
  return coder.load(value) if encoded?
116
118
 
@@ -128,23 +130,26 @@ module DatastaxRails
128
130
  when :binary then klass.binary_to_string(value)
129
131
  when :boolean then klass.value_to_boolean(value)
130
132
  when :uuid, :timeuuid then klass.value_to_uuid(value)
131
- when :list, :set then wrap_collection(value.collect {|v| type_cast(v, record, @options[:holds])}, record)
132
- when :map then wrap_collection(value.each {|k,v| value[k] = type_cast(v, record, @options[:holds])}.stringify_keys, record)
133
+ when :list, :set then wrap_collection(value.map { |v| type_cast(v, record, @options[:holds]) }, record)
134
+ when :map
135
+ wrap_collection(value.each { |k, v| value[k] = type_cast(v, record, @options[:holds]) }.stringify_keys, record)
133
136
  else value
134
137
  end
135
138
  end
136
-
139
+
137
140
  def wrap_collection(collection, record)
138
- klass.new(record, name, collection)
141
+ Types::DirtyCollection.ignore_modifications do
142
+ klass.new(record, name, collection)
143
+ end
139
144
  end
140
-
145
+
141
146
  # Cql-rb does a really good job of typecasting, so for the most part we
142
147
  # just pass in the native types. The only exceptions are for UUIDs that
143
148
  # are passed in as strings and dates.
144
149
  def type_cast_for_cql3(value, dest_type = nil)
145
150
  return nil if value.nil?
146
151
  return coder.dump(value) if encoded?
147
-
152
+
148
153
  case (dest_type || type)
149
154
  when :uuid then value.is_a?(::Cql::Uuid) ? value : self.class.value_to_uuid(value)
150
155
  when :time, :datetime, :timestamp then value.to_time.utc
@@ -154,37 +159,37 @@ module DatastaxRails
154
159
  else value
155
160
  end
156
161
  end
157
-
162
+
158
163
  # By contrast, since Solr isn't doing things like prepared statements
159
164
  # it doesn't know what the types are so we have to handle any casting
160
165
  # or encoding ourselves.
161
166
  def type_cast_for_solr(value, dest_type = nil)
162
167
  return nil if value.nil?
163
168
  return coder.dump(value) if encoded?
164
-
169
+
165
170
  case (dest_type || type)
166
171
  when :boolean then value ? 'true' : 'false'
167
172
  when :date, :time, :datetime, :timestamp then value.to_time.utc.strftime(Format::SOLR_TIME_FORMAT)
168
- when :list, :set then self.list_to_solr_value(value)
169
- when :map then self.map_to_solr_value(value)
173
+ when :list, :set then list_to_solr_value(value)
174
+ when :map then map_to_solr_value(value)
170
175
  else value
171
176
  end
172
177
  end
173
-
178
+
174
179
  def list_to_solr_value(value)
175
- value.map {|v| type_cast_for_solr(v, @options[:holds].to_sym)}
180
+ value.map { |v| type_cast_for_solr(v, @options[:holds].to_sym) }
176
181
  end
177
-
182
+
178
183
  def map_to_solr_value(value)
179
- value.each {|k,v| value[k] = type_cast_for_solr(v, @options[:holds].to_sym)}
184
+ value.each { |k, v| value[k] = type_cast_for_solr(v, @options[:holds].to_sym) }
180
185
  end
181
-
186
+
182
187
  def list_to_cql3_value(value)
183
- value.map {|v| type_cast_for_cql3(v, @options[:holds].to_sym)}
188
+ value.map { |v| type_cast_for_cql3(v, @options[:holds].to_sym) }
184
189
  end
185
-
190
+
186
191
  def map_to_cql3_value(value)
187
- value.dup.each {|k,v| value[k] = type_cast_for_cql3(v, @options[:holds].to_sym)}
192
+ value.dup.each { |k, v| value[k] = type_cast_for_cql3(v, @options[:holds].to_sym) }
188
193
  value
189
194
  end
190
195
 
@@ -198,9 +203,9 @@ module DatastaxRails
198
203
 
199
204
  def extract_default(default)
200
205
  case type
201
- when :map then {}#lambda {|rec| DatastaxRails::Types::DynamicMap.new(rec, self.name.to_s, {})}
202
- when :list then []#lambda {|rec| DatastaxRails::Types::DynamicList.new(rec, self.name.to_s, [])}
203
- when :set then Set.new#lambda {|set| DatastaxRails::Types::DynamicSet.new(rec, self.name.to_s, Set.new)}
206
+ when :map then {} # lambda {|rec| DatastaxRails::Types::DynamicMap.new(rec, self.name.to_s, {})}
207
+ when :list then [] # lambda {|rec| DatastaxRails::Types::DynamicList.new(rec, self.name.to_s, [])}
208
+ when :set then Set.new # lambda {|set| DatastaxRails::Types::DynamicSet.new(rec, self.name.to_s, Set.new)}
204
209
  else default
205
210
  end
206
211
  end
@@ -209,9 +214,9 @@ module DatastaxRails
209
214
  def string_to_binary(value)
210
215
  self.class.string_to_binary(value)
211
216
  end
212
-
217
+
213
218
  def full_solr_range
214
- if %w[date uuid].include? solr_type
219
+ if %w(date uuid).include? solr_type
215
220
  '[* TO *]'
216
221
  else
217
222
  '[\"\" TO *]'
@@ -248,7 +253,7 @@ module DatastaxRails
248
253
 
249
254
  fast_string_to_time(string) || fallback_string_to_time(string)
250
255
  end
251
-
256
+
252
257
  def string_to_dummy_time(string)
253
258
  return string unless string.is_a?(String)
254
259
  return nil if string.empty?
@@ -294,7 +299,7 @@ module DatastaxRails
294
299
  value.to_s.to_d
295
300
  end
296
301
  end
297
-
302
+
298
303
  # convert something to a TimeUuid
299
304
  def value_to_uuid(value)
300
305
  if value.is_a?(::Cql::Uuid)
@@ -303,84 +308,91 @@ module DatastaxRails
303
308
  ::Cql::TimeUuid.new(value) rescue nil
304
309
  end
305
310
  end
306
-
311
+
307
312
  protected
308
- # '0.123456' -> 123456
309
- # '1.123456' -> 123456
310
- def microseconds(time)
311
- time[:sec_fraction] ? (time[:sec_fraction] * 1_000_000).to_i : 0
312
- end
313
313
 
314
- def new_date(year, mon, mday)
315
- if year && year != 0
316
- Date.new(year, mon, mday) rescue nil
317
- end
318
- end
314
+ # '0.123456' -> 123456
315
+ # '1.123456' -> 123456
316
+ def microseconds(time)
317
+ time[:sec_fraction] ? (time[:sec_fraction] * 1_000_000).to_i : 0
318
+ end
319
319
 
320
- def new_time(year, mon, mday, hour, min, sec, microsec, offset = nil)
321
- # Treat 0000-00-00 00:00:00 as nil.
322
- return nil if year.nil? || (year == 0 && mon == 0 && mday == 0)
320
+ def new_date(year, mon, mday)
321
+ year && year != 0 && Date.new(year, mon, mday) rescue nil
322
+ end
323
323
 
324
- if offset
325
- time = Time.utc(year, mon, mday, hour, min, sec, microsec) rescue nil
326
- return nil unless time
324
+ def new_time(year, mon, mday, hour, min, sec, microsec, offset = nil) # rubocop:disable Style/ParameterLists
325
+ # Treat 0000-00-00 00:00:00 as nil.
326
+ return nil if year.nil? || (year == 0 && mon == 0 && mday == 0)
327
327
 
328
- time -= offset
329
- Base.default_timezone == :utc ? time : time.getlocal
330
- else
331
- Time.public_send(Base.default_timezone, year, mon, mday, hour, min, sec, microsec) rescue nil
332
- end
333
- end
328
+ if offset
329
+ time = Time.utc(year, mon, mday, hour, min, sec, microsec) rescue nil
330
+ return nil unless time
334
331
 
335
- def fast_string_to_date(string)
336
- if string =~ Format::ISO_DATE
337
- new_date $1.to_i, $2.to_i, $3.to_i
338
- end
332
+ time -= offset
333
+ Base.default_timezone == :utc ? time : time.getlocal
334
+ else
335
+ Time.public_send(Base.default_timezone, year, mon, mday, hour, min, sec, microsec) rescue nil
339
336
  end
337
+ end
340
338
 
341
- # Doesn't handle time zones.
342
- def fast_string_to_time(string)
343
- if string =~ Format::ISO_DATETIME
344
- microsec = ($7.to_r * 1_000_000).to_i
345
- new_time $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, microsec
346
- end
339
+ def fast_string_to_date(string)
340
+ if string =~ Format::ISO_DATE
341
+ new_date Regexp.last_match[1].to_i, Regexp.last_match[2].to_i, Regexp.last_match[3].to_i
347
342
  end
343
+ end
348
344
 
349
- def fallback_string_to_date(string)
350
- new_date(*::Date._parse(string, false).values_at(:year, :mon, :mday))
351
- end
345
+ # Doesn't handle time zones.
346
+ def fast_string_to_time(string)
347
+ return unless string =~ Format::ISO_DATETIME
348
+ microsec = (Regexp.last_match[7].to_r * 1_000_000).to_i
349
+ new_time(Regexp.last_match[1].to_i,
350
+ Regexp.last_match[2].to_i,
351
+ Regexp.last_match[3].to_i,
352
+ Regexp.last_match[4].to_i,
353
+ Regexp.last_match[5].to_i,
354
+ Regexp.last_match[6].to_i,
355
+ microsec
356
+ )
357
+ end
352
358
 
353
- def fallback_string_to_time(string)
354
- time_hash = Date._parse(string)
355
- time_hash[:sec_fraction] = microseconds(time_hash)
359
+ def fallback_string_to_date(string)
360
+ new_date(*::Date._parse(string, false).values_at(:year, :mon, :mday))
361
+ end
356
362
 
357
- new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction))
358
- end
363
+ def fallback_string_to_time(string)
364
+ time_hash = Date._parse(string)
365
+ time_hash[:sec_fraction] = microseconds(time_hash)
366
+
367
+ new_time(*time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction))
368
+ end
359
369
  end
360
-
370
+
361
371
  private
362
-
363
- def compute_cql_type(field_type, options)
364
- options[:cql_type] || case field_type.to_sym
365
- when :integer then 'int'
366
- when :time, :date, :timestamp, :datetime then 'timestamp'
367
- when :binary then 'blob'
368
- when :list then "list<#{compute_cql_type(options[:holds], options)}>"
369
- when :set then "set<#{compute_cql_type(options[:holds], options)}>"
370
- when :map then "map<text, #{compute_cql_type(options[:holds], options)}>"
371
- when :string then 'text'
372
- else field_type.to_s
373
- end
372
+
373
+ def compute_cql_type(field_type, options)
374
+ options[:cql_type] ||
375
+ case field_type.to_sym
376
+ when :integer then 'int'
377
+ when :time, :date, :timestamp, :datetime then 'timestamp'
378
+ when :binary then 'blob'
379
+ when :list then "list<#{compute_cql_type(options[:holds], options)}>"
380
+ when :set then "set<#{compute_cql_type(options[:holds], options)}>"
381
+ when :map then "map<text, #{compute_cql_type(options[:holds], options)}>"
382
+ when :string then 'text'
383
+ else field_type.to_s
374
384
  end
375
-
376
- def compute_solr_type(field_type, options)
377
- options[:solr_type] || case field_type.to_sym
378
- when :integer then 'int'
379
- when :decimal then 'double'
380
- when :timestamp, :time, :datetime then 'date'
381
- when :list, :set, :map then compute_solr_type(options[:holds], options)
382
- else field_type.to_s
383
- end
385
+ end
386
+
387
+ def compute_solr_type(field_type, options)
388
+ options[:solr_type] ||
389
+ case field_type.to_sym
390
+ when :integer then 'int'
391
+ when :decimal then 'double'
392
+ when :timestamp, :time, :datetime then 'date'
393
+ when :list, :set, :map then compute_solr_type(options[:holds], options)
394
+ else field_type.to_s
384
395
  end
396
+ end
385
397
  end
386
398
  end