lib/dynamoid/criteria/chain.rb in dynamoid-1.3.4 vs lib/dynamoid/criteria/chain.rb in dynamoid-2.0.0

- old
+ new

@@ -16,10 +16,15 @@ def initialize(source) @query = {} @source = source @consistent_read = false @scan_index_forward = true + + # Honor STI and :type field if it presents + if @source.attributes.key?(:type) + @query[:'type.in'] = @source.deep_subclasses.map(&:name) << @source.name + end end # The workhorse method of the criteria chain. Each key in the passed in hash will become another criteria that the # ultimate query must match. A key can either be a symbol or a string, and should be an attribute name or # an attribute name with a range operator. @@ -54,36 +59,40 @@ def all records end # Returns the last fetched record matched the criteria + # Enumerable doesn't implement `last`, only `first` + # So we have to implement it ourselves # def last - all.last + all.to_a.last end # Destroys all the records matching the criteria. # - def destroy_all + def delete_all ids = [] + ranges = [] if key_present? - ranges = [] Dynamoid.adapter.query(source.table_name, range_query).collect do |hash| ids << hash[source.hash_key.to_sym] - ranges << hash[source.range_key.to_sym] + ranges << hash[source.range_key.to_sym] if source.range_key end - Dynamoid.adapter.delete(source.table_name, ids,{:range_key => ranges}) + Dynamoid.adapter.delete(source.table_name, ids, range_key: ranges.presence) else - Dynamoid.adapter.scan(source.table_name, query, scan_opts).collect do |hash| + Dynamoid.adapter.scan(source.table_name, scan_query, scan_opts).collect do |hash| ids << hash[source.hash_key.to_sym] + ranges << hash[source.range_key.to_sym] if source.range_key end - Dynamoid.adapter.delete(source.table_name, ids) + Dynamoid.adapter.delete(source.table_name, ids, range_key: ranges.presence) end end + alias_method :destroy_all, :delete_all # The record limit is the limit of evaluated records returned by the # query or scan. def record_limit(limit) @record_limit = limit @@ -119,28 +128,23 @@ # @since 0.2.0 def each(&block) records.each(&block) end - def consistent_opts - { :consistent_read => consistent_read } - end - private # The actual records referenced by the association. # # @return [Enumerator] an iterator of the found records. # # @since 0.2.0 def records - results = if key_present? + if key_present? records_via_query else records_via_scan end - @batch_size ? results : Array(results) end def records_via_query Enumerator.new do |yielder| Dynamoid.adapter.query(source.table_name, range_query).each do |hash| @@ -171,21 +175,21 @@ name, operation = key.to_s.split('.') val = type_cast_condition_parameter(name, query[key]) case operation when 'gt' - { :range_greater_than => val } + { range_greater_than: val } when 'lt' - { :range_less_than => val } + { range_less_than: val } when 'gte' - { :range_gte => val } + { range_gte: val } when 'lte' - { :range_lte => val } + { range_lte: val } when 'between' - { :range_between => val } + { range_between: val } when 'begins_with' - { :range_begins_with => val } + { range_begins_with: val } end end def field_hash(key) name, operation = key.to_s.split('.') @@ -213,10 +217,14 @@ end return { name.to_sym => hash } end + def consistent_opts + { consistent_read: consistent_read } + end + def range_query opts = {} # Add hash key opts[:hash_key] = @hash_key @@ -225,11 +233,11 @@ # Add range key if @range_key opts[:range_key] = @range_key if query[@range_key].present? value = type_cast_condition_parameter(@range_key, query[@range_key]) - opts.update(:range_eq => value) + opts.update(range_eq: value) end query.keys.select { |k| k.to_s =~ /^#{@range_key}\./ }.each do |key| opts.merge!(range_hash(key)) end @@ -259,10 +267,10 @@ def key_present? query_keys = query.keys.collect { |k| k.to_s.split('.').first } # See if querying based on table hash key - if query_keys.include?(source.hash_key.to_s) + if query.keys.map(&:to_s).include?(source.hash_key.to_s) @hash_key = source.hash_key # Use table's default range key if query_keys.include?(source.range_key.to_s) @range_key = source.range_key